banner
阿珏酱

阿珏酱

いつもとは逆の電車に乗り、見たこともない風景を見に行く
twitter
github
facebook
bilibili
zhihu
steam_profiles
youtube

[Note] Complete List of Commonly Used Git Commands

Tips: When you see this prompt, it means that the current article has been migrated from the original emlog blog system. The publication time of the article is too long ago, and the formatting and content may not be complete. Please understand.

[Note] Complete List of Common Git Commands

Date: 2018-6-16 Ajue Code Fiddling: 1796 views Comments: 4

Following the previous post, I took some time to organize a comprehensive list of commonly used Git commands and found a great mind map, which is very high-resolution (1759*3162).
View, add, commit, delete, retrieve, and reset modified files

git help <command> # Display help for command

git show # Display the content of a specific commit git show $id

git co -- <file> # Discard changes in the working directory

git co . # Discard changes in the working directory

git add <file> # Stage modified working files

git add . # Stage all modified working files

git rm <file> # Remove a file from the version control system

git rm <file> --cached # Remove a file from the version control system, but keep the file

git reset <file> # Restore a file from the staging area to the working directory

git reset -- . # Restore files from the staging area to the working directory

git reset --hard # Restore the state of the most recent commit, discarding all changes made since the last commit

git ci <file> git ci . git ci -a # Combine git add, git rm, and git ci operations into one command git ci -am "some comments"

git ci --amend # Modify the last commit record

git revert <$id> # Restore the state of a specific commit, creating a new commit object for the restoration action itself

git revert HEAD # Restore the state of the last commit

View file differences

git diff <file> # Compare the differences between the current file and the staged file git diff

git diff <$id1> <$id2> # Compare the differences between two commits

git diff <branch1>..<branch2> # Compare between two branches

git diff --staged # Compare the differences between the staging area and the version control system

git diff --cached # Compare the differences between the staging area and the version control system

git diff --stat # Compare only the statistical information

View commit history

git log git log <file> # View the commit history of a specific file

git log -p <file> # View the detailed changes of each commit

git log -p -2 # View the detailed changes of the last two commits

git log --stat # View commit statistics

tig

On Mac, you can use tig instead of diff and log, brew install tig

Git local branch management

View, switch, create, and delete branches

git br -r # View remote branches

git br <new_branch> # Create a new branch

git br -v # View the last commit information of each branch

git br --merged # View branches that have been merged into the current branch

git br --no-merged # View branches that have not been merged into the current branch

git co <branch> # Switch to a specific branch

git co -b <new_branch> # Create a new branch and switch to it

git co -b <new_branch> <branch> # Create a new_branch based on branch

git co $id # Checkout a specific historical commit record, without branch information. It will be automatically deleted when switching to another branch

git co $id -b <new_branch> # Checkout a specific historical commit record and create a branch

git br -d <branch> # Delete a branch

git br -D <branch> # Force delete a branch (force deletion is required when deleting an unmerged branch)

Branch merging and rebase

git merge <branch> # Merge the branch into the current branch

git merge origin/master --no-ff # Merge without Fast-Forward, which generates a merge commit

git rebase master <branch> # Rebase master onto branch, equivalent to: git co <branch> &amp;&amp; git rebase master &amp;&amp; git co master &amp;&amp; git merge <branch>

Git patch management (convenient for synchronizing development on multiple machines)

git diff > ../sync.patch # Generate a patch

git apply ../sync.patch # Apply a patch

git apply --check ../sync.patch # Test if the patch can be successfully applied

Git stash management

git stash # Stash changes

git stash list # List all stashes

git stash apply # Restore stashed content

git stash drop # Delete stash

Git remote branch management

git pull # Fetch updates from remote repository and merge into local branch

git pull --no-ff # Fetch updates from remote repository and merge into local branch without fast-forward merge

git fetch origin # Fetch updates from remote repository

git merge origin/master # Merge remote master branch into current local branch

git co --track origin/branch # Track a remote branch and create a corresponding local branch

git co -b <local_branch> origin/<remote_branch> # Create a local branch based on a remote branch, same as above

git push # Push all branches

git push origin master # Push local master branch to remote master branch

git push -u origin master # Push local master branch to remote (create remote master branch if it doesn't exist, used for initializing remote repository)

git push origin <local_branch> # Create a remote branch, origin is the remote repository name

git push origin <local_branch>:<remote_branch> # Create a remote branch

git push origin :<remote_branch> # Delete a local branch (git br -d <branch>) and then push to delete the remote branch

Git remote repository management

GitHub

git remote -v # View remote server address and repository name

git remote show origin # View remote server repository status

git remote add origin git@ github:robbin/robbin_site.git # Add remote repository address

git remote set-url origin git@ github.com:robbin/robbin_site.git # Set remote repository address (used to modify remote repository address) git remote rm <repository> # Delete remote repository

Create a remote repository

git clone --bare robbin_site robbin_site.git # Create a bare version repository from a versioned project

scp -r my_project.git git@ git.csdn.net:~ # Upload the bare repository to the server

mkdir robbin_site.git &amp;&amp; cd robbin_site.git &amp;&amp; git --bare init # Create a bare repository on the server

git remote add origin git@ github.com:robbin/robbin_site.git # Set remote repository address

git push -u origin master # Initial client submission

git push -u origin develop # Initial submission of local develop branch to remote develop branch, and track it

git remote set-head origin master # Set the HEAD of the remote repository to the master branch

You can also use commands to set up tracking between remote and local repositories

git branch --set-upstream master origin/master

git branch --set-upstream develop origin/develop

Friendly reminder: Open the image in a new window for a larger view


image

User Comments:

image Derrick 3 years ago (2018-11-08)
Thank you for sharing.

image Derrick 3 years ago (2018-11-08)
This article is reprinted by the master.

image Yunnan Tea Network 3 years ago (2018-10-04)
The summary is really good.

image mengkun 3 years ago (2018-06-24)
This is great! Bookmarked [#wb_dog9]

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.