hi I have a develop branch based on master branch. I make changes in the develop branch. Now, my question is if the master branch is to be updated, how can I upgrade the develop branch based on the master branch Without losing my changes in develop branch?
-
1Use `git pull` or `git pull --rebase`. If Git won't let you pull, then you'll have to either first stash or commit your changes. – Tim Biegeleisen Jul 23 '19 at 08:36
3 Answers
Depends on what you mean "upgrade", but I would assume you want your develop branch to have the commits master has.
So you need a git merge
or a git rebase
.
# merge master into develop
git checkout develop
git merge master
or
# rebase develop on top of master
git checkout develop
git rebase master
Or if your master is on a server and you want to get the changes from your origin, you can have to do the same things, but with the pull
command.
git checkout develop
git pull #or git pull --rebase
Note that your current working directory must be clean. Otherwise you must commit (or stash) your changes, in order to get things from master.

- 4,116
- 2
- 14
- 24
-
2"# rebase master on top of develop" -> I see it more like "# rebase develop on top of master" – jo_ Jul 23 '19 at 11:21
-
I would advise you to use the rebase solution (see mereg vs rebase ). Assuming you are in your personal development branch (no one else uses this branch)
1st save your current dev (in ) :
git add <all files you created>
# -am is ; a: all added an modified m: message follows
git commit -am "<comment your commit>"
git push origin <my-dev-branch>
From this point you cannot loose anything as there is a copy on the remote
Then update local with remote info
git fetch --all
git rebase master
at this point you might have to deal with a few merge conflict
git mergetool
check this for setting a good merge tool
Once you have solved all merge conflict, and re-tested your code you will push again to server forcing (-f) the branch update
git push -f origin <my-dev-branch>

- 8,324
- 1
- 18
- 14
-
Please be careful with using "push -f", since this will break the branch for all other people using that repository. – creinig Jul 23 '19 at 12:00
-
@jo: I know :) That's why the documentation linked by you states: "Once you understand what rebasing is, the most important thing to learn is when not to do it. The golden rule of git rebase is to never use it on public branches." ([link](https://www.atlassian.com/git/tutorials/merging-vs-rebasing#the-golden-rule-of-rebasing)) – creinig Jul 24 '19 at 06:53
-
1@creinig ok got your point, so I should add something like "assuming your are on a **personal** dev branch , then I call it
– jo_ Jul 24 '19 at 08:10
Go to develop and pull using rebase.
git checkout develop
git pull --rebase origin master

- 3,418
- 11
- 21
- 28

- 314
- 7
- 15