998

In my repository I have a branch called aq which I'm working on.

I then committed new work and bugs in master.

What is the best way to get those commits into the aq branch? Create another new branch out of master and merge it with aq?

4
  • 3
    In the future, you could also start your bugfix branch from a common ancestor of master and other branches that will need the fixes, so that you can merge it into all of those branches, without picking anything else up.
    – Cascabel
    Mar 17, 2011 at 18:11
  • 13
    @Jefromi but that's out of his control if he's not the sole person working on the project. other people update the master. hell, you yourself may update the master from a third branch, and the situation would be unavoidable, and in need of a general solution.
    – ahnbizcad
    Apr 9, 2015 at 16:19
  • @ahnbizcad I'm pretty sure he's in control of where he starts his own branch. If his branch is a common ancestor of the ones he'll want to merge into, and people subsequently add to those branches, it'll still be a common ancestor.
    – Cascabel
    Apr 9, 2015 at 16:20
  • guys question, does this command do it, git pull origin my_branch_name Oct 16, 2017 at 12:09

15 Answers 15

1121

Check out the aq branch, and rebase from master.

git checkout aq
git rebase master
9
  • 2
    can rebase come from any other branch? Ie. git rebase otherbranch? It seems I was a little off in my question, I branched from a branch then made changes to the original branch.
    – Slee
    Mar 17, 2011 at 15:39
  • 4
    If im right, rebase on the pull request it will show all the master commits. if you use merge/origin master all master commits will be shown as 1 commit, which it makes it easier for code review. Jan 30, 2014 at 10:19
  • 5
    Sometimes, git merge would be better. If both branches evolved over time, you should consider which is best for you.
    – erick2red
    May 25, 2015 at 15:48
  • 103
    Late to the party, but this is a great overview of when to rebase vs merge: atlassian.com/git/tutorials/merging-vs-rebasing/… Dec 12, 2015 at 1:06
  • 10
    If your previous commits on branch aq are public then don't do rebase. atlassian.com/git/tutorials/rewriting-history/git-rebase
    – Hanmant
    Jan 12, 2017 at 6:54
433

You should be able to just git merge origin/master when you are on your aq branch.

