ChatGPT解决这个技术问题 Extra ChatGPT

Update Git branches from master

I'm new to Git, and now I'm in this situation:

I have four branches (master, b1, b2, and b3).

After I worked on b1-b3, I realized I have something to change on branch master that should be in all other branches.

I changed what I needed in master and... here is my problem:

How do I update all other branches with master branch code?

Yet another simple task made difficult by Git. The Git devs should use Stack Overflow as feedback in their SDLC loop. 300,000 people should indicate something is seriously wrong with Git's workflow. They need to hire a UX expert because they clearly cannot git it right on their own.
@jww what is sdlc loop?system development life cycle?

C
Chris Kooken

You have two options:

The first is a merge, but this creates an extra commit for the merge.

Checkout each branch:

git checkout b1

Then merge:

git merge origin/master

Then push:

git push origin b1

Alternatively, you can do a rebase:

git fetch
git rebase origin/master

I have a concern about this approach. When I run git log --graph, the graph show the master is actually merged to the topic branch. Will this cause any problem in the long run? I thought the best practice is always merge the topic branch back to master. Please comment.
Look out for this issue if you're going with the merge workflow: randyfay.com/node/89
You are merging master into b1. Why do you got push origin master... doesn't make sense. You're not changing master branch. I think it's a mistake with 119 upvote :/
Do not use the merge method, using git rebase master is the correct answer
For those of us reading later - @Kursion 's concern about the typo was addressed by the author's edit. Also, the second highest upvoted answer below says basically the same thing as this answer but with a diagram of the branch structure and a warning as to why you wouldn't want to rebase.
C
Christian

You have basically two options:

You merge. That is actually quite simple, and a perfectly local operation: git checkout b1 git merge master # repeat for b2 and b3 This leaves the history exactly as it happened: You forked from master, you made changes to all branches, and finally you incorporated the changes from master into all three branches. git can handle this situation really well, it is designed for merges happening in all directions, at the same time. You can trust it be able to get all threads together correctly. It simply does not care whether branch b1 merges master, or master merges b1, the merge commit looks all the same to git. The only difference is, which branch ends up pointing to this merge commit. You rebase. People with an SVN, or similar background find this more intuitive. The commands are analogue to the merge case: git checkout b1 git rebase master # repeat for b2 and b3 People like this approach because it retains a linear history in all branches. However, this linear history is a lie, and you should be aware that it is. Consider this commit graph: A --- B --- C --- D <-- master \ \-- E --- F --- G <-- b1 The merge results in the true history: A --- B --- C --- D <-- master \ \ \-- E --- F --- G +-- H <-- b1 The rebase, however, gives you this history: A --- B --- C --- D <-- master \ \-- E' --- F' --- G' <-- b1 The point is, that the commits E', F', and G' never truly existed, and have likely never been tested. They may not even compile. It is actually quite easy to create nonsensical commits via a rebase, especially when the changes in master are important to the development in b1. The consequence of this may be, that you can't distinguish which of the three commits E, F, and G actually introduced a regression, diminishing the value of git bisect. I am not saying that you shouldn't use git rebase. It has its uses. But whenever you do use it, you need to be aware of the fact that you are lying about history. And you should at least compile test the new commits.


I was merging another source branch (not master) and additional steps to add to this nice answer was to update it on my local repo before merging (to have the latest code locally): git checkout <source branch> git pull. Then continuing with above: git checkout b1 ...
As a long-time SVN user, I prefer the merge option to the rebase: using any version control it is very, very important to keep accurate records of the changes you made and why. I can see the appeal of the rebase for simplifying the apparent history, but you should then go back and add to the commit comments of E', F', G' - and preferably have the rebase automatically added to those comments. Otherwise if the build/test/test-deploy process breaks on G' you have to work out why the changes were made without full information.
The history is a lie
Thanks i use "git merge any-branch-name" to merge one branch code to another branch. Locally i can test code of branch 1 while i am on branch 2
or you could end up with this, from this kind of scenario, a huge mess (just branch first) $ git rebase production First, rewinding head to replay your work on top of it... Applying: ADDED TO ENV AS TEST Using index info to reconstruct a base tree... M Puppetfile Falling back to patching base and 3-way merge... Auto-merging Puppetfile CONFLICT (content): Merge conflict in Puppetfile Failed to merge in the changes. Patch failed at 0001 ADDED TO ENV AS TEST The copy of the patch that failed is found in: /home/user/src/puppet4-controlrepo/.git/rebase-apply/patch
M
Michael J. Gray

git rebase master is the proper way to do this. Merging would mean a commit would be created for the merge, while rebasing would not.


What about when you have already pushed to origin, if you rebase you will be rewriting the commit history and this will conflict with your remote branch. I think rebase should only be used on a pull or when you haven't pushed to a remote branch.
If you are the only one working on the remote branch you can do git push --force origin feature to update your remote branch with rebased local branch. stackoverflow.com/questions/8939977/…
rebase and merge both works, rebase is best for private branches, because it gives a cleaner history graph. this answer is the best
Need to be clearer about the trade-off between clarity (great for single-user or small-team) or messy truth (for multi-contributor code branches - required for maintainability (in my experience - YMMV)).
re "what if you have already pushed?" --> The golden rule of git rebase is to never use it on public branches.
t
the Tin Man

