简体   繁体   中英

Git rebase/master changes from branch into master

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 (ie 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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM