Mastering Git Interactive Rebase: Tips for Editing Commits
27 Jan 2023

What is git rebase for?
Git interactive rebase is a way to edit commits in the past. It allows you to choose specific commits to edit, squash, reorder, and delete.
Tips:
-
To start an interactive rebase, use the command
git rebase -i HEAD~[number of commits to go back]. -
You can also use
git rebase -i [commit hash]to specify a specific commit to start the rebase from, instead of usingHEAD~[number of commits]. -
You can use
git rebase -i [branch name]when you are working with branches, this way you can make sure you are only modifying the commits of the branch you want to modify. -
Use
git stashbefore starting an interactive rebase, in case you need to abort the rebase and want to get back to your previous state. -
Since 2015, you can use
fixupto combine the changes of a commit with the previous commit. -
If you're working with a remote repository, it's better to use
git pull --rebaseinstead of git pull to avoid conflicts when you update your local repository. -
Don't stress if you make a mistake during a rebase, you can utilize
git reflogto undo the changes by going back in time ๐ฎ
I also suggest you read the Git Pro book.
The cover image was created by deepai.org based on this text: "Git wizard with a long staff in her hand" ๐ ๐งโโ๏ธ