Front-end and mobile development-Git-Git basic use

Git

Insert picture description here

Ready to work

The previous way to manage versions

Insert picture description here

Version control software

What is version control software

  • A software that records file changes
  • The version is a snapshot of each recorded code

scenes to be used

  • Scenario 1: The project is constantly changing, and it may need to be changed back to the original code
    • You can record every change to the code, record once, and form a version snapshot
    • You can easily roll back the code to any version. (You can use the code and files in the previous version)
    • The computer is broken, and the version will not be lost (premise: you have to upload the git repository to the remote server)
  • Scenario 2: Multiple people collaborate to develop the same project, and the code is merged into a project folder
    • Use version management software to merge code

Many benefits

  • Only need to record a few commands, get started quickly, automatically manage versions (produce a copy-not visible)

  • Easy to compare: differences and changes between each version

  • Easy to trace back: such as rolling back the code to the previous stable version

  • Not easy to lose: it can be hosted on the server (and everyone has all the version records in the computer), and the local/server computer is not afraid

    Insert picture description here

  • Convenient collaboration: Easily realize the development of the same project by multiple people, and merge the code written by everyone into a project folder

Introduction to Git

Git is an open source distributed version control system software, the most advanced and popular version control system.

Git records complete file snapshots

The disconnected network can also be saved locally (but wait for the network to upload to the central server to synchronize to others)

Git installation

Do not install Chinese and special path, do not move the directory of git software

Download link: https://git-scm.com/

The installation can be completed by default in the next step

Open the cmd terminal and enter the command git --version(note that the space in between is required)

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-nmb4H5fV-1609125020693)(images/image-20201129200436492.png)]

If the version number appears, it means that the git software has been successfully installed on your computer

Git concept-3 areas and 3 states

Insert picture description here

  • Work area
    • The file is displayed on the disk, the area for us to use or modify. The project folder is the workspace
  • storage cache
    • Executed git add .after a file from the working area, added to the staging area. The temporary storage area saves the file list information that will be submitted next time.
  • Git warehouse (local warehouse)
    • Execute git commit -m '提交说明'(single quotes required), temporary storage area -> Git local warehouse.
    • The warehouse area is the most important part of Git. Only when the code is submitted to the warehouse, will it form a historical record and form a version (a snapshot will be recorded after submission)
  • Remote warehouse
    • The warehouse on the Git server, for example, the warehouse created on the code cloud gitee or github website, or the git remote warehouse created by your own server is called a remote warehouse. Remote warehouses are used to store code and release projects, and multi-person collaboration also requires remote warehouses.

Insert picture description here

Git basic use

Local-configure user name and mailbox globally (one only needs once)

When Git software is working, you need to know who you are? So you need to set up a username and email.

This user name and email address, it is best to use your code cloud account, of course, you can fill in casually. (Let others be able to contact you)

specific methods:

  1. Any folder, blank space, right click --> Git Bash Here

  2. Execute the following two lines of code in turn-global will save the configuration items to the user configuration (in the future, this computer, no matter which git project folder will use this name and mailbox

  3. Execute command-set your name

  4. git config --global user.name "dongsancheng"
    
  5. Execute command-set your contact email

  6. git config --global user.email "[email protected]"
    
  • Will be configured to C:\Users\lenovo\.gitconfig中

Git initialization

Initialize the local git environment, let git monitor local files, and build three major areas

Create a project folder, open the project folder

注意:一定要在项目文件夹, Right click -> Git Bash Here.

Execute command-initialize .git folder

  • git init
  • Executed git initafter the command, generates a hidden folder in the project .gitfolder
  • Every record version snapshot of the Git management project is in .gitStored in the folder. and soThis folder cannot be deleted
  • The project does not have ==.git==, only neededgit init

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-dZ8Y8ilG-1609125020698)(images/image-20201129232421858.png)]

  • Mac system: shift Command. Can show hidden files

