Get changes from master into branch in Git
Solution 1
Check out the aq
branch, and rebase from master
.
git checkout aq
git rebase master
Solution 2
You should be able to just git merge origin/master
when you are on your aq branch.
git checkout aq
git merge origin/master
Solution 3
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.
Solution 4
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.
Solution 5
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, rungit push
.
Slee
Updated on July 08, 2022Comments
-
Slee almost 2 years
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 ofmaster
and merge it withaq
? -
Douglas F Shearer about 13 yearsMerge isn't the ideal solution when you want to pull in changes from a parent branch. See the following for why rebase is better: stackoverflow.com/questions/457927/…
-
Bombe about 13 yearsIf rebase is “better” depends completely on the specific situation.
-
Slee about 13 yearscan 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.
-
Adam Dymitruk about 13 yearsorigin master may have commits besides the bug fix commits.
-
Michael Küller over 10 yearswhy don't you just call "git merge master" instead of "git merge origin/master"?
-
Foo Bar User over 10 yearsIf 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.
-
garbagecollector about 10 yearsUse
rebase
if your branch is local and hasn't been pushed toorigin
. Usemerge
if your branch is already pushed.rebase
will rewrite history. -
erick2red almost 9 yearsSometimes,
git merge
would be better. If both branches evolved over time, you should consider which is best for you. -
mtet88 almost 9 years@Slee you answered yourself... it's not the solution for this situation
-
Toskan almost 9 yearswhy don't you just call "git merge master" instead of "git merge origin/master"?
-
Chris Kooken almost 9 years@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.
-
ebuat3989 over 8 yearsLate to the party, but this is a great overview of when to rebase vs merge: atlassian.com/git/tutorials/merging-vs-rebasing/…
-
Hanmant over 7 yearsIf your previous commits on branch aq are public then don't do rebase. atlassian.com/git/tutorials/rewriting-history/git-rebase
-
Magne over 7 yearsNB: This will copy the commits to the
aq
branch, but also still leave the commits onmaster
, which I presume wasn't your intention. So you'd have to rollback master to the commit before the ones you wanted to move, for instance by usinggit stash
thengit reset --hard <SHA of last commit you want to keep>
, and thengit stash pop
-
quasoft almost 7 yearsHere is one more thoughtful article on merging vs rebasing: kristopherwilson.com/2015/02/12/stop-merging-master
-
cameck about 6 yearsMake sure to checkout master, pull latest changes and then change to feature branch. I forgot to do that and kept wondering why my changes weren't applying :P
-
nurettin about 6 years@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.
-
Fabien Haddadi over 5 yearsIn the SVN world, we can specify date intervals. Is that the case in the Git world too? Let's suppose I am only interested in merging changes into my branch from master for the past 24 hours, how would I do this?
-
Luis D Urraca over 4 yearsif private branch and no one else see it, then use rebase, otherwise don't use it -> hackernoon.com/git-merge-vs-rebase-whats-the-diff-76413c117333
-
guychouk over 4 yearsImportant 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.
-
MikeBeaton about 4 yearsThis is not doing what you think it is doing.
git merge a b
merges branchesa
andb
into the current branch. Butgit merge a
when you are on brancha
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/… .) -
RedYeti almost 4 yearsWhy no fast forward?
-
kdperspective almost 4 years@MichaelKüller the only way ti avoid git merge origin/master would be to first do git checkout master and then do a git pull and then do git merge master. That way your local is in sync with remote master.
-
Will Eccles over 2 yearsIt'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!
-
SherylHohman over 2 yearsIf 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.
-
MikeBeaton over 2 years(Spokesperson? Autocorrect typo?) Anyway, this solution is about merging some changes from
master
intoaq
, as per the original question, so I'm not sure I understand the problem you're suggesting? Certainly, you could eventually mergeaq
later tomaster
, after doing either of the above solutions I mention, and after then committing further changes to both - it would cause no issues. -
Carl Walsh over 2 yearsIf you don't
git fetch
this will merge in your local git repo's understanding of whereorigin/master
-- which could be behind what's live on the remote. There's a reason thatpull
runsfetch
before it runmerge
.