GitTutorial command cheat sheet


The foreword 0.

  • In this paper,99%Summarized from Liao XuefengGitThe tutorial.
  • otherwise1%From personal experience.

1. Create a repository

The command use
mkdir learngit

cd learngit
Create and go to an empty directory
git init Initialize theGitwarehouse
Git add 1... Add files
git add . Hold all changed files
Git commit -m "Description" To submit documents

2. Time travel

The command use
git status View the current warehouse status
Git diff filename View changes between the workspace and the staging area

If the staging area is empty, view the changes made between the workspace and the repository’s last commit
git log Display commit Log
git log --pretty=oneline The commit log is displayed in a single line
git reset --hard HEAD^ Return to the previous version
git reset --hard HEAD^^ Return to the first two versions
git reset --hard HEAD~100 Returns the first 100 versions
Git reset --hard commit number Specifies to return to a version number
git reflog Viewing Command History
Git diff HEAD -- file name View the differences between the workspace and the latest version of the repository
Git checkout -- file name Discard the workspace changes and go back to the last onegit add/commitWhen the state of the
Git reset HEAD Name Roll back the staging changes to the workspace
The rm file name

Git rm/add filename

Git commit -m "delete files"
Delete files from the repository
The rm file name

Git checkout -- file name
Recover an mistakenly deleted file from the text library

3. Remote warehouse

The command use
ssh-keygen -t rsa -C "[email protected]" createSSHThe secret key to
Git remote add origin [email protected]: your user name/repository name Associating remote libraries
Git remote add origin https://github.com/ User name/repository name Associating remote libraries
git push -u origin master First pushmasterBranch all contents and associate them with the remote librarymasterbranch
git push origin master pushmasterThe latest modifications to the branch
Git clone [email protected]: user name/repository name. Git Clone remote library to local (no.gitAll right)
Git clone https://github.com/ Username/repository name. git Clone the remote library to the local

4. Branch management

The command use
git branch See the branch
Git Branch Name Create a branch
Git Checkout branch name Switch branch
Git checkout -b Branch name Create and switch branches
Git merge branch name Merge the branch name into the current branch
Git branch -d Specifies the name of a branch Delete the branch
git log --graph --pretty=oneline -abbrev-commit View branch merges
Git merge -- no-f-m "Commit note" branch name Merge branches in non-fast forward mode
git stash Storage area
git stash list View the list of workspace stores
git stash apply

git stash drop
Restore work area

Delete stored content
Git stash apply stash@{number}

Git stash drop Stash @{number}
Restores the workspace from the specified store and deletes the stored content
git stash pop Restore the stored content and delete it simultaneously
Git branch -d Specifies the name of a branch Deletes unmerged branches
git remote View information about remote libraries
git remote -v View details about remote libraries
Git push Origin branch name Push the branch
Git checkout -b branch name origin/ branch name Create a branch locally
Git branch -- set-ups-to =origin Specifies the link between the local branch and the remote branch
git pull Grab the latest remote commit
git rebase Change the base, put the local notpushThe forked submit history is sorted into straight lines

5. Label management

The command use
Git Tag Name of a tag Tag the latest submission for the current branch
Git Tag Tag name commit number Labels the specified submission number
git tag View all tag names
Git show tag name Viewing Label Details
Git tag -a -m "git tag description" Create a label with instructions
Git push origin tag name Push local Labels
git push origin --tags Push all unpushed local labels
Git tag -d Specifies the tag name Deleting a Local Label
Git tag -d Specifies the tag name

Git push Origin :refs/tags/ tags
Deleting a Remote Label

6. Use code clouds

The command use
git remote rm origin deleteoriginThe remote repository
Git remote add github [email protected]: user name/repository name addGitHubThe remote repository
Git remote add gitee [email protected]: user name/repository name. git addGiteeThe remote repository
git push github master Pushed to theGitHub
git push gitee master Pushed to theGitee

7. The customGit

The command use
git config --global color.ui true letGitAccording to the color
Git add -f File name Forcing file addition
Git check-ignore -v File name View the line on which the file is.ignoreignore
git config --global alias.st status withstInstead ofstatus
git config --global alias.co checkout withcoInstead ofcheckout
git config --global alias.ci commit withciInstead ofcommit
git config --global alias.br branch withbrInstead ofbranch
git config --global alias.unstage 'reset HEAD' Undo temporary area modificationGit unstage File nameCan be
git config --global alias.last 'log -1' withgit lastDisplays the last commit
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" withgit lgDisplay commit Log
Under the current warehouse.git/configfile The configuration file for the current repository
In the user’s home directory.gitconfigfile Configuration file of the current user

Commonly used 8.GitCommand quick lookup table


2018.08.16 First revision 2019.01.17 Second revision 2019.03.06 Third revision: Added quick reference table of common Git commands