Git record-code changes

  1. Write code test

    • Create a file at will, and write some code in it
  2. In the project folder , right click, Git Bash Here

    • Execute the command (. represents the project folder-save everything in the workspace to the temporary storage area)

      • Only added to the staging area, it is called managed by Git
      • Empty folders cannot be staged
    • git add .  
      
    • Then execute the command (represents saving everything in the temporary storage area to the git local warehouse)

    • git commit -m '提交说明' 
      
    • After execution, these two commands indicate that the current code and file changes have been recorded using Git-onceSnapshot

  3. Repeat the 2 steps 1-2, this process 4 times, use Git to record several times

View-History

View commit log

  • git log-print detailed information

  • Execute the command: (View the submitted log-display briefly on one line)

  • git log --oneline
    

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-adlxhmwO-1609125020701)(images/image-20201129234558773.png)]

Experience-Shuttle through history

  • Execute the command (you can shuttle in the historical version (the version number refers to the above figure 3885368))
  • git checkout 版本号   
    
  • Execute the command (you can switch back to "now")-master represents the most recent commit

  • git checkout master
    

After shuttle-view all version numbers

After the shuttle to the historical version, by git logviewing the version number is less than the future

You need to add --reflogoption to view all the version number

  • Execute the command (see all version numbers, short version)
  • git reflog --oneline
    

Check the status of the file

After modifying/adding the code again-if there are too many, how to check what files have changed locally?

  • Execute commands (view several states of files)
git status
  • Untracked-new file

    Insert picture description here

  • Modified-modified

    Insert picture description here

  • Temporary-execute git add. Check git status later

    Insert picture description here

  • Submitted-execute git commit -m'submit information description' and run git status later

    Insert picture description here

    • Indicates that there is nothing to submit; that is, all the content has been submitted (all have been saved to the local warehouse)

If you see a file with red font or green font, it means that there are still files that have not been submitted to the local warehouse to form a snapshot (git is not responsible for reporting errors)

Git advanced_branch

Create branch

When using Git to manage a project, there will be many commits. Connect each submission as a line. This is called branching.

After initialization, the default is to operate on the master branch, which is also called the master branch.

Related commands:

  • Execute command-view all local branches

  • git branch
    
  • Execute command-create a new branch (dev is called the branch name, whatever you want)

  • git branch dev
    
  • Execute the command-switch branches (now the local code is under the dev branch)-if you want to return to master, write dev as master

  • git checkout dev
    

test:

  • On the dev branch, write some code and submit it (temporary storage, submission)
  • Switch back to the master branch to see the code difference

Before switching branches, you must submit all the code of the current branch to the warehouse

If you need to merge the code from the dev branch to the master branch

  • Switch to master

  • Execute command-currently on the master branch, merge the modified code on dev

  • git merge dev
    

Print git log --oneline to view the master branch and dev branch

Two combinations

Fast-forward mode (Fast-forward)

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-hmKTDMph-1609125020708)(images/image-20201201215140155.png)]

  • The dev branch, which contains all the commit records of the master branch

  • When merging, you only need to make the code of the maser branch the same as the code of the dev branch.

  • Execute command-delete useless branch (will not affect the code in master -d means delete)

  • git branch -d dev
    

Merge mode

  • New newdev branch

  • git branch newdev
    
  • Switch to the newdev branch

  • git checkout newdev
    
  • Then delete a bit of the original js code, and modify the new code on it (make sure it is different from the original master state)

  • Then switch to the master branch

  • git checkout master
    
  • The same place in the master branch was also modified, and then submitted by yourself (very important, otherwise the operation will not show conflicts)

  • Under the master branch, merge the contents of the newdev branch

  • git merge newdev
    
  • Report the conflict as follows:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-o1UUVFio-1609125020709)(images/image-20201130151519258.png)]

  • In other words, one branch does not include all the commit records of another branch

Insert picture description here

  • In this mode of merging, there may be conflicts in the above screen, you need to perform the following operations:

  • Go back to vscode to check the code conflict, and solve: find out who the other party is, delete whose one, or keep it all

Conflict resolution

