Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 19, 2020 02:19 pm GMT

Git quick tips 1: git commit --fixup

You like clean commit histories. If something goes wrong you use --amend and carry on. Life is good.

The problem

Then it hits you, you forgot something in an older commit. So you have to reach for a heavier gun in the form of git rebase --interactive (or git rebase -i), which offers a handy fixup option:

f, fixup <commit> = like "squash", but discard this commit's log message

Let's see this in action. We are starting from this repository with 3 commits:

$ git log --oneline4e69593 (HEAD -> trunk) Some more work happenedea75734 Some work happened2a030ce First commit

We now notice that we messed up the second commit (ea75734). So we fix the problem and make a new commit:

$ git log --oneline22c85d6 (HEAD -> trunk) Oops4e69593 Some more work happenedea75734 Some work happened2a030ce First commit

Now we have to rebase the last 3 commits with git rebase -i HEAD~3. This will drop us in an editor and list the commits as follows:

pick ea75734 Some work happenedpick 4e69593 Some more work happenedpick 22c85d6 Oops

We can now reorder them and change "pick" to "fixup" (or "f" for short).

pick ea75734 Some work happenedfixup 22c85d6 Oopspick 4e69593 Some more work happened

After saving and exiting the editor, our commit history now is clean (notice the changed commit hashes):

$ git log --oneline321e945 (HEAD -> trunk) Some more work happened1b33eb6 Some work happened2a030ce First commit

The solution

While this works, it's a somewhat cumbersome process. But git offers a convenient alternative in the form of git commit --fixup.

Let's go back to our previous example and change the "Some work happened" commit once again, but this time with a fixup commit:

git commit --fixup=1b33eb6[trunk 3e12b6f] fixup! Some work happened

Not only does this automatically generate a commit message, if we now do the same interactive rebasing operation, the fixup commit will automatically be moved to the right position and have "pick" replaced with "fixup".

pick 1b33eb6 Some work happenedfixup 3e12b6f fixup! Some work happenedpick 321e945 Some more work happened

Original Link: https://dev.to/citizen428/git-quick-tips-1-git-commit-fixup-5df9

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To