4

This may be a duplicate of How to keep a git branch in sync with master (I assume I can just replace Master with the other branch), but it's vital that this works and doesn't merge the wrong way around so I need to make sure.

Scenario

There was a branch called v1, and I created a branch off of that called v1_adminui. I've made around 10 commits to my branch v1_adminui, but a major improvement to another part of the project has been made in v1, so I want to Sync that change with my current branch.

I believe the following would do it:

git checkout v1
git pull
git checkout v1_adminui
git merge v1

Please can you confirm if this is correct? If not, any help would be appreciated on how to accomplish this.

Community
  • 1
  • 1
Gary
  • 395
  • 2
  • 7
  • 24
  • If v1_adminui is a local branch, I would do it by rebasing `v1_adminui` on top of `origin/v1` as proposed in a different answer to the question you linked: http://stackoverflow.com/a/16330782/5085250. – havogt Jul 17 '15 at 09:00
  • `v1_adminui` was created on bitbucket and fetched then checked out. However, I am the only who is using `v1_adminui` in the team. Should I still try to use the rebase solution? I assume the command to use is `git rebase v1_adminui/v1` – Gary Jul 17 '15 at 09:10
  • If your branch is on bitbucket (with all commits!), you can try different approaches without breaking anything. If something goes wrong you can do a `git reset origin/` and try again. – havogt Jul 17 '15 at 09:14
  • The command would be `git checkout v1_adminui` then `git rebase origin/v1`. Then your local `v1_adminui` branch is rebased to the `origin/v1` branch. – havogt Jul 17 '15 at 09:16
  • On the following page [rebase vs merge](https://www.atlassian.com/git/tutorials/merging-vs-rebasing/conceptual-overview) is explained very well. – havogt Jul 17 '15 at 09:19
  • Can't upvote comments so thanks to havogt. For others with this issue please see the accepted answer, it expands on the rebase solution. – Gary Jul 17 '15 at 09:44

1 Answers1

9

Since you are the only one working on this branch, you should use rebase instead of merge.

# Get the base branch
git checkout v1

# Pull in any changes to make sure you have the latest version
git pull

# Check out your branch
git checkout v1_adminui

# Rebase your changes on top of the v1 changes
git rebase v1

# Optionally push your rebased branch
git push origin v1_adminui

You might have to use the --force option when pushing in the last step. Since you're rewriting the branch's history with the rebase, the remote branch will have a different history. Only do this if no-one else is using this branch!

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • I fixed my conflicts but now I see: ``git rebase --continue app/index.html: needs merge app/v1/js/controllers/AdminCtrl.js: needs merge app/v1/js/models/AdminModel.js: needs merge app/v1/views/admin/admin.html: needs merge You must edit all merge conflicts and then mark them as resolved using git add`` – Gary Jul 17 '15 at 09:26
  • Once you fixed the conflicts, do what Git asks you to do: Add all of the changed files with `git add .`. Once that is done, continue the rebase with `git rebase --continue`. – nwinkler Jul 17 '15 at 09:29
  • I see, thank you. Seems like it goes through every commit and resolves conflicts so had to do that a few times. It looks like it's working now will double check everything. Thanks for the help! – Gary Jul 17 '15 at 09:32
  • A commit and push will not update v1 correct? I see: ``git status # On branch v1_adminui # Your branch and 'origin/v1_adminui' have diverged, # and have 15 and 7 different commits each, respectively. # nothing to commit (working directory clean)`` – Gary Jul 17 '15 at 09:33
  • Good - the last message tells you that your remote branch has a different history now. If you want to push, you'll have to use the `--force` flag, as described in my answer. – nwinkler Jul 17 '15 at 09:35
  • Okay that worked, thanks! Small mistake on my part, I had a really old commit (around 3 months old) which was never pushed to master, it was always rejected, and that just got pushed (forced update it said). No idea what was on that commit, I thought it was deleted a while ago, but seems not. Checking commit history I don't see it though so unsure what really happened... `+ cca309a...1018fb6 master -> master (forced update)` – Gary Jul 17 '15 at 09:39
  • Sorry, I have another machine which hosts this code and I want it to host the `v1_adminui` code (it was before too). It wasn't actually changing it, just pulling changes. I didn't think this would be an issue but it looks like it was.... the force seems to have affected it and now it can't pull `v1_adminui`. Any idea how to get around this? Would I need to trash then recreate the project? – Gary Jul 17 '15 at 09:54
  • `git reset --hard origin/v1_adminui` did the job – Gary Jul 17 '15 at 10:23
  • You noticed the effect of the forced push on the other machine, its history was no longer in sync with what you pushed... – nwinkler Jul 17 '15 at 10:33