Frequently used Git commands

Summary

Mastering Git involves configuring user details and command aliases, then executing fundamental file operations like staging, committing, reverting, and discarding changes. Essential commands facilitate inspecting differences between files and commits, alongside reviewing comprehensive commit history. Effective branch management is crucial, encompassing the creation, switching, merging, and rebasing of both local and remote branches. Additionally, developers can leverage tools for creating and applying patches, temporarily stashing work, and synchronizing with remote repositories.

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"    # Configure Editor to use textmate
git config -l  #List all configurations

User's git configuration file : ~/.gitconfig

Frequently used Git commands

Check、add、push、delete、find,reset file

git help   # Show help of command
git show            # Show the content pushed one time
git show $id

git co  --    # Drop the modification
git co  .           # Drop the modification

git add       # Add the edited file to the cache
git add .           # Add all the files to the cache

git rm        # Remove file from the version library
git rm  --cached  # Remove file from the cached library, but do not delete the file

git reset     # Reset file
git reset -- .      # 
git reset --hard    # 

git ci 
git ci .
git ci -a           # Combine git add, git rm and git ci
git ci -am "some comments"
git ci --amend      # Amend the last push record

git revert <$id>    # 
git revert HEAD     # 

Check file diff

git diff      # Check the difference between the current file and the cached file
git diff
git diff <$id1> <$id2>   # Check difference between two files
git diff .. # Check difference between two branches
git diff --staged   # 
git diff --cached   # 
git diff --stat     # Just compare statistic difference

Check push records

git log
git log       # Check all the push history of one file
git log -p    # 
git log -p -2       # 
git log --stat      # 

tig

On Mac, you can use tig to substitute diff and log,brew install tig

Git local branch management

Check, switch, create and delete branches

git br -r           # Check remote branch
git br <new_branch> # Create new branch
git br -v           # Check submit information of each branch
git br --merged     # Check the branch which has been merged into the current branch
git br --no-merged  # Check the branch which is not merged into the current branch yet

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

git co $id          # checkout one push history
git co $id -b <new_branch>  # checkout one push history and create a new branch

git br -d   # Delete one branch
git br -D   # Force delete one branch

Merge branch and rebase

git merge                # 
git merge origin/master --no-ff  # 

git rebase master        # 
git co  && git rebase master && git co master && git merge 

Git patch management

git diff > ../sync.patch         # Create patch
git apply ../sync.patch          # Apply patch
git apply --check ../sync.patch  # Test patch status

Git stash

git stash                        # stash
git stash list                   # lish stash
git stash apply                  # recover stash
git stash drop                   # delete stash

Git remote branch management

git pull                         #
git pull --no-ff                 # 
git fetch origin                 # 
git merge origin/master          # 
git co --track origin/branch     # 
git co -b <local_branch> origin/<remote_branch>  # 

git push                         # 
git push origin master           # 
git push -u origin master        # 
git push origin <local_branch>   # 
git push origin <local_branch>:<remote_branch>  # 
git push origin :<remote_branch>  

You can use below commands to trace remote library and local library

git branch --set-upstream master origin/master

git branch --set-upstream develop origin/develop

Source : http://robbinfan.com/blog/34/git-common-command

COMMAND GIT

  RELATED

  COMMENTS

0

No comment for this article.