开发者

git merging changes to local branch

开发者 https://www.devze.com 2022-12-26 19:33 出处:网络
Is it possible to merge changes from a central repo to a local branch without having to commit/stash the edits on the local branch and checkout master?

Is it possible to merge changes from a central repo to a local branch without having to commit/stash the edits on the local branch and checkout master?

If I am working on local branch "work" and there are some unco开发者_StackOverflowmmited changes, I use the following steps to get updates from the central repo into my working branch.

git stash
git checkout master
git pull 
git checkout work
git rebase master
git stash pop

Usually there are no uncommitted changes in "work" and then I omit the stash steps.

What I would really like is something like the following:

git pull master  (updates master while work branch is checked out and has changes)
git rebase master (rebases the updates into work branch uncommited changes are still safe)

Is there something easier than what I currently do?


You can do (on branch work):

git stash
git pull --rebase origin master
git stash apply

git pull --rebase both pulls remote changes and rebases your local changes on top of the remote ones. I.e. essentially does what you show in your script.

Local changes, of course, should be committed or stashed on merge or rebase (because if merge conflict happens, conflicting changes should be uncommitted for you to resolve them--but where would your local changes go then?)

When you later decide to update master and to merge work branch with it, it won't cause problems, since changes pulled are the same. So you don't have to update master when you pull changes.


I don't think you can pull if you have uncommitted changes.

Therefore, you could do something along the lines of:

git stash
git pull --rebase <remote> <branch> # Calls rebase instead of merge.
git stash pop


Maybe things have changed since 2010, but you can pull from a remote even if you have uncommited changes. Your local changes will stay uncommitted, so you can diff or merge them at your leisure.

git pull origin master

Tested with git version 1.9.5.msysgit.1

From this SO answer

0

精彩评论

暂无评论...
验证码 换一张
取 消