git config --list [--system|-global|-local]
Show current configurationgit config [--system|--global|--local] <key> <value>
Set parametersgit config -e [--system|--global|-local]
Edit configurationgit config --global alias.<alias-name> "<git-command>"
Create aliasgit <alias-name> <arguments>
Use aliasgit config --global --unset <key>
Remove individual variablesgit config --global --unset-all <key>
Remove all variables# Set the identity
git config --global user.name "cheatsheet"
git config --global user.email "[email protected]"
# Preferred editor
git config --global core.editor vim
# Set up automatic command line coloring
git config --global color.ui auto
# Set aliases
git config --global alias.co checkout
# Use aliases
git co master
#Local repository file configuration
<repo>/.git/config
# system login user global configuration
~/.gitconfig
#System global configuration
/etc/gitconfig
ssh-keygen -t rsa -C "<email-address>"
Generate ssh-keygit init [dir]
initialize from localgit init --bare [dir]
Create a local bare repositorygit clone <git-url> [dir]
initialize from a remote repositorygit clone [--branch tag-name/branch-name] <git-url> [dir]
Download and bind the specified branchgit clone --bare <git-url> [dir]
Clone the remote repository baregit clone --mirror <git-url> [dir]
Create a mirror repositorygit remote set-url <remote-host-name> <new-git-url>
Change remote repository addressmkdir example
cd example
git init
git remote add origin [email protected]:trensy/cheatsheet.git
#pull the remote branch
git pull origin master
#Push data to the remote branch, or create a remote branch with the same name if it doesn't exist
git push -u origin master
mkdir example.git
cd example.git
git init --bare .
git clone --bare [email protected]:trensy/cheatsheet.git
git remote add origin [email protected]:trensy/cheatsheet_new.git
git push --mirror origin
git clone --mirror [email protected]:trensy/cheatsheet.git
git remote update
git remote add origin [email protected]:trensy/cheatsheet_new.git
git push --mirror origin
git remote set-url origin [email protected]:trensy/cheatsheet.git
git branch -a
View local and remote branchesgit branch
View all local branchesgit branch -r
View all remote branchesgit branch -vv
Show local branches associated with remote repositoriesgit checkout -b <branch-name> <existing-branch-name>
Create a new branch and switch to the new branchgit checkout <commit-id> -b <new-branch-name>
Create a new branch from an existing commit and switch to the new branchgit branch [-f] <branch-name>
Create a new branch, but don’t switchgit branch [branch-name] [commit-id]
Create a new branch that points to the specified commitgit branch <branch-name> <tag-name>
Create a new branch based on the taggit branch <-m|-M> <old-branch-name> <new-branch-name>
Rename the branch
#Will not overwrite the same named branch
git branch -m old-branch-name new-branch-name
#Forced rename, will overwrite the same branch
git branch -M old-branch-name new-branch-name
git branch <-d|-D> <branch-name>
Delete the local branch
#Branch will fail to delete if it is not merged
git branch -d branch-name
#Branch will also delete successfully if it is not merged
git branch -D branch-name
git branch <-d|-D> -r <branch-name>
Delete the remote branchgit checkout <branch-name>
switch branches
# Switch to the previous branch
git checkout -
git branch -d -r branch-name
#Don't forget to commit the changes to the remote repository
git push origin :branch-name
git push origin --delete branch-name
git checkout -b test
git push -u origin test
...
#Don't forget to commit your changes to the remote repository
git push
git tag
View taggit tag <tag-name>
create named tagsgit tag -a <tag-name> -m <message>
Add a comment taggit tag <tag-name> <commit-id> -a -m <message>
Create a tag for past commitsgit push origin <tag-name>
Push the tag to the remote repositorygit push origin --tags
Push all tagsgit pull origin --tags
pull tags remotelygit tag -d <tag-name>
Remove local tagsgit push origin :refs/tags/<tag-name>
Remove remote tagsgit status
to see changesgit status -s
View changes in simple modegit status --ignored
show ignored filesgit log
View logsgit log --oneline
View logs in a single linegit log --stat
Show detailed commit historygit reflog
Show the last few commits for the current branchgit log <-number> [--pretty] [--oneline]
Show past commitsgit log --follow [file]
Show file version historygit blame [file]
Show who modified the specified file and whengit show [--name-only] <commit-id>
Show changes to commitsgit show [commit-id]:[file]
Show the contents of a file when a commit was madegit log --grep <keyword>
filter & searchgit log --author <author-name>
Query for a specific authorgit log --after='<date>' --until='<date>'
Find by date
git log --oneline --after="2019-02-20" --until='2020-02-20'
git grep <keyword> [branch-name|tag-name]
search
git grep "Hello"
git grep "Hello" v2.5
git diff
shows the difference between the staging area and the workspacegit log -p [file]
Show every diff associated with a given filegit diff --cached [file]
Show the difference between the staging area and the previous commitgit diff [first-branch] [second-branch]
Show the difference between two commitsgit diff HEAD
Show the difference between the workspace and the latest commit of the current branchgit shortlog -sn
Show all users who have committed, sorted by number of commitsgit diff --shortstat "@{0 day ago}"
Shows how many lines of code you’ve written todaygit log --graph --decorate --oneline
Plot branch graphsgit add <file1> <file2> ...
Add the specified file to the staging areagit add <dir>
Add the specified directory to the staging area, including subdirectoriesgit add .
Commit all modified and new data staging areasgit add -u
commits all deleted and modified files to the staging areagit add -A
commits all deleted, replaced, modified and new files to the staging areagit rm <file1> <file2> ...
Delete files from the workspace and staging area at the same timegit rm --cached <file1> <file2> ...
Delete the file from the staging area, but the workspace file still existsgit rm -r <dir-name>
Delete a foldergit mv <file-from> <file-to>
rename files and add new files to the staging areagit stash
Create a new stashgit stash save "<stash-name>"
Create a new temporary store and name itgit stash -u/--include-untracked
Create a new temporary store (with untracked changes)git stash list
List all temporary stashesgit stash show
Browse temporary storage contentsgit stash show -p
Browse temporary storage differencesgit stash pop
restore the last temporary storage (delete temporary storage)git stash apply
Restore the last temporary storage (keep temporary storage)git stash apply stash@{n}
restores a specific store to the current branch (n = stash list number)git stash <pop|apply> stash@{n}
apply a specific stash (n = stash list number)git stash drop stash@{n}
Delete a specific temporary store (n = stash list number)git stash clear
Delete all temporary storesgit commit -m <message>
Commit the stash to the local repositorygit commit <file1> <file2> ... -m <message>
Commit the specified files from the staging area to the local repositorygit commit -am <message>
You don’t need to run the git add command to commit the specified files in the staging area to the local repository, it only works for modifying and deleting files, new files still need to be git addgit commit --amend [-m] [new-message]
modify the commit recordgit commit -v
Show all diffs to be added to the local repositorygit stash
git checkout branch2
git stash pop
git checkout .
Restore all staging area files to the workspace (used to undo all changes in the workspace)git checkout <file-name>
Restore staging area files to the workspace (used to undo changes made to files in the workspace)git checkout <commit-id> <file-name>
Restore a committed file to the staging area and to the workspacegit reset [--mixed | soft | hard | merge | keep ]
--mixed (default mode) only resets the staging area and points HEAD to the commit, but does not reset the workspace, local file changes are not affected. (fallback to a version, keep source only, fallback commit and index information)
No changes are made to the contents of the --soft workspace, HEAD points to commit and all changes since commit are rolled back to the 'staging area'. (The fallback to a particular version is only the commit information. If you want to commit again, just commit.)
--hard Resets the staging area and workspace so that any changes made in the workspace since commit are discarded and HEAD is pointed to commit. (A complete fallback to a version is made and the local source code is changed to the previous version.)
--keep Keeps the part of the difference between the workspace and HEAD, the command will fail if there is a conflict between the fallback and the retained changes (there are identical files). (Not commonly used)
--merge Preserve the difference between the staging area and the workspace. The command will fail if there is a conflict (with the same file) between the rewind and the retained changes. (not commonly used)
git reset
reset HEAD, keeping the staging area the same as the last commit; but leaving the workspace unchangedgit reset --hard
reset HEAD, staging area, workspace as last commitgit reset <file-name>
reset the specified file in the staging area; the workspace remains unchangedgit update-ref -d HEAD
which puts all changes back into the workspace and clears all commits, so that the first commit can be recommittedgit diff --name-only --diff-filter=U
show the list of conflicting files in the workspacegit reset <commit-id>
reset the pointer to the current branch to the specified commit, and reset the staging area, but leave the workspace unchanged, and erase any commits after the specified commitgit reset --soft <commit-id>
Reset the HEAD of the current branch to the specified commit, with the staging area and workspace unchanged, and any commits after the specified commit will be wipedgit reset --hard <commit-id>
Reset the HEAD of the current branch to the specified commit, and reset the staging area and workspace to match the specified commit, and any commits after the specified commit will be wipedgit revert -n <commit-id>
Create a new commit to undo the specified commit, keeping the commits after the target commitgit clean [-f|d|x] -n
Show the files that will be deletedgit clean
Delete untracked files from the workspacegit clean -d
Remove untracked folders from the workspacegit clean -f [path]
Force deletiongit clean -X -f
Remove files ignored by .gitignore settinggit reset --hard
git clean -df
git fetch --all && git reset --hard origin/master
git log #View branch commit history, identify commits that need to be rolled back
git reset --hard 551c2aa #Roll back commits
git push -f origin master #Push to the remote branch
git push -f origin master
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
git merge [branch-name]
Merge a branch into the current branch, which will merge the merged branch and your current branch’s commit together to form a new commitgit rebase [branch-name]
Merge a branch into the current branch, putting your current branch’s commit at the end of the merged branchgit rebase -i HEAD~[num]
merge the last num commits into one (so that there is only one commit after pushing to the remote repository, avoiding contaminating the remote commit)git pull --rebase
is equivalent to git fetch + git rebasegit checkout <branch-name> [--] <file-name>
pulls the current file from a different branchgit checkout --theirs [file-name]
means check out the other branch, i.e. save the changes from the other branch and discard the changes from the current branch. In case of conflict, use someone else’s code directlygit checkout --ours [file-name]
Check out the current branch, i.e. save the changes in the current branch and discard the changes in the other branch. Use your own code directly in case of conflictsgit cherry-pick <commit-id1> <commit-id2> ...
Merge some of the specified commits into the current branchgit cherry-pick <branch-name>
Move the most recent commits from the selected branch to the current branchgit checkout dev
git rebase master
#or git merge master
#or git cherry-pick 8797a4f
#or git merge master #or git cherry-pick
git add .
#continue to modify the merge
git rebase --continue
#or git merge --continue
#or git cherry-pick --continue
#If you don't want to merge, just terminate
git rebase --abort
#or git merge --abort
#or git cherry-pick --abort
git merge and git rebuild
Use merge for public branches, rebase for personal branches
local and remote branches are the same, use rebase instead of merge
git fetch <remote-host-name> <remote-branch-name>
Update remote code to local repository, no automatic merge, later you can compare the difference between local and online branchesgit merge <remote-host-name>/<remote-branch-name>
Merge the remote code into the local repository#git pull = git fetch + git merge
#It is not recommended to use git pull directly, instead the following command is recommended
git fetch origin master
#compare changes, then merge
git diff HEAD FETCH_HEAD
git merge origin/master
git pull <remote-host-name> --tags
pull remote tagsgit remote -v
show all remote repositoriesgit remote show <remote-host-name> <remote-branch-name>
Show information about a remote repositorygit remote add <remote-host-name> <git-url>
Add a new remote repository and name it git push <remote-host-name> <remote-branch-name>
Push the local branch to a remote branch that it tracks (usually both have the same name), or create a new remote branch if it doesn’t existgit push <remote-host-name> : refs/for/<remote-branch-name>
Delete the specified remote branch1git push <remote-host-name> --delete <remote-branch-name>
Delete the specified remote branch 2git push <remote-host-name>
If the current branch has a tracking relationship with the remote branch, both the local and remote branches can be omitted and the current branch pushed to the corresponding branch on the origin hostgit push
If the current branch has only one remote branch, then both host names can be omittedgit push -u <remote-host-name> <remote-branch-name>
If the current branch has a tracking relationship with multiple hosts, you can specify a default host with the -u argument, so that you can use git push without any arguments latergit push --all <remote-host-name>
pushes all local branches to the remote host, regardless of whether there is a corresponding remote branchgit push --force <remote-host-name>
force the push to the remote hostgit push <remote-host-name> --tags
Push tagsgit subtree add --prefix=<sub-repository-path> <sub-git-url> master [--squash]
Add a new child repository to the parent repositorygit subtree pull --prefix=<sub-repository-path> <sub-git-url> master [--squash]
pull to update the child repositorygit subtree push --prefix=<sub-repository-path> <sub-git-url> master
Push sub-repositorygit remote add -f <sub-remote-git> <sub-git-url>
git subtree add --prefix=<sub-repository-path> <sub-remote-git> master
git submodule add <sub-git-url> <sub-repository-path>
Adding submodulesgit submodule foreach git pull [remote-host-name] [remote-branch-name]
Updategit clone <git-url> --recursive
clone the main repository and submodulesgit submodule init
initialize the local configuration filegit submodule update
check out the commits listed in the parent repositorygit pull
git submodule init && git submodule update // or git submodule update --init --recursive
git submodule foreach git pull origin master // When updating code use
git rm --cached sub-repo
rm -rf sub-repo
# Delete the .gitmodules file [submodule "sub-repo"] related content
# Delete .git/config file [submodule "sub-repo"] Related
git lfs track "<pattern-file-name>"
Keep track of which files need to be processed
# track all files with the a suffix
git lfs track "*.a"
git lfs ls-files
can display a list of files currently tracked by lfsgit lfs track
to see the existing file tracking patternsgit lfs untrack "*xx.a"
unmanage tracking of xx.a by git flsgit lfs version
View the currently used git lfs versiongit lfs pull
pull large files from remote repositories# For details see
https://github.com/git-lfs/git-lfs#downloading
#Details on the official download page
https://git-scm.com/downloads
git archive --format=zip <branch-name> > <file_name>
Packed
git archive --format=zip master > nb.zip
--
escape character
git checkout -- master
# prefixed with '--' means master is a file, not a master branch
git help <command>
View Help
git help -a
git help add
https://fhefh2015.github.io/Fast-GitHub/
https://ghproxy.com/
#other
vi ~/.gitconfig
[url "https://ghproxy.com/https://github.com/"]
insteadOf = https://github.com/