3

When I know that a branch has been 'neatened up' with rebase -i, I find myself doing:

git fetch
git checkout master
git branch -D tracking-branch
git checkout -t origin/tracking branch

Does some git command include this as an option? Note that this comes up with my entirely private task branches when I need to test things on multiple systems.

4 Answers 4

2

Instead of deleting your local branch, you can simply make a hard reset to the remote branch and preserve 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 away, do

git reset --hard origin/tracking
2
1

If your tracking-branch is already tracking its remote branch,
the following commands should do the trick:

git fetch
git branch -f tracking-branch origin/tracking

That way:

  • You don't have to switch to master and back.
  • You avoid a git reset --hard which could delete some work in progress in the working tree.
  • Avoid the expensive disk-I/O involved in git checkout and git reset --hard
    (completely unnecessary in this scenario)
1
  • the checkout to master in the question is most likely to enable one to delete the current branch... Commented May 4, 2015 at 11:44
1

If you are sure that you want to discard your local history, then see the other answers about git reset and git branch -f.

An alternative, that may be safer if you're not 100% sure that you don't have important local changes (or when you do know that you have important local commits that you don't want to discard) is to use 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 an upstream commit. When some patches have been rewritten, you may get some conflicts but then you can check manually whether the patch is actually to be skipped, and skip it manually with git rebase --skip.

0

Using git reset --hard origin/tracking to force the state of the tracking branch would do the trick without having to remove and recreate the local branch.

Not the answer you're looking for? Browse other questions tagged or ask your own question.