Updating git branch to remote despite rebase
When I know that the branch was "flushed" with rebase -i, I found myself doing:
git fetch
git checkout master
git branch -D tracking-branch
git checkout -t origin/tracking branch
Does any git command use this as an option? Note that this is related to my private task branches where I need to check things out on multiple systems.
source to share
Instead of deleting the local branch, you can simply hard reset the remote branch and keep your working directory:
git fetch
git checkout tracking-branch
git reset --keep origin/tracking
If you are completely sure that your local changes should be thrown, do
git reset --hard origin/tracking
source to share
If your tracking branch is already tracking a remote branch, the
following commands should do the trick:
git fetch
git branch -f tracking-branch origin/tracking
Thus:
- You don't have to switch to master and back.
- You avoid
git reset --hard
that can remove some of the work in progress in the working tree. - Avoid expensive disk IOs involved in
git checkout
andgit reset --hard
(completely unnecessary in this scenario)
source to share
If you are sure you want to undo your local history, see the other answers about git reset
and git branch -f
.
An alternative, which might be safer if you're not 100% sure you don't have important local changes (or when you know you have important local commits that you don't want to discard), use this git rebase
:
git checkout tracking-branch
git pull --rebase
Git will automatically notice which patches have already been applied upstream when the patch to apply is identical to the patch introduced by the upstream post. When some of the fixes have been overwritten, you might run into some conflicts, but then you can manually check if the patch should really be skipped and manually skip it with git rebase --skip
.
source to share