git version management tool command details

Download git tools

Download link: Portal
windows can be directly opened and installed by next.
mac can download xcode directly from the app store.
After the installation is completed, enter the following command to check whether the installation is successful

git --version

git management mode

git has four working areas: working directory, temporary storage area, local warehouse and remote warehouse (github, gitlab...)

The working directory is where we need to write code locally. The staging area is used as a medium between the working area and the local warehouse. When we need to submit the code of the working directory to the local warehouse or remote warehouse, we need to add it to the staging area first.

git initializes the local library

First, we create a directory as the local repository

# mkdir file name
mkdir test  //Create a warehouse named test.
cd ./test //Switch directories to this warehouse
git init //Initialize local warehouse

At this time, we have initialized. The system will create a. git hidden directory in the test directory. We can enter the command to view it

ls -lA

Set git signature

Signature form

  • user name
  • mailbox

Main role

Distinguish the identities of different developers. The account and email here have nothing to do with the remote login code hosting platform.

Signature level

  • System level: valid globally.
  • Project level: valid only in this project.
    The project level signature priority is higher than the system level system level priority. When both exist, the project level signature will be preferred. It is not allowed when neither of them exists. One must be set.

Project level signature settings

set command

git config user.name user name //Set signature user name
git config user.email mailbox //Set signature mailbox

Command example

git config user.name hello
git config user.name 1234@qq.com

View current project signature

cat .git/config //In fact, the user signature mainly exists in the. git/config file

Global signature settings

It is the same as the method of setting the project, that is, there is one more – global

git config --global user.name user name //Set signature user name
git config --global user.email mailbox //Set signature mailbox

View global signatures

cat .gitconfig

Submit file to staging area

git add file name

Undo files submitted to staging

git rm --cached file name

Submit files from staging area to local warehouse

git commit -m "Submission description" [Submit file name,Commit all staging files when not specified]

Submit modified (tracked) documents directly to the local warehouse

git commit -m "Submission description" -a

View the submission history of the warehouse

git log #Most detailed display
git log --pretty=oneline #Display all hash values
git log --online #One line displays one version information
 The above command command displays the version after the current version record

git reflog #Displays the head fallback step size and submission information
 Display all version records, including all log records of submission and fallback

Version fallback and version switching

Version switching based on index value

git reset --hard Index value #The index value can be provided by git reflog

For version switching based on pointer, you can only go back

git reset --hard HEAD[^|~n]

git version switching mode

--soft Move the pointer only in the local library, and the workspace and staging area will not operate.
--mixed The staging area is reset and the workspace is not reset
--hard Both staging and workspace are reset [The most commonly used method]

You can retrieve deleted files by using version switching, provided that the deleted files have been submitted to the local library in the previous version

git comparison file

Compare files in the workspace and staging area

git diff # Compare multiple files
git diff file name #Compare specified files

Compare files in workspace and local warehouse

git diff [Local warehouse pointer]
git diff [Local warehouse pointer] file name

git branch operation

git branch Branch name  #Create branch 
git branch -v  #View branch
git checkout Branch name #Switch branch

git merge branch

Switch branches to merged branches

git merge Branch name [merged branch name]

Resolve branch conflicts

  • Edit branch files and delete special symbols
  • Change the file to a satisfactory level, save and exit
  • Execute git add [file name]
  • Execute git commit -m "submit comments" without adding a specific file name

Remote warehouse alias

  • Add warehouse address
git remote add origin Warehouse address
  • View remote warehouse
git remote -v
  • Delete remote warehouse
git remote remove alias

Push from local to remote warehouse

git push Warehouse alias branch name 

Pull from remote warehouse to local warehouse

  • Directly pull and merge
git pull Warehouse alias branch name
  • Branch conflicts can be resolved step by step
git fetch Warehouse alias branch name
git merge Warehouse alias/Branch name

Common problem solving

git downloads branch code from a remote repository

git clone Warehouse address
git remote add Warehouse alias warehouse address
git branch View branch 

If there are no required branches
git checkout -b Branch name warehouse alias/Branch name
git pull Warehouse alias

Keywords: git github xcode

Added by Jax2 on Wed, 10 Nov 2021 14:52:27 +0200