HomeBlog
Frequently Used Commands of git - 羡辙杂俎
2017.05.23 12:21:09

Frequently Used Commands of

git

This post is about the git commands that I find hard to remember.

git add

Add Tracked Files Only

$ git add -u

git checkout

Checkout A Remote Branch

$ git fetch
$ git checkout <branch_name>

Discard Unstaged Changes

$ git clean -df
$ git checkout -- .

View on StackOverflow

git commit

Change Author After Commit

$ git commit --amend --author="Wenli Zhang <zwl.sjtu@gmail.com>"

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.

View on StackOverflow

git merge

Undo unpushed merge

$ git merge --abort

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>

git remote

Show Remote URL

$ git remote show origin

Change Remote URL

$ git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git

git reset

Delete Unpublished Commits

$ git reset --hard <hash_code_of_commit>

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

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>

View on StackOverflow

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>
博文对你有帮助吗?如果有的话,要不要送我一本书呢?:heart_eyes:

微信公众号“羡辙部落格”(xianzheblog)提供本站优质非技术类博文推送。

Creative Commons License
Every blog post in this site by Wenli Zhang is licensed under a CC 3.0 License, based on a work at Github. You can see the source code of this blog site at github.com/Ovilia/blog.

Comments

comments powered by Disqus