Git rebase or reset hard

I’m trying to fixup my commits or delete them. So, I don’t know if I should use rebase or reset --hard. I tried to use rebase one time and I didn’t know what to do with the untracked files.

1 Like

Both options are equally valid to handle modifications over commits, I often use more rebase than resets when I want to modify or fix a commit, but that is my personal choice since it’s cleaner and faster than a complete git reset. Regarding the untracked files you can either stash them, move them to another temporary folder outside the repo, or delete them to avoid conflicts. so here are my reccomendations:

  • If you choose to reset, try git reset @~ n where n is the number of commits you want to undo, if it’s just the last commit, n would be 1, modify your files and then commit again

  • If you want to rebase, use an interactive rebase: git rebase -i @~ n, where n is the number of commits you want to modify. If you choose this option you must first commit the changes after the commit you want to fix, using either the fixup or the squash option for the interactive rebase you can combine your commits into a single one. It’s a little tricky at the beginning but it becomes quite handy when you learn how to use it.

1 Like