3

I created a branch about a month ago. The master branch has been heavily updated since then, and the feature branch doesn't include those updates. I want my commits to sit on top of the updated master branch and I only want my commits to be published. How do I do this (or is this even possible)?

More info: My local branch commits have already been pushed to the remote branch, but not the master branch.

I tried git rebase, but then I get a message that says my branches have diverged "and have X and Y different commits each, respectively."

I'm not sure if git push -f will fix my problem. I don't want my remote branch to make note of the changes that have been done on the master branch. I just want my remote/local branch to "sit on top" of the newest version of master and still only show my commits.

I don't want to “merge” master into my branch because then 1000+ commits will overshadow my commits and I want only my commits to be easily reviewed by my peers.

Brian
  • 14,610
  • 7
  • 35
  • 43
esanz91
  • 893
  • 2
  • 12
  • 24
  • 1
    By "new master branch" do you actually mean a new branch or do you just mean that the master branch has updates that the feature branch doesn't? – Brian Sep 03 '15 at 15:30
  • I mean that the master branch has been heavily updated and the feature branch doesn't include. – esanz91 Sep 03 '15 at 15:34
  • I updated the phrasing of your question to reflect that. I don't think there's a good way to hide the changes from the master branch if you bring them into the feature branch, but you should be able to display only your changes in the log. See [How can I view a git log of just one user's commits?](http://stackoverflow.com/questions/4259996/how-can-i-view-a-git-log-of-just-one-users-commits) for ways to do that. – Brian Sep 03 '15 at 15:45

2 Answers2

7

This is what I do to rebase my branch "davesBranch" with Master.

  1. Switch to your local master and type "git pull".
  2. Switch back to "davesBranch" and type "git rebase master".
  3. When it has finished, check for conflicts by typing "git status".
  4. If any files are not merged aka their text color is red, they have conflicts that require you to manually fix them. After fixing them type "git add .".
  5. Then type "git rebase --continue".
  6. Repeat steps 3 - 5 until there are no conflicts. (You'll see something like "Your branch and 'origin/davesBranch' have diverged"). Now you can type "git push origin davesBranch --force".
zafrani
  • 4,030
  • 5
  • 31
  • 39
4

From your description, I'm pretty sure that git rebase and git push -f is what you want. The rebase command replays your changes on top of the updated master branch. This picture (from here) is a good representation of what happens:

git rebase picture

You get the "have X and Y different commits each" message after rebasing because a rebase results in a new series of commits, so the history of your local branch no longer matches the history of your remote branch.

If you haven't shared your feature branch with other people, there are no problems with using git push -f. This will simply rewrite the remote feature branch to match your local feature branch.

If other people have checked out their feature branch, they will need to resync their local working copy, probably by running git fetch followed by git reset --hard your/feature/branch.

larsks
  • 277,717
  • 41
  • 399
  • 399