DEV Community

Cover image for Turn around your Git mistakes in 17 ways

Turn around your Git mistakes in 17 ways

Smitter on October 09, 2022

Mistakes! These are not a necessity in software development. But they always find a way to taint the beauty of software development. Let's make pe...
Collapse
 
fjones profile image
FJones

For 17 I would instead almost always recommend making a new commit on HEAD and then using rebase with the fixup verb to reorder and join the commits. This is for the simple reasons that it:

  • creates a reflog entry
  • lines up with the rebase workflow
  • has some safeguards for timeline-incompatible changes (i.e. file existed at the time but had entirely different contents)

Similarly, I would avoid the restore command, instead using checkout and reset where appropriate.

Collapse
 
netch80 profile image
Valentin Nechayev

creates a reflog entry

Really, a reflog entry appears for each state at begin and end of each rebase, so they at least can be recovered.

has some safeguards for timeline-incompatible changes (i.e. file existed at the time but had entirely different contents)

That's why, an opposite, a file could be easily edited with the edited commit but not later.

Similarly, I would avoid the restore command, instead using checkout and reset where appropriate.

restore is a new command which just unifies multiple use cases for former checkout, branch and reset under a convenient common name. It hasn't added new functionality. To use restore or older variants is still just a matter of taste.

Collapse
 
smitterhane profile image
Smitter

Well I think it is a bit of work having to reorder the commits so as to use the fixup action verb.

Then regarding the restore command, it was built for the restoring purpose in mind unlike the checkout command which is like a know it all command

Collapse
 
lucassperez profile image
Lucas Perez

Just as a side note, if you need to reorder and then fixup, you can instead just squash and then rewrite the commit message appropriately. Squash itself puts you in a commit "screen" anyways, so you can easily use the commit message you want.

Collapse
 
netch80 profile image
Valentin Nechayev • Edited

A good intro, just a few notes:

For commands like restore, it is useful to separate paths from options with '--', e.g. git restore -s <commit_id> -- <path1> <path2>. Shell completion relies on it.

For recipe 3, if not to add -m, this opens editor. In a case of multiline commit message (typical for a complex project) this is much handier.

Recipe 9: it's useful to note exceptions to cleaning, like: git clean -dfx -e .vscode.

For recipe 11: git log works with commit ids as well, so, git log -p <id> may suggest what was the branch to restore. Anyway, well, good messages are useful... but I'm regularly encountering the same commit sequence over different branches.

Collapse
 
smitterhane profile image
Smitter

Good notes👍. For recipe 9, I am curious why would I add the -x and then manually add -e. Looks like a lot of work

Collapse
 
netch80 profile image
Valentin Nechayev • Edited

I am curious why would I add the -x and then manually add -e.

This is the case build artifacts (all of them - object files, libraries, autogenerated sources, etc.) are present in the same directory as the working copy. Normally, they are all in .gitignore. Heavy cleaning from build results require deleting all of them. But, if IDE and other locals aren't a level upper, they are to be saved from deletion.

Collapse
 
jessekphillips profile image
Jesse Phillips

I have some added suggestions to complement what you have.

Use git switch instead of git checkout.

Rather than just dropping changes with a reset, switch into a new branch, commit, then switch out. This ensures if nothing else you'll have a reflog for a bit.

$ git commit --fixup

Or search a commit message instead of hash

$ git commit --fixup :/msg

Collapse
 
robinbastiaan profile image
Robin Bastiaan • Edited

Good overview of the different options at your disposal! Thank you very much for the article.

I just want to add that not all methods will remove your data from the repository. In normal circumstances that is exactly what you want. However, if you accidentally committed a password for example, this is something to look out for. In that situation it can make sence to revert your commit by rewriting history, but this will only make sence if you have not yet commited your code to the remote repo. If that is the case, you are better off considering your password compromised and start changing your password.

Collapse
 
smitterhane profile image
Smitter

Changing your password is a good security precaution at personal level. I agree👍.

Collapse
 
igormihacic profile image
Igor Mihačič

A very very nice beginner friendly list. Tnx!!!

Cheers

Collapse
 
smitterhane profile image
Smitter

Welcome @igormihacic

Collapse
 
anyanka profile image
Anja

Oh that's a nice overview, thanks!

Collapse
 
smitterhane profile image
Smitter

Welcome Anja, I'll be having something for you again in this platform

Collapse
 
eje_sunay profile image
Eje Sunay

Nice article

Collapse
 
colin-williams-dev profile image
colin-williams-dev

This is so helpful for a beginner, thank you!

Collapse
 
smitterhane profile image
Smitter

Thanks! I'm grateful you found this useful

Collapse
 
efecollins profile image
Efosa Collins EVBOWE

This is the first dev post I'm bookmarking. It's so resourceful.

Collapse
 
smitterhane profile image
Smitter

Thanks

Collapse
 
binayakjha profile image
Binayak Jha

Thanks for sharing !

Collapse
 
smitterhane profile image
Smitter

Welcome again on the next one

Collapse
 
ashutoshbisoyi profile image
Ashutosh

Hush! that was something I need to save and refer later.

Collapse
 
devharsh01 profile image
Harsh Anand

That's awesome blog post. Helped me and saved my time.

Collapse
 
smitterhane profile image
Smitter

I am glad i helped you increase your productivity👍

Some comments have been hidden by the post's author - find out more