This post is about the git commands that I find hard to remember.
git add
Add Tracked Files Only
$ git add -u
Move Last Commits To A New Branch
$ git branch <branch_name> # Create a new branch, saving the desired commits
$ git reset --hard HEAD~3 # Move master back by 3 commits (GONE from master)
$ git checkout <branch_name> # Go to the new branch that still has the desired commits
git checkout
Checkout A Remote Branch
$ git fetch
$ git checkout <branch_name>
Discard Unstaged Changes
Discard changes with new files:
$ git clean -df
<figure class="highlight"><pre><code class="language-sh" data-lang="sh">Discard changes with modified files:</code></pre></figure>
$ git checkout -- .
git clone
$ git clone --single-branch --branch <branchname>
git commit
Change Author After Commit
$ git commit --amend --author="Wenli Zhang <zwl.sjtu@gmail.com>"
Reference GitHub Commit
$ git commit -m 'User/Project@<commit_hash>'
git config
Prevent GitHub git bash from Asking for Password
Change remote to be SSH instead of HTTPS.
$ git config remote.origin.url git@github.com:<the_repository_username>/<your_project>.git
Colorful UI
$ git config --global color.ui auto
Alias
git config --global alias.ck checkout
So that I can use git ck
instead of git checkout
later.
Setup email and name
git config --global user.email <your_email>
git config --global user.name <your_name>
git diff
View Changes After Commit
Before committing, changes can be viewed by git diff
. But if you forget to check it before committing, this is what you can do.
$ git log # to get the last two commit hashes
$ git diff <old-commit-hash> <new-commit-hash>
View Changes of A Commit
To view the change of a specific commit, use
$ git diff --stat <commit-hash> <commit-hash>
View Changed Line Counts
$ git diff <commit-hash>^!
git diff-tree
View Files Changed in A Commit
To list the files changed in a commit, use
$ git diff-tree --no-commit-id --name-only -r <commit-hash>
git log
View Remote Commit Log
$ git log <remote_name>/<branch_name>
E.g., git log origin/master
.
Search in Commit Log
$ git log --all --grep='aria'
git merge
Undo unpushed merge
$ git merge --abort
Merge without New Commit
git merge <branch_name> --no-commit --no-ff
git push
Delete A Remote Branch
$ git push origin --delete <branch_name>
Push To A New Remote Branch
$ git push origin <local_branch_name>:<remote_branch_name>
Delete A Remote Tag
$ git push origin :refs/tags/<tag_name>
git remote
Add Remote
$ git remote add origin https://github.com/USERNAME/OTHERREPOSITORY.git
Show Remote URL
$ git remote show origin
Change Remote URL
$ git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git
git reset
Undo Unpublished Commits
$ git reset --hard <hash_code_of_commit>
Undo Published Commits
$ git reset --hard <hash_code_of_commit>
git push --force
Undo A Commit
After commit, to undo the commit, use:
$ git reset --soft HEAD~
Undo Uncommitted Changes
For tracked files and directories:
$ git reset --hard
For untracked files and directories:
$ git clean -fd
Revert to Last Remote Commit
$ git reset --hard origin/master
git revert
Revert Pushed Commit
$ git revert <hash_code_of_commit>
git show
View Files of Last Commit
$ git show REVISION:<path/to/file>
Or, redirect it to a file.
$ git show REVISION:<path/to/file> > <file_name>
git update
Ignore tracked local file
$ git update-index --assume-unchanged <file>
Corresponding undo operation is as follows.
$ git update-index --no-assume-unchanged <file>