How many times the below thing happens to you?
hotfix
or patch branch in your workhotfix
branch out-of-sync with master.git rebase
or git merge
?Though,
git rebase
is the keyword everyone uses and your managers will force you to rebase without knowing consequences. Simply,
git rebase
won't work. Will explain you why.git rebase
takes all the commits from your hotfix
branch and applies on the new branch out of master as if that hotfix
commit was never made. It is like as if yourself
hotfix
branch and then applies the changes to the corresponding files manually via a code editor and then you commit them like usual in a new temp
branch cut out of master.hotfix
branch. hotfix
branch and rename the temp
branch as your hotfix
branchgit rebase
automates the above steps with optimisations. You can read about git rebase
in here.One thing you might have noticed that the old
hotfix
branch and new rebased hotfix
branch are not the same, though their contents are. you can confirm them by git log
. You will see the new datetimes and new hashids or commit ids compared to what you saw in the old hotfix
. So, now when you try to push the changes to
hotfix
branch, you will get an error similar to, "Updates were rejected because the tip of your current branch is behind its remote".This is completely understandable since both branches have different ids in their history and no commit in common except for the base commit out of master.
And a normal
git push origin hotfix
cannot force the remote repo to override its hotfix
branch. Most often, Your organization would have disabled
force-push
, which lets u overwrite the hotfix
branch. Only working option u have is
git merge
.git merge
is a very simple command. It adds a merge commit on top of current hotfix
in addition to resolving any conflicts. You can read about it in here. Now, you can easily push the branch because you are pushing only a single commit on top of existing
hotfix
branch without changing old commit ids.So local
hotfix
is same as remote hotfix
but one additional commit in local hotfix
. But both hotfixes
share the same history except for that one additional commit. Thereby, Remote can fast-forward its remote branch. TLDR; don't use
git rebase
, if u have pushed your branch to remote. Always use git merge
.