Chanvin Xiao
Posted on December 31, 2022
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 namegit clone REPOSITORY_URL FOLDER
Clone repository, and use FOLDER as local folder name
git fetch
git fetch origin
Update all the remote branchgit fetch origin BRACH
Update designated remote branch
git pull
git pull origin
Equivalent tofetch
+merge
coresponding upstream branchgit pull origin BRACH
Pull designated branch to current branchgit pull origin --rebase master
Make local branch rebase remote master branch
git push
git push origin
Push branch to coresponding remote upstream branchgit push origin BRANCH
Push branch to remote designated branchgit 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 branchgit branch -a
List all local and remote branchgit branch -m NEW_BRANCH
Rename current branchgit branch -d BRANCH
Delete merged branchgit branch -D BRANCH
Force delete branch (even if not be merged yet)
git checkout
git checkout BRANCH
Switch to designated branchgit checkout -b NEW_BRANCH
Create new branchgit checkout -b NEW_BRANCH BRANCH
Create new branch based on BRANCHgit checkout SHA-1
Switch to a commit, or use HEAD~N (N as 1, 2, 3...) to switch to previous Nth commitgit checkout SHA-1 /PATH/TO/FILE
Restore file to designated commit versiongit checkout --theirs /PATH/TO/FILE
Use theirs' file version in case of conflictgit checkout --ours /PATH/TO/FILE
Use ours' file version in case of conflictgit 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-committedgit 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 bygit add
git commit -a
Commit modified / deleted files (if there's newly added file, need to usegit 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 rungit add
firstly to append updated filesgit 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-committedgit diff --cache
Updating contents of current modified files which has been marked as to-be-committedgit diff /PATH/TO/FILE
Updating contents of designated file, and can also be distinguished with--cache
git log
git log
Show all logs in detailgit log -n 10
Show latest 10 logsgit log --oneline
Show all logs brieflygit 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-committedgit stash -u
Stash modified / deleted / added files, which means the added files is included without usinggit add
git stash pop
Pop the stashed files
git revert
git revert SHA-1
Cancel a commit by forming a new commitgit 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 withdrawinggit add
)git reset --hard
Cancel updating for modified / deleted files and added files marked as to-be-committedgit 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 asHEAD^
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 BRANCHgit rebase -i SHA-1
Update commits after SHA-1, canpick/p
,edit/e
,drop/d
,squash/s
corresponding commits
If the first commit used withp
, and the following commit used withs
, then multiple commits will join into a single commit
git merge
git merge BRANCH
Merge BRANCH into current branch, try not to form merged commitgit merge --no-ff BRANCH
Merge BRANCH into current branch, and make sure to form merged commitgit merge --squash BRANCH
Make the differences between BRANCH and the current branch as to-be-committed contents, need to rungit 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 makegit
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
Posted on December 31, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.