Git global settings:

1
2
git config --global user.name "wanzi"
git config --global user.email "iwz2099@163.com"

Git commit code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
git clone git@github.com:iwz2099/test.git
cd test
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
#The above command pushes the local master branch to the remote origin branch. At the same time, -u specifies the default remote branch name of the current warehouse as origin. You can use git push without any parameters later

#Push all local branches to the origin host
git push -u origin --all

#If the local development is based on the case_dev_wanzi branch, push to the remote case_dev branch
git push origin case_dev_wanzi:case_dev

Git query and cleanup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
git status #Query the current branch status information
git log #View the current branch commit information
git log -n3 #Query the last three commits
git log -p -2 #Query the content difference of each commit, only display the last two commit records,
git log --stat #Query the resume statistics of each commit
git log --pretty=oneline #Display the information of each commit in one line, oneline can be short, full, fuller
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph #ASCII string to visualize your branch and merge history
git reflog #Display all branch operation information (including commit, reset and deleted commit)
git reflog #Display the last 10 logs
git grep -n wanzi #Search the commit history and working directory
git grep --count wanzi #Search display times
git clean #Remove untracked files that are not ignored
git clean -d #Clean up the working directory
git clean -d -n #-n tests the cleanup of the working directory, but it is not actually cleaned up

Git branch operation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
git branch #View the current branch
git branch -v #View the last commit of each branch
#Create a local dev branch based on the remote master branch
git checkout -b dev origin/master

git branch --merged #View which branches are merged into the current branch
git branch --no-merged #View which branches have not yet been merged into the current branch

#Develop a new feature, -b create a branch, and switch to the newly created branch
git checkout -b dev-20180720-111111-wanzi
Equivalent to:
git branch r-20180720-111111-wanzi
git checkout r-20180720-111111-wanzi

Merge the post-development feature into the Master trunk
git checkout master
git merge dev-20180720-111111-wanzi

Git merge operation

Create a new branch issue54 based on the remote master, and develop the project based on this branch

1
2
git remote add origin git@github.com:iwz2099/test.git
git checkout -b issue54 origin/master

After the function is developed, merge the developed function into the remote branch

1
2
3
4
5
git fetch origin #Pull away the difference information
git merge origin/master #Merge the remote branch into the current project
git push origin master #Push code to the remote branch

git log --no-merges issue54..origin/master #Check which information in master is not merged into the issue54 branch

Git tag operation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
git tag #List the tag information of the current project
git tag -l 'wanzi-2018*' #-l can also list only your own branches
git tag v1.8-20180720 #Create a tag (usually temporary)
git tag -a v1.8-20180720 -m 'wanzi version 1.8' #-a creates a tag, -m adds a description, and the tag information will be stored
git show v1.8-20180720 #View the tag information. At this time, you can compare the difference between git tag -a and git tag
git push origin tag_name #Push tags to remote warehouses
git push origin --tags #Push many tags to remote warehouses at one time
git push origin :tag_name #Delete remote tag information
git checkout -b dev-20180720-111111-wanzi v1.2.0 #Create a new tag version based on a branch
git branch -D dev-20180720-123333-wanzi #Forced deletion of an unmerged branch

Git multi-person collaboration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#A coder creates a new branch feature1 based on the remote master
git checkout -b feature1 origin/master

#B coder creates a new branch feature11 based on the remote master
git checkout -b feature11 origin/master

#A coder finishes development first and pushes the code to the feature1 branch of the remote service. Coder B finishes development later and wants to merge the modified information into feature1
git fetch origin
git merge origin/feature1
git push -u origin feature11:feature1

Git cancels files in the temporary storage area and cancels file modifications

1
2
3
4
5
6
7
#Git add the code to the cache area, but does not commit it, roll back the code
git reset HEAD . or
git reset HEAD a.txt
This command only changes the staging area, not the workspace, which means that without any other operations, the actual files in the workspace remain unchanged from before the command was run

git checkout -- . #Cancel all changes to the path and return to the last commit version
git checkout -- a.txt #Cancel all changes to a.txt and return to the last commit version

Git rollback to the last commit version

1
2
3
4
5
6
7
git log
git reset --hard <commit_id> #Rollback to the commit id of a commit
git reset --hard HEAD^ #Rollback to the previous version
git reset --hard HEAD^^ #Rollback to the second to last version
git reset --hard HEAD~4 #Rollback to the fourth to last version

git push origin HEAD --force #Force a commit, and the previous wrong commit will be deleted from the remote repository (be careful when you have project management permissions)

Git rollback master code to a tag version

  • Based on release tag creates a temporary rollback branch and pushes the branch to the remote master
1
git checkout -b rollback_20180720 r-20180720-111225-wanzi
  • In the gitlab project setting, the master project administrator cancels the master protection

  • Force push the rollback_20180720 branch to the remote master trunk (this process will force rollback to the r-20180720-111225-wanzi version)

1
git push origin rollback_20180720:master -f

Other Git operations

  • The commit information is wrong and has not been submitted yet. Modify the commit information
1
git commit --amend --only -m 'fix: fix a bug' #Modify the commit information
  • The commit username and email address are wrong and have not been submitted yet. Modify the commit information
1
git commit --amend --author="wanzi <iwz2099@163.com>"