简体   繁体   中英

Git (Bit Bucket) merge changes from two users to same branch

I have created a branch in Git (bit bucket). Two persons are modifying same file. One person committed the file. Other person also has local changes in the same file. The second person should be able to merge his code without losing his contents and also the earlier commits should also be intact. I searched many options, but could find a clear reply. Please help me on this.

Git won't allow you (unless you --force it) to push a set of commits to a remote branch unless the HEAD of that remote branch is an ancestor of that set of commits.

In other words, if you push, and the other person pushes after you, the other person's push will fail because their commits aren't descendants of your commits. They'll have to fetch your change, rebase their commits on top of that change (or merge it, as an alternative to rebasing) and only then will they be able to push (provided that the remote repo didn't change even more while they were rebasing).

Yes, second person will be able to commit his changes. But before commit he will have to pull the changes of the first person. After pulling the changes if there is merge conflict, the second person has to fix the those conflict and then commit. Tips: Working on a same file at the same time is not a good idea. Almost always it creates conflict.

Well, you problem is independent of which git provider use (github, bitbucket, codebase, etc.)

A possible workflow to your case is the following.

There are two developers, Alice and Bob . Both modified the parameters.yml file with next content.

# parameters.yml
database_user: 'root'
database_password: 'root'

Alice adds a parameter to turn on/off the cache.

# parameters.yml
database_user: 'root'
database_password: 'root'
cache: true

While Bob adds other parameter to turn on/off the debug mode in the app.

# parameters.yml
database_user: 'root'
database_password: 'root'
debug: false

The Alice pushes hers changes to super-cache branch. After, Bob pushes his changes to super-cache branch, also. However his push fail because, as @PSkocik said, the Bob 's commits are not descendants of Alice 's commits.

Bob have to fetch the Alise 's commits before he pushes his commits. He choices to do a pull with rebase git pull --rebase origin/super-cache

Bob will have a conflict in parameters.yml . The file after resolve the conflict will be

# parameters.yml
database_user: 'root'
database_password: 'root'
debug: false
cache: true

Now, Bob can continue with the merge with git rebase --continue

After this, Bob have all commits of super-cache branch plus his commits.

Finally, Bob can push his commits.

You can see: Git rebase of Atlassian

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM