本文共 2513 字,大约阅读时间需要 8 分钟。
reference :
Who is not tired of committing a "Remove pdb" or a "Fix a typo" few minutes or hours after committing a clean feature ? A few time ago, I discovered two useful options in GIT that work together : git commit --fixup andgit rebase --autosquash. With these, you can easily merge little fixes with the original feature and keep your branch clean.
Preferably, you won't use it in a stable or master branch, because rebase rewrites history and can create a big mess, mainly if project counts several developers. It rather can be convenient to clean a development branch before merging it in master.
Take a git repos with a branch dev. You intend to commit features A and B:
$ (dev) git add featureA$ (dev) git commit -m "Feature A is done" [dev fb2f677] Feature A is done $ (dev) git add featureB $ (dev) git commit -m "Feature B is done" [dev 733e2ff] Feature B is done
Your work is in progress and you find minor mistakes in Feature A : it's time to use --fixup option !
$ (dev) git add featureA # you've removed a pdb : shameful commit$ (dev) git commit --fixup fb2f677 [dev c5069d5] fixup! Feature A is done
Here, you see that GIT automatically retrieved featureA commit message prefixed by fixup!.
All work is done, let's see the log:
$ (dev) git log --onelinec5069d5 fixup! Feature A is done733e2ff Feature B is donefb2f677 Feature A is done ac5db87 Previous commit
Now, you want to clean your branch before merging it : it's time to use --autosquash option !
$ (dev) git rebase -i --autosquash ac5db87pick fb2f677 Feature A is donefixup c5069d5 fixup! Feature A is donefixup c9e138f fixup! Feature A is done pick 733e2ff Feature B is done
This command has opened your editor with lines above. Just save & quit and ... :
$ (dev) git log --onelineff4de2a Feature B is done5478cee Feature A is doneac5db87 Previous commit
Your shameful commit has been merged properly with the original feature. It's just a shorcut for something you could do otherwise but I find it very convenient :).
That's all folks !
EDIT : git rebase i <after-this-commit> must be launched with as argument the last commit you want to retain as-is, not the first one you want to change.
本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/8034011.html如需转载请自行联系原作者
demoblog