When merging the code, there may be conflicts. If you encounter a conflict, resolve the conflict and submit it.

Execution git merge 分支名time, if the following screen appears, indicating that there is a conflict:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-0oHREqCp-1609125020712)(images/image-20201130151849196.png)]

  • Open conflicting file
  • Remove the dividing line
  • Conflict resolution
  • Save the code execution git add .and git commit -m '提交说'to complete the merger.

Remote warehouse

Registration code cloud account

Both Code Cloud and github provide warehouse hosting services.

The difference is that Code Cloud is a domestic server, and the opening speed is relatively fast; github is a foreign server, and the opening speed cannot be guaranteed.

Code Cloud: https://gitee.com/-use this

github:https://github.com/

If you can log in successfully, it means the registration is successful. Remember the account/login method

Configure Code Cloud SSH key

The secret key is a key, the function of opening the door (no login)

When we push the code to the code cloud or github website, it is best to use the SSH solution, which requires the key to be configured in advance. The following is the specific configuration method.

In any folder location, right-click on the blank space, select " Git Bash Here" in the right-click menu, and execute the following command to generate an SSH key (including a public key and a private key)

  • Execute command (generate key file)

    • ssh-keygen fixed command to generate secret key file
    • -t specifies the key type (encryption method-rsa encryption)
    • -C Set note text (how to contact you)
  • ssh-keygen -t rsa -C "[email protected]"
    

Note that press Enter to execute the command, and then continue to press Enter, press Enter... until the generation is complete~

The last generated secret key is in:

Windows:
C:\Users\用户名\.ssh
# 例如: C:\Users\lenovo\.ssh

mac:
你的用户名那个文件夹中。 按shift+Commend+.  可以显示隐藏文件,然后就可以看到 .ssh文件夹了

Find it .ssh/id_rsa.pub, open it with vscode, copy all the content inside, and configure it to the remote warehouse

(Important) It is equivalent to establishing a login link with a remote warehouse other than account password

  • Settings under the avatar in the upper right corner of Code Cloud
  • SSH public key on the left
  • In the displayed interface, paste all the values ​​in .id_rsa.pub in the public key, and the title is automatically filled in
  • Click OK

Create remote warehouse-get SSH address

Create a project on the website and apply for a disk space (if you have a server, it is the same to build a git server service on the server)

  1. gitee.com homepage after login
  2. + Sign in the upper right corner, new warehouse

Don't make your own company projects public, it may be stolen by hostile agencies

Insert picture description here

  1. After clicking create Do not click to initialize the readme file

Insert picture description here

Push code to remote

Existing warehouse locally

  1. Execute command-add remote warehouse address locally (remote branch name is origin)
  • git remote add origin 你的地址
    
    • remote
    • add add
    • Origin, the name can be customized, it doesn't matter what it is called.
    • Finally, it is the ssh address (note, not the https address)
  • If you report an error: If you are prompted to use the name of the local warehouse, you can remove git remote remove origin and then execute this command
  • If there is nothing on the remote warehouse git push -u origin master
    • push
    • origin The remote warehouse address, which corresponds to the origin in the previous command
    • master, means pushing the local master branch
    • -u, -u is required for the first push. (Indicates that the local master is directly associated with the remote master) ((full name is upstream, upload)
  • If there is something in the remote warehouse, First pull down and merge with the files in the local warehouse (rebase is to merge the remote files directly into the local master)
    • git remote add origin your address
    • git pull origin master --rebase (one more step)
    • Execute again, git push -u origin master
  • You can go to the webpage to see the remote warehouse to see it all came up hahaha

Only the code in the local warehouse can be pushed to the remote warehouse; the code in the work area and the staging area cannot be pushed to the remote.

SSH warning

If you use SSH for the first time, an SSH warning will appear, prompting as follows

  • If it prompts " Are you sure you want to continue connecting (yes/no/[fi....])"
    • Type yes and press enter

Insert picture description here

Precautions

A remote warehouse can only put the code of a local warehouse

Insert picture description here

Is SSH not https

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-WvlbbM9O-1609125020718)(images/image-20200712102055340.png)]

