I am trying to rebase an entire branch back onto master.
Master has passed my first branch commit by several new commits.
Currently, I use git reset HEAD~<X>
to create 1 commit out of the entire branch, I unstage my branch, then commit it to a separate temporary branch/stash it and then rebase it on top of master. Is there a better way?

- 681
- 3
- 14
-
Are your sure you want to rewrite history of the master branch? Why don't you merge the other branch into master? Why don't you rebase the other branch with master and merge and then merge in back into master? – k0pernikus Apr 25 '19 at 11:01
-
Or to ask differently: Why are you trying to "rebase an entire branch back onto master"? What issue are you having? – k0pernikus Apr 25 '19 at 11:01
-
why not `git rebase master` while being in your branch? – ensc Apr 25 '19 at 11:06
-
I wish to put my changes only on top of the master. – schanti schul Apr 25 '19 at 11:47
-
If something changed on master during my work on the branch (other files), i do not wish to touch those changes at all. – schanti schul Apr 25 '19 at 11:48
1 Answers
You have following options. From your question, I think maybe you want the Merge + squash feature branch into master option, since this will lead to a new commit on master
containing all the changes you made on feature
branch and it seems that is what you want to achieve, but I explain other options too, just in case you prefer another approach.
Merge feature
branch into master
git checkout master
git merge feature
Merge feature
branch into master
after incorporating master
branch changes into feature
branch
git checkout feature
git merge master
# Resolve merge conflicts if needed & git commit
git push
git checkout master
git merge feature
# Conflicts are not possible here,
# since they would have been already resolved in previous merge
git push
Merge + squash feature
branch into master
.
This creates a new commit on master
containing all changes you are incorporating from feature
branch. Disadvantage: feature
branch commits don't appear to be contained in master
branch.
git checkout master
git merge --squash feature
git commit
git push
Rebase entire feature
branch on master
Disadvantages:
- All commits of
feature
branch are rewritten on top of master branch. Therefore commits offeature
branch won't appear as contained inmaster
branch after the rebase. - You eventually will have to resolve merge conflicts more than one time in different commits.
- You loose history and you'll see al the commits of the
feature
branch in one line
So, consider one of the above options.
git co master
git rebase feature
git push
Squash feature
branch and then merge it into master
.
Disadvantage: You need to rewrite remote history if your feature
branch is already pushed to remote. This is usually not advisable. See https://stackoverflow.com/a/55824162/6939011 for more details

- 2,178
- 2
- 15
- 19