Reprinted from: http://blogread.cn/it/article/6282?f=wb
Git configuration
git config --global user.name "robbin" git config --global user.email "[email protected]" git config --global color.ui true git config --global alias.co checkout git config --global alias.ci commit git config --global alias.st status git config --global alias.br branch git config --global core.editor "mate -w" # Set Editor to use textmate git config -l # list all configurations
User's git configuration file ~/.gitconfig
Git common commands
View, add, submit, delete, retrieve, reset modified files
git help <command> # Display help for command git show # show the content of a commit git show $id git co -- <file> # discard workspace changes git co . # discard workspace changes git add <file> # Commit the working file changes to the local staging area git add . # commit all modified working files to the staging area git rm <file> # remove file from repository git rm <file> --cached # remove file from repository, but not file git reset <file> # restore from staging area to working file git reset -- . # restore from staging area to working file git reset --hard # Restore the state of the last commit, that is, give up all the changes after the last commit git ci <file> go c. git ci -a # Merge operations such as git add, git rm and git ci git ci -am "some comments" git ci --amend # modify the last commit record git revert <$id> # restore the state of a commit, the restore action itself also creates a commit object git revert HEAD # restore the state of the last commit
view file diff
git diff <file> # Compare the difference between the current file and the staging area 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 difference between the staging area and the repository git diff --cached # Compare the difference between the staging area and the repository git diff --stat # only compare statistics
View commit history
git log git log <file> # View the record of each commit of the file git log -p <file> # View the diff of each detailed modification git log -p -2 # View the diff of the last two detailed modifications git log --stat # View commit statistics
tig
You can use tig instead of diff and log on Mac, brew install tig
Git local branch management
View, switch, create and delete branches
git br -r # view remote branch 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 branch git co -b <new_branch> # Create a new branch and switch to it git co -b <new_branch> <branch> # Create a new new_branch based on branch git co $id # Check out a certain historical commit record, but there is no branch information. Switching to another branch will automatically delete it git co $id -b <new_branch> # Checkout a historical commit record and create a branch git br -d <branch> # delete a branch git br -D <branch> # Force delete a branch (requires force when unmerged branches are deleted)
Branch merge and rebase
git merge <branch> # Merge branch to current branch git merge origin/master --no-ff # Don't Fast-Foward merge, this will generate merge commits git rebase master <branch> # Rebase master to branch, equivalent to: git co <branch> && git rebase master && git co master && git merge <branch>
Git patch management (convenient for development and synchronization on multiple machines)
git diff > ../sync.patch # Generate patch git apply ../sync.patch # apply the patch git apply --check ../sync.patch # Test whether the patch is successful
Git staging management
git stash # Staging git stash list # list all stash git stash apply # restore stash content git stash drop # delete the stash area
Git remote branch management
git pull # Grab all the branch updates of the remote warehouse and merge them into the local git pull --no-ff # Grab all branch updates of the remote warehouse and merge them into the local, do not fast-forward merge git fetch origin # fetch remote repository updates git merge origin/master # Merge the remote master branch to the local current branch git co --track origin/branch # Track a remote branch to create a corresponding local branch git co -b <local_branch> origin/<remote_branch> # Create a local branch based on a remote branch, the same function as above git push # push all branches git push origin master # push the local master branch to the remote master branch git push -u origin master # Push the local master branch to the remote (if there is no remote master branch, it will be created to initialize the remote warehouse) git push origin <local_branch> # Create a remote branch, origin is the name of the remote warehouse git push origin <local_branch>:<remote_branch> # Create a remote branch git push origin :<remote_branch> #First delete the local branch (git br -d <branch>), and then push to delete the remote branch
Git remote repository management
git remote -v # View the remote server address and repository name git remote show origin # View remote server warehouse status git remote add origin git@github:robbin/robbin_site.git # Add remote warehouse address git remote set-url origin [email protected]:robbin/robbin_site.git # Set the remote warehouse address (used to modify the remote warehouse address) git remote rm <repository> # delete remote repository
Create a remote repository
git clone --bare robbin_site robbin_site.git # Create a version-only repository with a versioned project scp -r my_project.git [email protected]:~ # Upload the pure repository to the server mkdir robbin_site.git && cd robbin_site.git && git --bare init # Create a pure repository on the server git remote add origin [email protected]:robbin/robbin_site.git # Set the remote warehouse address git push -u origin master # client first commit git push -u origin develop # Commit the local develop branch to the remote develop branch for the first time, and track git remote set-head origin master # Set the HEAD of the remote repository to point to the master branch
You can also command settings to track remote libraries and local libraries
git branch --set-upstream master origin/master git branch --set-upstream develop origin/develop