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.

bmargulies
  • 97,814
  • 39
  • 186
  • 310

4 Answers4

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
user1978011
  • 3,419
  • 25
  • 38
  • Isn't the fact that [the `HEAD`, the index and the working copy are modified with this approach](http://stackoverflow.com/a/28150031/319204) a point in the cons column for this method?... – TheCodeArtist May 04 '15 at 11:56
  • That's what I'm asking for. I want the environment to match up to the current state of the tracked branch. – bmargulies May 04 '15 at 21:54
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)
TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
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.

Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
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.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
werz
  • 1