"git pull" or "git merge" between master and development branches
I have my master
branch and a develop
branch for working on a few changes. I need to merge changes from master
into develop
, but will eventually merge everything from develop
into master
. I have two different workflows in mind:
git pull origin master
intodevelop
branchgit merge master
intodevelop
branch
Which is the best way to do this, and开发者_如何学C why?
This workflow works best for me:
git checkout -b develop
...make some changes...
...notice master has been updated...
...commit changes to develop...
git checkout master
git pull
...bring those changes back into develop...
git checkout develop
git rebase master
...make some more changes...
...commit them to develop...
...merge them into master...
git checkout master
git pull
git merge develop
Be careful with rebase. If you're sharing your develop branch with anybody, rebase can make a mess of things. Rebase is good only for your own local branches.
Rule of thumb, if you've pushed the branch to origin, don't use rebase. Instead, use merge.
The best approach for this sort of thing is probably git rebase
. It allows you to pull changes from master into your development branch, but leave all of your development work "on top of" (later in the commit log) the stuff from master. When your new work is complete, the merge back to master is then very straightforward.
If you are not sharing develop branch with anybody, then I would just rebase it every time master updated, that way you will not have merge commits all over your history once you will merge develop back into master. Workflow in this case would be as follows:
> git clone git://<remote_repo_path>/ <local_repo>
> cd <local_repo>
> git checkout -b develop
....do a lot of work on develop
....do all the commits
> git pull origin master
> git rebase master develop
Above steps will ensure that your develop branch will be always on top of the latest changes from the master branch. Once you are done with develop branch and it's rebased to the latest changes on master you can just merge it back:
> git checkout -b master
> git merge develop
> git branch -d develop
my rule of thumb is:
rebase
for branches with the same name,merge
otherwise.
examples for same names would be master
, origin/master
and otherRemote/master
.
if develop
exists only in the local repository, and it is always based on a recent origin/master
commit, you should call it master
, and work there directly. it simplifies your life, and presents things as they actually are: you are directly developing on the master
branch.
if develop
is shared, it should not be rebased on master
, just merged back into it with --no-ff
. you are developing on develop
. master
and develop
have different names, because we want them to be different things, and stay separate. do not make them same with rebase
.
I prefer to use git pull origin <branch_name>
over to the branch I am looking to merge into.
I infact haven't ever used git merge <branch_name>
Reason, I used to get a bug with it in GitLab when I had started to use it.
But now it is more logical as well... because git pull
does:
(a) git fetch
(b) git merge
2 commands vs one; makes it no brainer application to keep work going fast.
PS: if you aren't sure what might come, it might be better to use the 'broken down' commands there right ;)
精彩评论