DEV Community

Don't Amend, Fix

Tamir Bahar on March 10, 2017

As git users, we know that we should "commit early, commit often." While this is a wonderful thing to do, it does mean that from time to time we ma...
Collapse
 
freeatnet profile image
Arseniy ✌️ Ivanov

Here are a few shortcuts in the same vein:
fixup with the default of HEAD:
fu = !bash -c 'git commit --fixup ${1:-HEAD}' -
Find the point of divergence of the current branch from another branch (default master), useful for the following trick:
diverges = !bash -c 'diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | head -1' -
Finally, rebase with autosquash from the point of divergence from another branch (so that you don't have to count the commits):

ar = !bash -c 'git rebase --autosquash -i `git diverges ${1:-master}`'

Collapse
 
tmr232 profile image
Tamir Bahar

This is great, thanks!

Collapse
 
dmerejkowsky profile image
Dimitri Merejkowsky

Nice article!

Note that you can also use git config --global rebase.autosquash=true so that you do not need to add the --autosquash flag to all your git rebase commands.

Collapse
 
tmr232 profile image
Tamir Bahar

Thanks!

That's true. Just make sure to add --no-autosquash to the git mri alias to get the same behavior.

Collapse
 
supremebeing7 profile image
Mark J. Lehman

Cool tip!

Just to clarify, it's git config --global rebase.autosquash true; having the = throws an invalid key error.

Collapse
 
nipafx profile image
Nicolai Parlog

I don't quite get why I wouldn't use the much simpler git amend (alias for git commit --amend) for the simple case of adding to the last commit and only do a rebase (remember no unstaged changes for that, may need to stash!) if I really need it.

Collapse
 
tmr232 profile image
Tamir Bahar

That's a matter of personal preference.
I like having the history of minor changes available. I usually only rebase before a git push, and by that time, I try not to have any uncommited changes in any case.
I feel that the little extra typing (which can be saved using @freeatnet tip here) is worth it for the extra history and for editing previous commits.

Collapse
 
thepracticaldev profile image
dev.to staff

Wow, this is great. I am going to immediately modify my workflow in this direction.

Collapse
 
ben profile image
Ben Halpern

Woops, commented from the staff account. ¯_(ツ)_/¯

Collapse
 
engineercoding profile image
Wesley Ameling

Basically a fix commit here, as you probably could've changed it in the database ;)

Collapse
 
l_giraudel profile image
Loïc Giraudel

Yes, yes and a thousand times yes!
Developers need to learn to commit often and create small commits for their own safe and their mates.

So huge thank you for spreading the word :)

I also wrote an article on this subject few weeks ago: adopteungit.fr/en/methodology/2017...

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
geovanisouza92 profile image
λ • Geovani de Souza

Another alternative is git rebase -i and marking the "Added First Module" with "edit". Then make the change, add, commit and rebase --continue

Collapse
 
lunks profile image
Pedro Nascimento

Also a nice tip that usually goes along with --fixup is to find the commit with :/ i.e. git commit --fixup :/First will find the last commit with First in its message.

Collapse
 
bgadrian profile image
Adrian B.G.

Cool, haven't used fixup yet.

Just saying if you hate things because they hide history you should fight against rebase too

Collapse
 
tmr232 profile image
Tamir Bahar

It's the hiding history while I work part that bothers me.

Collapse
 
dimpiax profile image
Dmytro Pylypenko

Thanks for the article, useful information.
But amend is not evil if you use it in right purposes.
ps: never used amend it fix the history, only for append changes on dev commit.