记录一些git小知识

push

force-with-lease

可以在rebase后force, 同时避免覆盖他人提交, 具体见 git push --help

git push origin --force-with-lease
This option allows you to say that you expect the history you are updating is what you rebased and want to replace. If the remote ref still points at the commit you specified, you can be sure that no other people did anything to the ref. It is like taking a "lease" on the ref without explicitly locking it, and the remote ref is updated only if the "lease" is still valid.
--force-with-lease alone, without specifying the details, will protect all remote refs that are going to be updated by requiring their current value to be the same as the remote-tracking branch we have for them.

merge

查看分叉点

~/ejoy/battleship(etcd1) » git merge-base --fork-point master etcd1
489889ee3e6ab88f5be217590b9bcfab908227da

rebase

interactive rebase 时显示作者和commit msg

git config --add rebase.instructionFormat "(%an <%ae>) %s" 
git config --global rebase.instructionFormat "(%an <%ae>) %s"

clean

清理本地的untracked files

how-to-remove-local-untracked-files-from-the-current-git-working-tree

# Print out the list of files which will be removed (dry run)
git clean -n
# Delete the files from the repository
git clean -f

conflict

选择自己/他人的修改

choose-git-merge-strategy-for-specific-files-ours-mine-theirs

git checkout --ours -- <paths>
git checkout --theirs -- <paths>

revert

revert ammend

刚刚amend
# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before 
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}

soft会保留提交, head@{1}即ammend之前的提交.

已经ammend许久
git log --reflog
git reset SHA1 --soft

enjolras1205
77 声望9 粉丝