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
?
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
?
Check out the aq
branch, and rebase from master
.
git checkout aq
git rebase master
You should be able to just git merge origin/master
when you are on your aq branch.
git checkout aq
git merge origin/master
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.
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.
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
.
Merge it with aq
git checkout master
git pull
git checkout aq
git merge --no-ff master
git push
Scenario :
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.
Either cherry-pick
the relevant commits into branch aq
or merge branch master
into branch aq
.
Easy way
# 1. Create a new remote branch A base on last master
# 2. Checkout A
# 3. Merge aq to A
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
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.
You have a couple options. git rebase master aq
onto 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.
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.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