Configurations
  • git config --list [--system|-global|-local] Show current configuration
  • git config [--system|--global|--local] <key> <value> Set parameters
  • git config -e [--system|--global|-local] Edit configuration
  • git config --global alias.<alias-name> "<git-command>" Create alias
  • git <alias-name> <arguments> Use alias
  • git config --global --unset <key> Remove individual variables
  • git config --global --unset-all <key> Remove all variables
  • Example of setting
    # 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
    
  • Alias usage examples
    # Set aliases
    git config --global alias.co checkout
    # Use aliases
    git co master
    
  • File configuration
    #Local repository file configuration
    <repo>/.git/config
    # system login user global configuration
    ~/.gitconfig
    #System global configuration
    /etc/gitconfig
    
Initialization
  • ssh-keygen -t rsa -C "<email-address>" Generate ssh-key
  • git init [dir] initialize from local
  • git init --bare [dir] Create a local bare repository
  • git clone <git-url> [dir] initialize from a remote repository
  • git clone [--branch tag-name/branch-name] <git-url> [dir] Download and bind the specified branch
  • git clone --bare <git-url> [dir] Clone the remote repository bare
  • git clone --mirror <git-url> [dir] Create a mirror repository
  • git remote set-url <remote-host-name> <new-git-url> Change remote repository address
  • Local repository initialization
    mkdir 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 
    
  • Creating a local bare library
    mkdir example.git
    cd example.git
    git init --bare .
    
  • Push to new remote repository(1)
    git clone --bare [email protected]:trensy/cheatsheet.git
    git remote add origin [email protected]:trensy/cheatsheet_new.git 
    git push --mirror origin
    
  • Push to new remote repository(2)
    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
    
  • Modify remote repository address
    git remote set-url origin [email protected]:trensy/cheatsheet.git
    
Branches
  • git branch -a View local and remote branches
  • git branch View all local branches
  • git branch -r View all remote branches
  • git branch -vv Show local branches associated with remote repositories
  • git checkout -b <branch-name> <existing-branch-name> Create a new branch and switch to the new branch
  • git checkout <commit-id> -b <new-branch-name> Create a new branch from an existing commit and switch to the new branch
  • git branch [-f] <branch-name> Create a new branch, but don’t switch
  • git branch [branch-name] [commit-id] Create a new branch that points to the specified commit
  • git branch <branch-name> <tag-name> Create a new branch based on the tag
  • git 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 branch
  • git checkout <branch-name> switch branches
    # Switch to the previous branch
    git checkout - 
    
  • Delete the remote branch directly (1)
    git branch -d -r branch-name
    #Don't forget to commit the changes to the remote repository
    git push origin :branch-name
    
  • Delete the remote branch directly (2)
    git push origin --delete branch-name 
    
  • Create a branch and associate it with the remote branch
    git checkout -b test
    git push -u origin test
    ...
    #Don't forget to commit your changes to the remote repository
    git push
    
Tags
  • git tag View tag
  • git tag <tag-name> create named tags
  • git tag -a <tag-name> -m <message> Add a comment tag
  • git tag <tag-name> <commit-id> -a -m <message> Create a tag for past commits
  • git push origin <tag-name> Push the tag to the remote repository
  • git push origin --tags Push all tags
  • git pull origin --tags pull tags remotely
  • git tag -d <tag-name> Remove local tags
  • git push origin :refs/tags/<tag-name> Remove remote tags
Viewing Logs
  • git status to see changes
  • git status -s View changes in simple mode
  • git status --ignored show ignored files
  • git log View logs
  • git log --oneline View logs in a single line
  • git log --stat Show detailed commit history
  • git reflog Show the last few commits for the current branch
  • git log <-number> [--pretty] [--oneline] Show past commits
  • git log --follow [file] Show file version history
  • git blame [file] Show who modified the specified file and when
  • git show [--name-only] <commit-id> Show changes to commits
  • git show [commit-id]:[file] Show the contents of a file when a commit was made
  • git log --grep <keyword> filter & search
  • git log --author <author-name> Query for a specific author
  • git 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 workspace
  • git log -p [file] Show every diff associated with a given file
  • git diff --cached [file] Show the difference between the staging area and the previous commit
  • git diff [first-branch] [second-branch] Show the difference between two commits
  • git diff HEAD Show the difference between the workspace and the latest commit of the current branch
  • git shortlog -sn Show all users who have committed, sorted by number of commits
  • git diff --shortstat "@{0 day ago}" Shows how many lines of code you’ve written today
  • git log --graph --decorate --oneline Plot branch graphs
Local Repository Operations
  • git add <file1> <file2> ... Add the specified file to the staging area
  • git add <dir> Add the specified directory to the staging area, including subdirectories
  • git add . Commit all modified and new data staging areas
  • git add -u commits all deleted and modified files to the staging area
  • git add -A commits all deleted, replaced, modified and new files to the staging area
  • git rm <file1> <file2> ... Delete files from the workspace and staging area at the same time
  • git rm --cached <file1> <file2> ... Delete the file from the staging area, but the workspace file still exists
  • git rm -r <dir-name> Delete a folder
  • git mv <file-from> <file-to> rename files and add new files to the staging area
  • git stash Create a new stash
  • git stash save "<stash-name>" Create a new temporary store and name it
  • git stash -u/--include-untracked Create a new temporary store (with untracked changes)
  • git stash list List all temporary stashes
  • git stash show Browse temporary storage contents
  • git stash show -p Browse temporary storage differences
  • git 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 stores
  • git commit -m <message> Commit the stash to the local repository
  • git commit <file1> <file2> ... -m <message> Commit the specified files from the staging area to the local repository
  • git 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 add
  • git commit --amend [-m] [new-message] modify the commit record
  • git commit -v Show all diffs to be added to the local repository
  • Move uncommitted changes from the current branch to another branch
    git stash
    git checkout branch2
    git stash pop
    
