ChatGPT解决这个技术问题 Extra ChatGPT

Get changes from master into branch in Git

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 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.
@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 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.
guys question, does this command do it, git pull origin my_branch_name

P
Peter Mortensen

Check out the aq branch, and rebase from master.

git checkout aq
git rebase master

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.
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.
Sometimes, git merge would be better. If both branches evolved over time, you should consider which is best for you.
Late to the party, but this is a great overview of when to rebase vs merge: atlassian.com/git/tutorials/merging-vs-rebasing/…
If your previous commits on branch aq are public then don't do rebase. atlassian.com/git/tutorials/rewriting-history/git-rebase
C
Chris Kooken

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

git checkout aq
git merge origin/master

If rebase is “better” depends completely on the specific situation.
why don't you just call "git merge master" instead of "git merge origin/master"?
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.
@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.
@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.
P
Peter Mortensen

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.


C
Cameron Clough

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.


h
hestellezg

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.


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.
a
alditis

Merge it with aq

git checkout master
git pull
git checkout aq
git merge --no-ff master
git push

Why no fast forward?
A
Alan Haggai Alavi

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


@Slee you answered yourself... it's not the solution for this situation
s
shreyansh pandey

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.


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!
P
Pete B.

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

A
Alen Lee

Easy way

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

M
MikeBeaton

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.


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.
(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.
D
Dan McNamara

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.


p
prafi

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

This is equivalent to

git checkout aq
git merge master

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/… .)