If you've been working on a branch on-and-off, or lots has happened in other branches while you've been working on something, it's best to rebase your branch onto master. This keeps the history tidy and makes things a lot easier to follow.

git checkout master
git pull
git checkout local_branch_name
git rebase master
git push --force # force required if you've already pushed

Notes:

Don't rebase branches that you've collaborated with others on.

You should rebase on the branch to which you will be merging which may not always be master.

There's a chapter on rebasing at http://git-scm.com/book/ch3-6.html, and loads of other resources out there on the web.


Thanks for the simple solution
Tip: git checkout local_branch_name could be git checkout - if the branch you were previouly on was local_branch_name
J
Jens

@cmaster made the best elaborated answer. In brief:

git checkout master #
git pull # update local master from remote master
git checkout <your_branch>
git merge master # solve merge conflicts if you have`

You should not rewrite branch history instead keep them in actual state for future references. While merging to master, it creates one extra commit but that is cheap. Commits does not cost.


Is refreshing a feature branch from master, something that should be done on a regular basis ? Say when the feature branch takes a while to complete, and master has evolved in that time.
Why do we use git merge at the end? Why not git pull, is it redundant?
S
Sundar Gsv

To update other branches like (backup) with your master branch copy. You can do follow either way (rebase or merge)...

Do rebase (there won't be any extra commit made to the backup branch). Merge branches (there will be an extra commit automatically to the backup branch). Note : Rebase is nothing but establishing a new base (a new copy)

git checkout backup git merge master git push

(Repeat for other branches if any like backup2 & etc..,)

git checkout backup git rebase master git push

(Repeat for other branches if any like backup2 & etc..,)


B
Brian Agnew

You can merge, or you can apply individual commits across branches by using git cherry-pick.


D
D_Oghli

to update your branch from the master:

  git checkout master
  git pull
  git checkout your_branch
  git merge master

h
harsha

git checkout master git pull git checkout feature_branch git rebase master git push -f

You need to do a forceful push after rebasing against master


S
Sanjay Bharwani

There are two approaches

You want to merge the master branch into your branch - git checkout master - git pull - git checkout your-feature-branch - git merge master //resolve conflicts if any and commit - git push

2: If you want to rebase your changes on top of main.

 git checkout master #Switch to main branch
 git pull #Take latest
 git checkout your-feature-branch #Switch to story branch
 git pull --ff-only # Ensure branch is up to date
 git rebase -i origin master #Interactively rebase your commits on top of master. So your changes are on top of latest commits in main.
 git rebase --continue #Resolve conflicts and rebase --continue to continue with next commits
 git push -f origin your-feature-branch # As you have rewritten the commit history, you have to **force push** the commits

@Anku I have added inline comments to make it more explanatory.
Hi @Sanjay.. I tried 2nd option but did not work. After running git push..I go to feature-branch using git checkout feature_branch. AND I do not see any changes in feature branch that comes from master branch.
Is there a way without resolving conflict as there are lot of changes has been made in Master and I do not want to manully resolve them.
If there are conflicts they have to be resolved. IF your branch is way out of date from master (not ideal), then I will suggest create another branch from main (so you have latest) and do your changes on top of it. (Some manual efforts)
Thanks @Sanjay, This is what I did: 1. I have cloned Master branch in different folder. 2. Copied (CTL + C) every content from this folder and Paste (CTL + V) to my feature branch folder. Now I have everthing that Master branch had. I hope this is something can be done by git command but anyways. Thanks for your help!
M
ManojP

IN CASE IF YOU WANT TO REVERT TO A LAST COMMIT AND REMOVE THE LOG HISTORY AS WELL

Use below command lets say you want to go to previous commit which has commitID SHA - 71e2e57458bde883a37b332035f784c6653ec509 the you can point to this commit it will not display any log message after this commit and all history will be erased after that.

git push origin +71e2e57458bde883a37b332035f784c6653ec509^:master

M
Martin W

For everyone who finds this thread searching for an easy-to-use and consistent solution to merge your current branch with the latest changes on master:

You can add this to your shell configuration:

alias merge='currentBranch=$(git rev-parse --abbrev-ref HEAD) && git checkout master && git pull && git checkout $currentBranch && git merge master'

This alias works with 5 commands:

currentBranch=$(git rev-parse --abbrev-ref HEAD) # gets your current branch(needed for point 4)
git checkout master # checks out master
git pull # gets latest changes from master
git checkout $currentBranch # checks out the in point 1 saved branch
git merge master # merges your current branch with master

After adding the alias, you can simply use the command “merge” to “update” the branch you are currently working on.


k
kris

There are two options for this problem.

1) git rebase

2) git merge

Only diff with above both in case of merge, will have extra commit in history

1) git checkout branch(b1,b2,b3)

2) git rebase origin/master (In case of conflicts resolve locally by doing git rebase --continue)

3) git push

Alternatively, git merge option is similar fashion

1) git checkout "your_branch"(b1,b2,b3)

2) git merge master

3) git push