Undoing & Rollback
  • 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 workspace
  • git 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 unchanged
  • ``git reset –soft` Reset HEAD to match the last commit; staging and workspace remain unchanged
  • git reset --hard reset HEAD, staging area, workspace as last commit
  • git reset <file-name> reset the specified file in the staging area; the workspace remains unchanged
  • git update-ref -d HEAD which puts all changes back into the workspace and clears all commits, so that the first commit can be recommitted
  • git diff --name-only --diff-filter=U show the list of conflicting files in the workspace
  • git 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 commit
  • git 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 wiped
  • git 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 wiped
  • git revert -n <commit-id> Create a new commit to undo the specified commit, keeping the commits after the target commit
  • git clean [-f|d|x] -n Show the files that will be deleted
  • git clean Delete untracked files from the workspace
  • git clean -d Remove untracked folders from the workspace
  • git clean -f [path] Force deletion
  • git clean -X -f Remove files ignored by .gitignore setting
  • Revert to the most recent commit
    git reset --hard
    git clean -df
    
  • Discard all local changes and go back to the remote repository
    git fetch --all && git reset --hard origin/master
    
  • Roll back the remote code
    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
    
  • Delete a branch that has been merged to master
    git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
    
Merging & Conflicts
  • 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 commit
  • git rebase [branch-name] Merge a branch into the current branch, putting your current branch’s commit at the end of the merged branch
  • git 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 rebase
  • git checkout <branch-name> [--] <file-name> pulls the current file from a different branch
  • git 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 directly
  • git 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 conflicts
  • git cherry-pick <commit-id1> <commit-id2> ... Merge some of the specified commits into the current branch
  • git cherry-pick <branch-name> Move the most recent commits from the selected branch to the current branch
  • One merge operation
    git 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
    
  • Suggestions for using git merge and git rebase
    Use merge for public branches, rebase for personal branches
    local and remote branches are the same, use rebase instead of merge
    
Synchronization
  • 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 branches
  • git merge <remote-host-name>/<remote-branch-name> Merge the remote code into the local repository
  • ``git pull :` pulls the remote branch updates to the local repository and automatically merges
    #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 tags
  • git remote -v show all remote repositories
  • git remote show <remote-host-name> <remote-branch-name> Show information about a remote repository
  • git 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 exist
  • git push <remote-host-name> : refs/for/<remote-branch-name> Delete the specified remote branch1
  • git push <remote-host-name> --delete <remote-branch-name> Delete the specified remote branch 2
  • git 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 host
  • git push If the current branch has only one remote branch, then both host names can be omitted
  • git 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 later
  • git push --all <remote-host-name> pushes all local branches to the remote host, regardless of whether there is a corresponding remote branch
  • git push --force <remote-host-name> force the push to the remote host
  • git push <remote-host-name> --tags Push tags
Subtrees
  • git subtree add --prefix=<sub-repository-path> <sub-git-url> master [--squash] Add a new child repository to the parent repository
  • git subtree pull --prefix=<sub-repository-path> <sub-git-url> master [--squash] pull to update the child repository
  • git subtree push --prefix=<sub-repository-path> <sub-git-url> master Push sub-repository
  • Add a remote repository to a subrepository to make it easier to reuse the subrepository’s git repository address
    git remote add -f <sub-remote-git> <sub-git-url>
    git subtree add --prefix=<sub-repository-path> <sub-remote-git> master
    
Submodules
  • git submodule add <sub-git-url> <sub-repository-path> Adding submodules
  • git submodule foreach git pull [remote-host-name] [remote-branch-name] Update
  • git clone <git-url> --recursive clone the main repository and submodules
  • git submodule init initialize the local configuration file
  • git submodule update check out the commits listed in the parent repository
  • Updating the entire repository
    git pull
    git submodule init && git submodule update // or git submodule update --init --recursive 
    git submodule foreach git pull origin master // When updating code use
    
  • Delete sub-repository (sub-repo)
    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
    
Large File Management (LFS)
  • 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 lfs
  • git lfs track to see the existing file tracking patterns
  • git lfs untrack "*xx.a" unmanage tracking of xx.a by git fls
  • git lfs version View the currently used git lfs version
  • git lfs pull pull large files from remote repositories
  • Install
    # For details see
    https://github.com/git-lfs/git-lfs#downloading
    
Others
  • Installation
    #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
    
  • github Acceleration
    https://fhefh2015.github.io/Fast-GitHub/
    https://ghproxy.com/
    #other 
    vi ~/.gitconfig
    [url "https://ghproxy.com/https://github.com/"]
    insteadOf = https://github.com/