Git Rebasing: Must-Know Commands Simplified

Alan LinsAlan Lins
3 min read

Using rebasing can make development easier. However, it gets complex when the team decides to keep a single commit per pull request, rebase branches with master, check out branches from remote, and fix conflicts. This article will list a few of the most used git commands that can help during rebasing.

Adding new changes to your existing branch

Once you have committed and pushed your branch, you will raise a Pull Request. After a code review, it is common to be asked to make some changes. If you need to update the code but want to keep a single commit and reuse the same message, you can use these commands:

git add .
git commit --amend --no-edit
git push --force-with-lease

The --amend option will overwrite your existing commit with your changes. The --no-edit option will keep the same message as the previous commit without opening the editor. These options save time because you won't need to squash commits or change the message in editor mode. Since the last commit was overwritten, a regular git push won't work. You must force the push using --force or --force-with-lease.

Rebasing your branch with the master branch

If you just want to sync your current branch with the latest master branch, you can do it with a single command:

git pull origin master -r

This command will rebase your branch with the latest master branch. Now, just push it again:

git push --force-with-lease

Fixing git conflicts

If the command git pull origin master -r doesn't complete because of conflicts, fix the conflicts, mark them as resolved, and then continue the rebase:

# after conflicts got fixed
git add .
git rebase --continue
git push --force-with-lease

Prevent losing approvals on github

Some platforms, like GitHub, have an approval feature for pull requests. This ensures developers can't merge changes into the codebase without a minimum number of approvals. Depending on the setup, it also prevents accepting a PR with an outdated branch. This means developers need to rebase their branches whenever a PR is merged into the master branch. If you rebase your branch with master and push it using the --force option, GitHub will revoke all approvals, requiring developers to seek approvals again. However, if you use the --force-with-lease option, it will keep the approvals, as long as your branch didn't have any conflicts with master.

Checkout modified remote branch

Let's assume there is a branch called featureA on the remote origin. To pull it, you can use the commands:

git fetch
git checkout -b featureA origin/featureA

If someone else rebases the featureA branch with master and you try to pull the latest changes with git pull, it will mix the new and old commits from your local branch. Since the latest commit was overwritten due to rebasing, it will add an extra commit, even if you didn't make any changes yourself. If you want to pull the latest changes and ignore your local changes, you can use these commands:

git checkout featureA
git fetch
git reset --hard origin/featureA

That's it. The local branch featureA is an exact copy of the remote branch.

Conclusion

Using Git rebasing can greatly improve your development workflow. By understanding and using commands like git commit --no-edit --amend, git push --force-with-lease, and git pull origin master -r, you can simplify your commits, resolve conflicts, and keep your branches up-to-date with the master branch.

0
Subscribe to my newsletter

Read articles from Alan Lins directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alan Lins
Alan Lins