Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

git pull and resolve conflicts

I am learning git and I have a scenario:

  1. My coworker makes changes and pushes the changes to the master.
  2. I make changes locally to my master.

So far from my understanding, at this point I can either:

  1. Pull the master that my coworker worked on and fix the merge conflicts that I will end up having.
  2. Create a back up of my local, clone a new copy of the master, and then merge my changes to the master.

I want to know if there is any better way of doing this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If there are different changes on both the remote and the local branch, instead of just pulling the master by git pull, I rather would do:

git pull --rebase

I even have it in my default config so that it will always will do a rebase if required on git pull:

git config --global pull.rebase true

A rebase avoids a merge commit and keeps your changes on top of the current remote branch.

Yet you still have to resolve any occurring merge conflicts when different people are working on the same branch, which is a bad practice especially because it leads to conflicts.

(Also be aware that in the scope of a rebase, the meaning of theirs and ours is switched.)

In cases with minor changes, yours are just applied on the top.

You are changing the history, yet only your local one, not the remote's.

You won't need to git push --force. You just git push it as you are used to it.

In general, you should be working on feature branches and merge those back to the master branch.

When working on feature branches one can also keep the feature branch close to the master by:

git checkout feature-branch
git fetch && git rebase origin/master

Yet here one would need to git push --force the feature-branch, so one should be careful not to use this strategy if more than one person is working on the same feature-branch.

If one wants to use rebase and push forcing, consider using git push --force-with-lease over git push --force as it prevents accidentally deleting other's commits on the remote.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...