Common Git Command Line Operation

This article record the specific usage method of some common git command line operation

git clone

  • git clone REPOSITORY_URL Clone repository, and use the name of repository as local folder name

  • git clone REPOSITORY_URL FOLDER Clone repository, and use FOLDER as local folder name

git fetch

  • git fetch origin Update all the remote branch

  • git fetch origin BRACH Update designated remote branch

git pull

  • git pull origin Equivalent to fetch + merge coresponding upstream branch

  • git pull origin BRACH Pull designated branch to current branch

  • git pull origin --rebase master Make local branch rebase remote master branch

git push

  • git push origin Push branch to coresponding remote upstream branch

  • git push origin BRANCH Push branch to remote designated branch

  • git push --set-upstream origin BRANCH Push branch to remote designated branch, and make it as upstream branch (generally need to be used for pushing branch of your own for the first time)

  • git push -f origin Force push branch to corresponding remote upstream branch (will override remote branch, need to be used carefully)

  • git push origin -d BRANCH Delete remote branch

git branch

  • git branch List all local branch

  • git branch -a List all local and remote branch

  • git branch -m NEW_BRANCH Rename current branch

  • git branch -d BRANCH Delete merged branch

  • git branch -D BRANCH Force delete branch (even if not be merged yet)

git checkout

  • git checkout BRANCH Switch to designated branch

  • git checkout -b NEW_BRANCH Create new branch

  • git checkout -b NEW_BRANCH BRANCH Create new branch based on BRANCH

  • git checkout SHA-1 Switch to a commit, or use HEAD~N (N as 1, 2, 3...) to switch to previous Nth commit

  • git checkout SHA-1 /PATH/TO/FILE Restore file to designated commit version

  • git checkout --theirs /PATH/TO/FILE Use theirs' file version in case of conflict

  • git checkout --ours /PATH/TO/FILE Use ours' file version in case of conflict

  • git checkout - Switch to previous branch, suitable for switching frequently between two branches

git add

  • git add . Mark all added / modified / deleted files as to-be-committed

  • git add /PATH/TO/FILE Mark a single file as to-be-committed, suitable for situation when there're other modified files which don't need to be committed

git commit

  • git commit Commit files marked by git add

  • git commit -a Commit modified / deleted files (if there's newly added file, need to use git add to mark firstly)

  • git commit -am "MESSAGE" Commit modified / deleted files and assign comment (suitable for temporary or simple comment content)

  • git commit --amend Update last commit, can add -a or run git add firstly to append updated files

  • git commit --amend --reset-author Default updating commit won't change author, can explicitly config if necessary

git cherry-pick

  • git cherry-pick SHA-1 Apply a commit to current branch

git status

  • git status Check current status

git diff

  • git diff Updating contents of current modified files which has not been marked as to-be-committed

  • git diff --cache Updating contents of current modified files which has been marked as to-be-committed

  • git diff /PATH/TO/FILE Updating contents of designated file, and can also be distinguished with --cache

git log

  • git log Show all logs in detail

  • git log -n 10 Show latest 10 logs

  • git log --oneline Show all logs briefly

  • git log --oneline master ^BRANCH | wc -l Compute how much commit differences between BRANCH and master

git stash

  • git stash Stash modified / deleted files, and the added files marked as to-be-committed

  • git stash -u Stash modified / deleted / added files, which means the added files is included without using git add

  • git stash pop Pop the stashed files

git revert

  • git revert SHA-1 Cancel a commit by forming a new commit

  • git revert SHA-1 -m 1 If the to-be-reverted commit is a merged one, need to designate which parent commit to be reverted to For example, if the merged commit is merging from BRANCH_2 to BRANCH_1, and you want to revert to BRANCH_1, then m should be 1 (it's the most cases)

git reset

  • git reset Cancel marking for to-be-committed files (equivalent to withdrawing git add)

  • git reset --hard Cancel updating for modified / deleted files and added files marked as to-be-committed

  • git reset SHA-1 Cancel all the commits after SHA-1, but retain updates of the committed files If want to just cancel last commit, SHA-1 can be set as HEAD^

  • git reset --hard SHA-1 Cancel all the commits after SHA-1, and don't retain updates of the committed files

git rebase

  • git rebase BRANCH Make current branch rebase BRANCH

  • git rebase -i SHA-1 Update commits after SHA-1, can pick/p, edit/e, drop/d, squash/s corresponding commits If the first commit used with p, and the following commit used with s, then multiple commits will join into a single commit

git merge

  • git merge BRANCH Merge BRANCH into current branch, try not to form merged commit

  • git merge --no-ff BRANCH Merge BRANCH into current branch, and make sure to form merged commit

  • git merge --squash BRANCH Make the differences between BRANCH and the current branch as to-be-committed contents, need to run git commit to complete merging with only one commit

git update-index

  • git update-index --assume-unchanged /PATH/TO/FILE When a file is modified temporary, but don't want to be committed, and not suitable to be added to .gitignore, can use this command to make git don't recognize it as modified Cannot use this command if the file is newly added, but can add the file path to .git/info/exclude

  • git update-index --no-assume-unchanged /PATH/TO/FILE Recover modified recognition for the designated file