3

I had the following situation:

-----O
     |
   master
   branch1

then I had to make some changes to master:

 -----O-----O-----O
      |           |
   branch1      master

now I want branch1 to be at the same point of master (and therefore to contain the commits I made):

-----O-----O-----O
                 |
               master
               branch1

I don't know if a merge would be the correct way to achieve this. what steps should I take?

EDIT: also consider that I have uncommitted changes that should be committed on branch1 after branch1 is up to date with master. so I need the current changes to be left untouched to be committed later on branch1

4 Answers 4

3

Since branch1 references a commit that is an ancestor of master, a merge operation won't result in a merge commit; instead, Git will simply move the branch1 reference forward so that it references the same commit as master. This is called a fast-forward merge.

From the documentation:

When you try to merge one commit with a commit that can be reached by following the first commit’s history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together – this is called a “fast-forward.”

So, in your case, you can simply say:

git checkout branch1
git merge master

which will make branch1 point to the same commit as master.

Update: note that you need to have a clean working directory before doing the merge. If you have uncommitted changes in branch1 (i.e. you have a dirty working directory), you should first store them away in the stash by using the git-stash command:

git stash save --include-untracked -m "Work in progress"

Once your changes are saved and your working directory clean, you can proceed with the merge. Afterwards, you can restore the files from the stash by saying:

git stash pop

which will put those files back in the working directory and remove them from the stash. At that point, you can choose to commit them in any numbers of commits.

4
  • I forgot to mention that I have uncommitted changes that should be committed on branch1, but after branch1 is up to date with master. will the method you have proposed leave the current changes untouched to be committed later on branch1? Commented Aug 8, 2016 at 12:03
  • No, but you can stash them before doing the merge. See my updated answer. Commented Aug 8, 2016 at 12:12
  • ok I see, so I checkout branch1, stash the changes, merge branch1 into master and unstash right? Commented Aug 8, 2016 at 12:15
  • Almost – you merge master into branch1; you want to bring branch1 to the same commit as master, so you merge master to branch1. Think about it this way: when using git merge, the branch you're on is the destination (i.e. to branch1) while the branch you specify is the source (i.e. the from master). Commented Aug 8, 2016 at 12:20
0

If your working directory is clean (otherwise do a 'stash'), you are in a special case where 'rebase', 'merge' and 'reset --hard' do exactly what you want to do...

2
  • I forgot to mention that I have uncommitted changes that should be committed on branch1, but after branch1 is up to date with master. will the method you have proposed leave the current changes untouched to be committed later on branch1? Commented Aug 8, 2016 at 12:03
  • In this case, that's a lot better to commit first (that way, if you mess around you will always be to fix and retry), then rebase and if you're not completely satisfied with your commit, amend it or at last 'reset --soft'
    – Philippe
    Commented Aug 8, 2016 at 16:47
0

This looks a job for rebase, so I would do the following:

git commit -m 'your work on branch 1'    # from branch1
git rebase master                        # also from branch1

This will pull in the new commits from master into branch1, and then replays your commit on top of the commits from master.

Functionally speaking, merging master into branch1 should also be OK, but you might not maintain the resolution the two commits, instead ending up with a single merge commit.

4
  • I forgot to mention that I have uncommitted changes that should be committed on branch1, but after branch1 is up to date with master. will the method you have proposed leave the current changes untouched to be committed later on branch1? Commented Aug 8, 2016 at 12:03
  • @JacopoGrassi No issues, just commit your work then do the rebase. Commented Aug 8, 2016 at 12:07
  • oh wait, but this will bring the new commit to be on master too (I guess), but I want them to be only on branch1 Commented Aug 8, 2016 at 12:11
  • No it won't. It will make a commit only on branch1. Commented Aug 8, 2016 at 12:15
0

In this simple case, you can do it the simple way:

git branch -f branch1 master

From git help branch:

git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]

EDIT: to keep uncommitted changes:

git stash
git checkout master
git branch -f branch1 master
git checkout branch1
git stash pop

The uncommitted changes will now again be uncommitted (or unadded, depending on what they were before).

1
  • I forgot to mention that I have uncommitted changes that should be committed on branch1, but after branch1 is up to date with master. will the method you have proposed leave the current changes untouched to be committed later on branch1? Commented Aug 8, 2016 at 12:04

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