Useful Aliases
Refer to dotfiles/git/gitconfig at master · calfzhou/dotfiles.
Delete a Remote Branch
1 | git push origin --delete BRANCH |
List Changed Files (w/o Details)
1 | git diff --name-only BRANCH |
List Commits Gap Between Two Branches
1 | git l --left-right master...HEAD |
Where a > commit is in the right (2nd) branch, while a < commit is in the left (1st) branch.
Diff Two Arbitrary Files
1 | git diff --no-index -- FILE1 FILE2 |
See the Changes Made by a Commit to the Specified File
1 | git show COMMIT-ID FILE |
Scenario: When git l a specific file, usually I’d like to see what changed in a history commit.
1 | $ git l FILE |
I want to see the changes made to this file by commit db217ef84d3 (without looking at the changes to other files):
1 | git show --oneline db217ef84d3 FILE |
Track Whether and How a Commit got into a Branch
To list branches containing the given commit:
1 | git branch --contains COMMIT-ID |
To track it:
1 | gitk COMMIT-ID..HEAD --ancestry-path |
This will show only commits that are descendants of the commit COMMIT-ID and antecessors of the HEAD of current branch (you can use any two commits separated by ..).
github - Git track how a commit got into a branch - Stack Overflow
撤回 Commit
撤回最后一次 Commit
1 | git reset --hard HEAD~1 |
撤回中间的某一次 Commit
比如:
gitGraph: commit id: "A" commit id: "B" commit id: "C" commit id: "D" commit id: "E"
要删掉 commit “C”:
1 | git rebase --onto B C |
The ~ vs. The ^
如果想要 HEAD 的第 10 个祖先,直接用 HEAD~10 就可以。<rev>~<n> 用来表示一个提交的第 n 个祖先提交,如果不指定 n,那么默认为 1。 另外,HEAD~~~ 和 HEAD~3 等价。
<rev>^<n> 用来表示一个提交的第 n 个父提交,如果不指定 n,那么默认为 1。 和 ~ 不同的是,HEAD^^^ 并不等价于 HEAD^3,而是等价与 HEAD^1^1^1。
~ 获取第一个祖先提交,^ 可以获取第一个父提交。 其实第一个祖先提交就是第一个父提交,反之亦然。 因此,当 n 为 1 时,~ 和 ^ 其实是等价的。
Here is an illustration, by Jon Loeliger. Both commit nodes B and C are parents of commit node A. Parent commits are ordered left-to-right.
1 | G H I J |