git checkout aq
git merge origin/master
11
  • 64
    If rebase is “better” depends completely on the specific situation.
    – Bombe
    Mar 17, 2011 at 15:01
  • 19
    why don't you just call "git merge master" instead of "git merge origin/master"? Oct 2, 2013 at 15:56
  • 217
    Use rebase if your branch is local and hasn't been pushed to origin. Use merge if your branch is already pushed. rebase will rewrite history. Apr 24, 2014 at 19:49
  • 22
    @Toskan you can run into issues where your local master isn't up to date with the remote. This way it insures that you are merging in the remote copy of the code. Aug 13, 2015 at 17:55
  • 17
    @garbagecollector I am against rebase (I can, but won't rebase) I see no reason to gamble with rebase. It just makes things needlessly complex. You always have the question "Did I push this to remote?" to ponder and it is a pain to explain to newcomers. Some people say it avoids merge commits. But I want to have merge commits. They are not clutter, they document when branches are merged. So for the last time, can we finally stop acting like we are all committing to master? If you dislike merge commits in log so much, just filter them with --no-merges.
    – nurettin
    Apr 18, 2018 at 6:19
134

First check out to master:

git checkout master

Do all changes, hotfix and commits and push your master.

Go back to your branch, 'aq', and merge master in it:

git checkout aq
git merge master

Your branch will be up-to-date with master. A good and basic example of merge is 3.2 Git Branching - Basic Branching and Merging.

1
  • this is actually very dangerous if you don't know the "state" of your local master in respect of the origin shared across developer's Team and "push" them to orgin. Feb 24 at 15:50
37

There is no guarantee that the master bug fixes are not amongst other commits, hence you can't simply merge. Do

git checkout aq
git cherry-pick commit1
git cherry-pick commit2
git cherry-pick commit3
...

assuming those commits represent the bug fixes.

From now on though, keep bug fixes in a separate branch. You will be able to just

git merge hotfixes

when you want to roll them all into the regular dev branch.

1
  • Thank you, I'll steal your tip, and use from now on. I like this idea of bug fixes in a separate branch. Although, I'd maybe wanna separate out any bug fixes, known to cause more bugs (if possible) 😁
    – Harsh
    Oct 7, 2022 at 4:51
32

This (from here) worked for me:

git checkout aq
git pull origin master
...
git push

Quoting:

git pull origin master fetches and merges the contents of the master branch with your branch and creates a merge commit. If there are any merge conflicts you'll be notified at this stage and you must resolve the merge commits before proceeding. When you are ready to push your local commits, including your new merge commit, to the remote server, run git push.

1
  • 1
    Important to note that this solution is perfect if a merge is required specifically, that is, if the master branch cannot be rebased for some reason.
    – guychouk
    Dec 14, 2019 at 23:55
22

Merge it with aq

git checkout master
git pull
git checkout aq
git merge --no-ff master
git push
1
  • 8
    Why no fast forward?
    – RedYeti
    Jul 6, 2020 at 13:52
21

Scenario :

  • I created a branch from master say branch-1 and pulled it to my local.
  • My Friend created a branch from master say branch-2.
  • He committed some code changes to master.
  • Now I want to take those changes from master branch to my local branch.

Solution

git stash // to save all existing changes in local branch
git checkout master // Switch to master branch from branch-1
git pull // take changes from the master
git checkout branch-1 // switchback to your own branch
git rebase master // merge all the changes and move you git head  forward
git stash apply // reapply all you saved changes 

You can find conflicts on your file after executing "git stash apply". You need to fix it manually and now you are ready to push.

1
  • It's worth noting that if someone else has a branch branched from yours (or changes otherwise linked to your existing history), rebasing will be a nightmare. If your branch is purely local, this will be fine, but be careful rewriting history which is already published! Sep 23, 2021 at 19:05
18

Either cherry-pick the relevant commits into branch aq or merge branch master into branch aq.

1
  • 6
    @Slee you answered yourself... it's not the solution for this situation
    – mtet88
    Jun 30, 2015 at 14:30
9

Easy way

# 1. Create a new remote branch A base on last master
# 2. Checkout A
# 3. Merge aq to A
8

For me, I had changes already in place and I wanted the latest from the base branch. I was unable to do rebase, and cherry-pick would have taken forever, so I did the following:

git fetch origin <base branch name>  
git merge FETCH_HEAD

so in this case:

git fetch origin master  
git merge FETCH_HEAD
7

EDIT:

My answer below documents a way to merge master into aq, where if you view the details of the merge it lists the changes made on aq prior to the merge, not the changes made on master. I've realised that that probably isn't what you want, even if you think it is!

Just:

git checkout aq
git merge master

is fine.

Yes, this simple merge will show that the changes from master were made to aq at that point, not the other way round; but that is okay – since that is what did happen! Later on, when you finally merge your branch into master, that is when a merge will finally show all your changes as made to master (which is exactly what you want, and is the commit where people are going to expect to find that info anyway).

I've checked and the approach below also shows exactly the same changes (all the changes made on aq since the original split between aq and master) as the normal approach above, when you finally merge everything back to master. So I think its only real disadvantage (apart from being over-complex and non-standard... :-/ ) is that if you wind back n recent changes with git reset --hard HEAD~<n> and this goes past the merge, then the version below rolls back down the 'wrong' branch, which you have to fix up by hand (e.g. with git reflog & git reset --hard [sha]).


[So, what I previously thought was that:]

There is a problem with:

git checkout aq
git merge master

because the changes shown in the merge commit (e.g. if you look now or later in Github, Bitbucket or your favourite local git history viewer) are the changes made on master, which may well not be what you want.

On the other hand

git checkout master
git merge aq

shows the changes made in aq, which probably is what you want. (Or, at least, it's often what I want!) But the merge showing the right changes is on the wrong branch!

How to cope?!

The full process, ending up with a merge commit showing the changes made on aq (as per the second merge above), but with the merge affecting the aq branch, is:

git checkout master
git merge aq
git checkout aq
git merge master
git checkout master
git reset --hard HEAD~1
git checkout aq

This: merges aq onto master, fast-forwards that same merge onto aq, undoes it on master, and puts you back on aq again!

I feel like I'm missing something - this seems to be something you'd obviously want, and something that's hard to do.

Also, rebase is NOT equivalent. It loses the timestamps and identity of the commits made on aq, which is also not what I want.

2
  • If you are not ready to merge aq into master yet, this cannot be a solution. Very important spokesperson if others are on your project. It also seems to defeat the purpose of a separate branch. Nov 12, 2021 at 13:28
  • (Spokesperson? Autocorrect typo?) Anyway, this solution is about merging some changes from master into aq, as per the original question, so I'm not sure I understand the problem you're suggesting? Certainly, you could eventually merge aq later to master, after doing either of the above solutions I mention, and after then committing further changes to both - it would cause no issues.
    – MikeBeaton
    Nov 13, 2021 at 16:31
3

You have a couple options. git rebase master aqonto the branch which will keep the commit names, but DO NOT REBASE if this is a remote branch. You can git merge master aq if you don't care about keeping the commit names. If you want to keep the commit names and it is a remote branch git cherry-pick <commit hash> the commits onto your branch.

1

You can also do this by running a single line.
git merge aq master

This is equivalent to

git checkout aq
git merge master
1
  • 1
    This is not doing what you think it is doing. git merge a b merges branches a and b into the current branch. But git merge a when you are on branch a will do nothing (which is why this looks a bit like it's doing what you think it's doing). (See git-scm.com/docs/git-merge#Documentation/… .)
    – MikeBeaton
    Feb 18, 2020 at 9:22
1

Just to distinguish between git merge and git rebase:

Scenario:

  • you made a feature branch
  • you made some commits to it
  • you (or someone else) made changes to main
  • you want the changes in main to be reflected in the feature branch

Switch to the feature branch (git checkout your-feature-branch), then:

  • git merge integrates the two branches by creating a new commit that has two parent commits: one from the main branch and one from the feature branch.

  • git rebase will essentially move or "replay" your feature branch's commits so that they appear after the latest commit on main.

-1
1.git stash - apply this when you have uncommitted changes
2.git checkout master 
3.git pull 
4.git checkout branch1 (branch1 - Your working branch)
5.git rebase master 
6.git stash apply - apply whether you stashed uncommitted changes

You might find merging conflicts after applying stashes. Solve it manually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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