문제

Here's my scenario: there are two users, they both cloned the same repository (so this means they have the same remote). One creates a branch locally, let's say 'branch1' starts some developing/committing in it, and in the end wish to push the branch to remote. This is possible.

However, the second one does the same, creates a branch with the same name ('branch1'), commits something in it and in the end wish to push; Is this possible? Which commands should he run in order to work? Is it enough a pull before doing the push?

Thanks.

도움이 되었습니까?

해결책

First, this just sounds risky and likely to lead to accidental removal or merging of code. I think a better scenario would be for the first person to create the branch and push it up, then the second person to pull the branch down and begin working from there.

However, I suppose ONE WAY to do this is that the second person can fetch down the history and then merge the remote branch into their local branch and then push their branch up.

git fetch
git checkout branch1
git merge origin/branch1
git branch -u origin/branch1
git push

From this point forward the second person should have their local branch tracking the same remote branch and fully merged and integrated.

다른 팁

There is no requirement that the local branch name and the branch name pushed to the remote be the same. This can be very useful for this kind of situation, especially in conjunction with "namespacing" branches. Your users could do something like this:

git push origin branch1:<username>/branch1

which will result in two distinct branches on the origin remote, even though the branch name they assigned locally is identical. That would then allow each user to git fetch the other users branch(es) to compare as well. Except for some specific set of "well-known" branch names (master, dev, test, etc...), doing something like this is actually a pretty useful workflow...

The two users are actually working on the same branch, so all limitations of that should apply.

The best practices for that kind of collaboration, are somewhat subjective. What I like is rebasing my work on the remote and then push a patch of some commits to the remote.

The second user will have their push rejected because the branch is not up-to-date. When they check their status they will have their status say that the branch has diverged and they are ahead by x commits and behind by y commits. The x commits will be the number of commits since the common merge-base of the two branches. And the y commits will be the ones that are on the second users branch that are not on the pushed branch.

The second user will need to do a merge/rebase operation to sync the branches before they are able to push. Which shouldn't be a problem.

Generally this should be ok, because whichever order users a and b push in, the other can pull and merge them together. This:

usera$ git push

userb$ git pull
userb$ git push

will result in something like:

C1 -- C2 -- ... <master
       |\
       | a1 -- a2 -- a3          <user a branch
        \                >- ab4    <remote branch (merge commit)
         b1 -- b2 -- b3          <user b branch

It's really no different to users a and b working on their local copies of master, and then having to merge the results.

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