Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase/master changes from branch into master

Tags:

git

git-rebase

Two weeks ago, I created a new branch, let's call it exp. During this time there have been several commits both in exp and in master. During this time exp has not been updated with the changes from master

Now I want to get all the changes from exp into master, without losing my whole history (i.e. I don't want to just have one commit in master with all the changes, but rather that all my commits in exp appear in the history of master)

How is this possible? I've looked into Rebase but when I do:

git checkout exp
git rebase master

At the end of the process I am in exp and when I git status I see all the commits I have made in exp as if I wanted to push them into exp again

like image 861
user3083022 Avatar asked Jun 24 '16 10:06

user3083022


People also ask

How do I rebase changes to master?

To rebase, make sure you have all the commits you want in the rebase in your master branch. Check out the branch you want to rebase and type git rebase master (where master is the branch you want to rebase on).

How do I convert changes from branch to master?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch.

Does rebase affect master?

git rebase origin/master will merge in the requested branch ( origin/master in this case) and apply the commits that you have made locally to the top of the history without creating a merge commit (assuming there were no conflicts).


2 Answers

Your goal is to have all the commits in exp in master's history and retain all of your commits. You can achieve this regardless of whether you use git merge or git rebase.

MERGE:

git checkout master
git pull
git checkout exp
git merge master
git push -u origin exp

Make to perform step 2 so that you pull the latest master and have the latest changes.

REBASE:

git checkout exp
git pull --rebase origin master
git push -u origin exp

If you do not want to update your local master in the process, you can alternatively perform the following steps:

git checkout master
git pull
git checkout exp
git rebase master
git push -u origin exp

Note that when you git pull --rebase origin master, you are basing your changes on the latest version of origin's master. Also, this command does not update your local copy of master.

NOTE: You might need to force push (git push -f origin exp) if you are using git rebase.

like image 96
progfan Avatar answered Oct 18 '22 07:10

progfan


I would suggest simply do merge your exp branch to your master. You would still have all your history and as a bonus you would keep all your original commit times.

git checkout master
git merge exp

If you really want to go with the rebase option, then you just need to merge your rebased exp branch to master.

git checkout exp
git rebase master

git checkout master
git merge exp

NOTE: With the merge (first) option, you would end up with a no-fast-forward merge (additional commit for your merge) because your branches probably have diverged. If you are GitHub's pull request user, it uses this kind of merges. If you want to do the same with your second (rebase) option, you can add --no-ff option to your merge command.

git merge exp --no-ff
like image 33
Uzbekjon Avatar answered Oct 18 '22 08:10

Uzbekjon