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
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).
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With