HomeBlog
Frequently Used Commands of git - 羡辙杂俎
2019.01.18 16:09:44

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

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

View on StackOverflow

git checkout

Checkout A Remote Branch

$ git fetch
$ git checkout <branch_name>

Discard Unstaged Changes

$ git clean -df
$ git checkout -- .

View on StackOverflow

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.

View on StackOverflow

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>

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>

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