Newcomer Report-Pull remote warehouse code to local

No warehouse locally

Ask the git address and secret key from the group leader

  1. Paste the secret key into the same position we created above
  2. Create a new blank folder (must be blank and no hidden files)
  3. Run the command directly (without initializing git)

Clone remote warehouse to local

  • In actual development, sometimes, we need to clone a remote warehouse locally

    • When developing a project, you can first create a remote warehouse, then clone it to the local, and then develop
    • When downloading other people’s code, you can choose to clone
    • When multi-person development, you need to clone the code of the partner to the local
  • Clone command-I want to execute this command again in the folder where the code is downloaded in the remote warehouse

    git clone [email protected]:lidongxuwork/test_119.git .
    

    Clone, equivalent to download

  • If the cloned own warehouse, so after cloning, you can modify the code, and then modify, direct add, commit, pull, pushyou can.

  • Note : After cloning, the path is not the path of the warehouse. Need to close the black window, enter the project folder, and reopen the black window

View branch

git branch -a (-a means that the remote branch is also viewed together)

Multiplayer collaboration

Implementation principle:

  • Branch merge (merge remote branch and local branch together)

Implementation steps:

  • You can create a remote warehouse first
  • [Optional] I will first push the basic code to the remote warehouse
  • Collaborators, you need to clone the remote warehouse code to your computer
  • The administrator invites collaborators to develop together
  • Collaborators, need to agree
  • Then, both the administrator and the collaborator can push code to the remote warehouse
    • Staging git add.
    • Submit git commit -m'message'
    • (Merging with the remote server)-You must first pull git pull (provided that you have interacted with the remote server, otherwise, please check the remote warehouse configuration chapter for the first interaction with the server)
    • After pulling, if there is a conflict, to resolve the conflict, you must add and commit once.
    • Finally push. git push can synchronize your newly written code to the remote server (others pull it, others will have your newly written code)

Help document

git help config-automatically pop up English documents

Chinese document: http://gitref.justjavac.com/

Ignore files

.gitignore This is an ignore file

Sometimes there are some files in the project, no need to let git manage, we can configure in the ignore file

Usage rules:

#Beginning with comments

/ Ends with the directory

/ To prevent recursive folders

! At the beginning means negation

You can also use regular expressions to match files

# 忽略 index.css文件 (无论哪里的)
index.css

# 忽略所有的 .a 结尾的文件
*.a 

# 只忽略当前.gitignore文件同级目录下的TODO文件夹, 不忽略abc/TODO
/TODO  

# 忽略任何目录下名为build的文件夹
build/
    
# 忽略 doc/a.txt  doc/cbd.txt 但不忽略doc/ab/a.txt
doc/*.txt

# 忽略 doc/ 目录以及所有子目录下的 .pdf文件
doc/**/*.pdf

Graphical interface

  • vscode
  • webstorm
  • sourceTree (the latest version of sourceTree, lower versions of windows may not be installed)
  • Little turtle (available in windows system)

vscode

  • Temporary files (git add .)

    Insert picture description here

  • Submit to local warehouse

    Insert picture description here

  • Push

    Insert picture description here

    Insert picture description here

    Insert picture description here

    Insert picture description here

Version management software classification

  • Centralized, typical of SVN
  • Distributed, typical of Git

SSH local configuration for multiple accounts

I have my own gitee account/ github account. Each account has a separate secret key/ my colleague’s secret key (I want to pull and push the code)

.ssh/config file-just fill in these configurations

The secret key for the link to your own gitee website

Host gitee.com

HostName gitee.com

IdentityFile ~/.ssh/id_rsa_ziji_gitee

Colleague's gitee

Host gitee.com

HostName gitee.com

IdentityFile ~/.ssh/id_rsa_tz

If there are any shortcomings, please advise, to
be continued, continue to update!
Make progress together!

Guess you like

Origin blog.csdn.net/qq_40440961/article/details/111836116