문제

I have three branches: master, task_one, and task_two. I want to pull all updates made by other people, and apply these updates to all of my work.

What are the correct commands?

도움이 되었습니까?

해결책

There is no good way to "just update everything" automatically, because of the potential for merge conflicts. Because this implies that there is no good way to simply "always be up-to-date", the next question to answer is: why do you require the updates?

If you require them for code review, then

git remote update

followed by

git log refs/remotes/<remote-name>/<branch-name>

will let you examine the commits which others have sent.

If you want to integrate them with changes which you have made, (you mention rebasing), then you only need to get changes before actually doing work on the branch. So, once you have checked out the branch you want to work on (and assuming you are ready to integrate changes at the moment- don't integrate for no reason! integrate when you're ready to!)

git pull --rebase <remote-name> <remote-branch-name>

will fetch <branch-name> from <remote-name>, and then rebase against it. It is actually a shortcut for (and is identical to) running:

git fetch <remote-name> <remote-branch-name> &&
git rebase <remote-branch-name>

You can also set the default upstream remote / branch for any given branch, which would allow you to simply type

git pull --rebase

in the future. This is useful for longer-running topics. To set the defaults, use:

git branch --set-upstream <local-branch-name> <remote-name>/<remote-branch-name>

In summary, that's:

  • git checkout <branch>
  • git pull --rebase <remote-name> <remote-branch-name>

for each branch you want to update, with the caveat that I would generally advise against simply going through every branch until you're actually ready to do work on it.

If "master" is being actively developed, "remote-master" is being actively developed (and should be rebased-to), while "task_one" and "task_two" are based on "master", not directly based on "remote-master", it might make sense to only ever pull --rebase from "master", not any of the task branches, and to rebase those on top of master, instead. eg:

  • git checkout master
  • git pull --rebase <remote> master
  • git checkout <task-branch>
  • git rebase master

Though really, git pull --rebase <remote> master, mixed with the occasional git rebase master, may very well "do the right thing", depending on the complexity of your history. Don't rely on it, but be aware of the possibility. Understand how "git patch-id" and "git rebase" interact, as well as how "git rebase" plays with merges before you base your workflow on that simplification, but depending on your workflow, that could save you a couple of steps.

다른 팁

I would create a new local branch "merge_work" from master: git checkout -b merge_work

Then merge task_one: git merge task_one

I would then fix any conflicts then merge task_two git merge task_two again fix the conflicts.

Once that is all done move to master git checkout master

The merge merge_work: git merge merge_work

Note: This assumes all your branches are already local.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top