Gain efficiency with git rebase.
Organize the project and improve its readability using simple commands to work efficiently.
Published on 09/24/2024, last updated on 10/05/2024
Operations carried out on git can greatly improve the readability and understanding of a project.
Prerequisites
Make sure you are up to date with the remote repository.
git pull
Fusionner deux branches en utilisant git merge
Merge two branches using git merge.
git checkout master
Merge work from the development branch into the master branch.
git merge dev
Move a branch using git rebase
Place yourself on the branch you wish to move.
git checkout dev
Move the branch to the desired one.
git rebase master
Squashing commits using git rebase
Let's take the example where there is a branch with two commits and I want to keep all the work inside a single commit, that's where we can squash the commits.
git checkout dev
We will specify the number of commits to include in the history.
git rebase --interactive dev HEAD~2
The editor is launched with the details of the current operation.
pick ffb2d36 ✨ My first commit
s 8407ee4 ✨ My second commit
# Rebasage de 06d0e7e..8407ee4 sur 06d0e7e (2 commandes)
#
# Commandes :
# p, pick <commit> = utiliser le commit
# r, reword <commit> = utiliser le commit, mais reformuler son message
# e, edit <commit> = utiliser le commit, mais s'arrêter pour le modifier
# s, squash <commit> = utiliser le commit, mais le fusionner avec le précédent
# f, fixup [-C | -c] <commit> = comme "squash", mais en ne gardant que le message
# du commit précédent, à moins que -C ne soit utilisé, auquel cas, conserver
# ne conserver que le message de ce commit ; -c est identique à -C mais ouvre
# un éditeur
# x, exec <commit> = lancer la commande (reste de la ligne) dans un shell
I specified the expected behavior for my second commit, I prefixed it with s to squash my commits.
Cancel a rebase in progress
It can happen that a rebase goes wrong and in this case, it is better to cancel the action.
git rebase --abort
Plan
Conclusion
Rebasing allows you to keep a more readable history and better visualize the entire project. The GitHub documentation explains the rebase command in detail.
Share this article on social networks with your friends