git pull 并解决冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35025587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git pull and resolve conflicts
提问by Asim Mahar
I am learning git and I have a scenario:
我正在学习 git,我有一个场景:
- My coworker makes changes and pushes the changes to the master.
- I make changes locally to my master.
- 我的同事进行更改并将更改推送给主。
- 我在本地对我的主人进行了更改。
So far from my understanding, at this point I can either:
到目前为止,我的理解是,在这一点上,我可以:
- Pull the master that my coworker worked on and fix the merge conflicts that I will end up having.
- 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.
我想知道是否有更好的方法来做到这一点。
采纳答案by k0pernikus
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
,我宁愿这样做:
git pull --rebase
This avoids a merge commit and keeps your changes on top of the master 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.
然而,当不同的人在同一个分支上工作时,您仍然必须解决任何发生的合并冲突,这是一种不好的做法,尤其是因为它会导致冲突。
In cases with minor changes, yours are just applied on the top. You are changing the history, yet only your localone, not the remote's.
在有细微变化的情况下,你的只是应用在顶部。你正在改变历史,但只是你本地的,而不是远程的。
You won'tneed to git push --force
. You just git push
it as you are used to it.
你不会需要git push --force
。你只是git push
习惯了它。
In general, you should be working on feature branches and merge those back to the master branch.
通常,您应该在功能分支上工作并将它们合并回主分支。
When working on master branches one can also keep the feature branch close to the master by:
在主分支上工作时,还可以通过以下方式使功能分支靠近主分支:
git checkout feature-branch
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.
然而,这里需要git push --force
用到功能分支,所以如果不止一个人在同一个功能分支上工作,应该小心不要使用这种策略。
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.
如果想要使用变基和强制推送,请考虑使用git push --force-with-lease
over,git push --force
因为它可以防止意外删除远程上其他人的提交。
回答by Patrick Motard
One (simple*) way to handle this without branching or stashing:
一种(简单*)方法来处理这个而无需分支或隐藏:
- stage/commit your changes locally
- pull remote
- at this point you'll be notified of any merge conflicts. If git cannot automatically resolve merge conflicts, it will open the two versions in whatever editor you have set up as your default merge editor. I recommend BeyondCompare.
- commit your merge, push your merge and commits remote master
- 在本地暂存/提交您的更改
- 拉遥控
- 此时,您将收到任何合并冲突的通知。如果 git 无法自动解决合并冲突,它将在您设置为默认合并编辑器的任何编辑器中打开这两个版本。我推荐 BeyondCompare。
- 提交您的合并,推送您的合并并提交远程主
回答by melatron
Best way from my experience is:
根据我的经验,最好的方法是:
- When working on a new feature, make a new branch cloned from the master. Always keep the branches up to date with the master.
- After writing and committing your new features to the created branch, you test your app.
- Then merge master into the working branch, if there are new commits in the master, you may have merge-conflict. After resolving it - test again.
- If everything is working as supposed to merge your branch to the master.
- 在处理新功能时,从 master 克隆一个新分支。始终保持分支与 master 保持同步。
- 在编写新功能并将其提交到创建的分支后,您可以测试您的应用程序。
- 然后将 master 合并到工作分支中,如果 master 中有新的提交,则可能存在合并冲突。解决后 - 再次测试。
- 如果一切正常,将您的分支合并到主分支。
That's how you can develop many new features and merge only the working ones when you need to deploy. Good luck and check this -> https://www.codeschool.com/courses/try-git
这就是您可以开发许多新功能并在需要部署时仅合并可用功能的方式。祝你好运并检查这个 -> https://www.codeschool.com/courses/try-git
回答by Jared Price
If you're both working on the same file(s), you'll inevitably run into conflicts. You'll just have to learn how to resolve conflicts.
如果你们都在处理同一个文件,你将不可避免地遇到冲突。你只需要学习如何解决冲突。
If you want to avoid having to resolve conflicts, then maybe you guys should delegate tasks to each other that involve working on a different files.
如果您想避免必须解决冲突,那么也许你们应该将涉及处理不同文件的任务委托给彼此。
回答by HappyCoding
I believe git sync would be good. Git sync commits your changes, pulls any changes existing in master, then pushes all the changes together to master.
我相信 git sync 会很好。Git sync 提交您的更改,拉取 master 中现有的任何更改,然后将所有更改一起推送到 master。