简体   繁体   English

从远程git存储库中提取更改

[英]Pull changes from remote git repository

I'm new to git. 我是git的新手。 What I have done is forked several repositories I'm interested in and then cloned them on my computer to work with them. 我所做的是分叉我感兴趣的几个存储库,然后将它们克隆到我的计算机上与它们一起工作。

Some of the original projects may be significantly updated for I ever mess with my local copies, or I might make some insignificant changes. 有些原始项目可能会因为我的本地副本混乱而大幅更新,或者我可能会做一些微不足道的更改。

From what I understand, I can "rebase" my clones to "pull" in the changes from the original. 根据我的理解,我可以“改变”我的克隆,以“拉”原始的变化。

What does that do to my changes? 这对我的变化有什么影响? For example, suppose there is a file DoSomething.cpp from the original. 例如,假设原始文件中存在DoSomething.cpp文件。 I modify it, maybe fix a small bug or add a feature. 我修改它,可能修复一个小bug或添加一个功能。 Now! 现在! 1 year later the original project has gone through many revisions and is much better. 1年后,原始项目经历了许多修改,并且要好得多。 I want to "pull" those changes into my clone BUT keep my change too! 我想把这些变化“拉”到我的克隆中,但也要保持我的变化! (so this is sort of the reverse of a push) (所以这与推动相反)

Is this easy to do? 这很容易吗? If so, what's the basic idea? 如果是这样,那么基本的想法是什么?

What I would like is that any changes in my clone from the original clone(things I've changed) are not overwritten but I can actually merge my changes and with the original(on my fork) and be given the ability to actually check and accept the changes. 我想要的是我的克隆从原始克隆(我已经改变的东西)的任何变化都没有被覆盖,但我实际上可以合并我的更改和原始(在我的分支上),并被赋予实际检查和接受变化。 (for example, if DoSomething.cpp was changed on the original then I need to compare the changes to make sure they are compatible. (例如,如果在原始版本上更改了DoSomething.cpp ,那么我需要比较更改以确保它们是兼容的。

I guess this is not difficult since I am the owner of the fork I can rebase or hard reset it then push my local changes to my fork? 我想这并不难,因为我是fork的所有者我可以重新设置或硬重置它然后将我的本地更改推送到我的分支? (not sure if it will work though since there is a huge potential for versioning issues) (不确定它是否会起作用,因为版本问题存在巨大潜力)

You're right, rebasing is one way you can keep your code up to date, and is very commonly used to do so. 你是对的,变基是你保持代码最新的一种方式,并且非常常用。

In my experience, rebasing is more useful in terms of managing your git history. 根据我的经验,在管理你的git历史方面,变基更有用。 It keeps your history nice and linear, it makes it seem like the work happened sequentially rather than in parallel. 它使您的历史保持良好和线性,这使得工作似乎顺序而不是并行发生。 Regular merges on the other hand will involve lots of diverging/converging commits. 另一方面,定期合并将涉及许多分歧/融合提交。 You can use git log --graph to see this difference visually. 您可以使用git log --graph地看到这种差异。

In a nutshell, rebase takes your commits, turns them into patches, and then applies them to the branch that you're rebasing onto. 简而言之, rebase接受你的提交,将它们变成补丁,然后将它们应用到你正在重新定位的分支上。 If there are conflicts git will stop and ask you to resolve them, and then you can have it continue. 如果有冲突,git会停止并要求您解决它们,然后您可以继续。 So you're still merging and resolving conflicts, but just in a way that makes the history linear. 所以你仍然在合并和解决冲突,但只是让历史变得线性。

  1. Find out which branch you're on, this is done with git status , it'll show you the name of the branch on the first or second line. 找出你所在的分支,这是通过git status完成的,它会显示第一行或第二行的分支名称。

  2. For testing, I'd suggest to first branch off your changes into a separate branch: 为了测试,我建议首先将您的更改分支到一个单独的分支:

     git checkout -b my-patches 
  3. Now make sure that all your changes are committed. 现在确保所有更改都已提交。 For this, you again invoke git status . 为此,您再次调用git status Ideally it should show Working directory clean , but if thats not the case, use git add to add your changes to the index and git commit to finally commit them. 理想情况下,它应该显示工作目录是否干净 ,但如果不是这种情况,请使用git add将更改git add到索引,并使用git commit最终提交它们。 If you want to split your change into several different patchsets (can be handy when conflicts occur), I suggest that you read into how to use git commit -p . 如果你想将你的变化分成几个不同的补丁集(当发生冲突时可以很方便),我建议你阅读如何使用git commit -p You do that until all your changes are not listed in git status anymore. 您这样做,直到所有更改都不再列在git status There will be some files you didn't touch listed in git status , possibly results of builds. git status中会列出一些你没有触及的文件,可能是构建的结果。 If there are no changes you're interested in to keep in there, you're fine. 如果没有任何变化,你有兴趣留在那里,你没事。

    If there are any makefiles or such which support cleaning up the directory ( make clean for example), run them now. 如果有任何makefile或支持清理目录(例如make clean ),请立即运行它们。

  4. Then switch back to your original branch, using (substitute master with whatever branch name you found out in step 1): 然后使用(使用您在步骤1中找到的任何分支名称替换master切换回原始分支:

     git checkout master 

    If you want to make sure everything worked, run: 如果你想确保一切正常,请运行:

     git diff my-patches 

    It should list the lines you changed in your fork. 它应该列出你在fork中更改的行。 If not, something went wrong. 如果没有,出了点问题。

  5. Now comes the scary part. 现在是可怕的部分。 You'll throw away any changes which have been made to this branch now. 你将丢掉现在对这个分支所做的任何更改。 Note that, if you committed all your changes to the separate branch as described in step 3, they will be fine. 请注意,如果您将所有更改提交到单独的分支,如步骤3中所述,它们就可以了。 If you're unsure, you can make a backup by copying the whole repository folder. 如果您不确定,可以通过复制整个存储库文件夹进行备份。 Then you run (again, substitute master with whatever you had before): 然后你跑(再次,用以前的任何东西替换master ):

     git fetch git reset --hard origin/master 
  6. Ideally, your branch should now have the exact state of the origin/master branch. 理想情况下,您的分支现在应该具有origin/master分支的确切状态。 Make sure everything looks okay. 确保一切看起来都不错。 Then you merge back your changes using: 然后使用以下方法合并您的更改:

     git merge my-patches 

    Git will do it's best to make it as hassle-free as possible, but there will possibly be conflicts. Git会尽可能地让它尽可能轻松,但可能会有冲突。 Git will mark those in the respective files with >>>> and `<<<< markers. Git将使用>>>>`<<<< markers标记相应文件中的那些。 For resolving conflicts, I suggest you do some internet research or have a read on the section about Basic Merge Conflicts in the open Git Book . 为了解决冲突,我建议你做一些互联网研究,或者阅读开放式Git Book中关于基本合并冲突的部分 Make sure to commit the merge afterwards. 确保事后提交合并。

  7. The hard part is over. 艰难的部分结束了。 You can now delete the temporary branch: 您现在可以删除临时分支:

     git branch -d my-patches 

    The reason I used the temporary branch is to be able to easily revert the status of the repository to the one before the merge attempt. 我使用临时分支的原因是能够轻松地将存储库的状态恢复为合并尝试之前的状态。 One could've of course also checked out the remote status in a separate branch, but I prefer it like this. 当然也可以在一个单独的分支中检查远程状态,但我更喜欢这样。

Did you add upstream remotes to your forked repos? 你有没有为你的分叉回购添加上游遥控器? That's the easiest way to keep your fork in sync with it's master. 这是保持fork与其主人同步的最简单方法。 It's Step 3 here . 这是第三步在这里

First you should know that whether you "merge" or "rebase" you will not loose your changes and git will give you the chance to commit your changes & resolve the conflicts (if any) and then push your modifications back to the remote repo you are pulling from. 首先你应该知道,无论你是“合并”还是“变基”, 你都不会失去你的更改,而git会让你有机会提交你的更改并解决冲突(如果有的话),然后将你的修改推回到远程仓库你正在拉扯。

when you git pull you are telling git to do this: (pull default is to use "merge") 当你git pull你告诉git这样做:(拉默认是使用“合并”)

pull the latest copy of the files from the remote, merge them with my local changes & if there is a conflict that you couldn't resolve automatically then notify me so that I resolve it manually; 从远程文件中提取最新的文件副本,将它们与我的本地更改合并,如果存在无法自动解决的冲突,请通知我,以便我手动解决; it is straightforward. 这很简单。

when you git pull --rebase you are telling git to do this: 当你git pull --rebase你告诉git这样做:

temporary remove* the changes from my local copy (my modified files), pull the latest copy from the remote, merge my changes on top of it & if there is a conflict that you couldn't resolve automatically then notify me so that I resolve it manually. 临时删除*我的本地副本(我修改过的文件)中的更改,从遥控器中提取最新副本,在其上合并我的更改,如果存在无法自动解决的冲突,请通知我以便我解决它手动。 ( technically nothing is removed; it is just to make this otherwise vague logic clearer. ) (从技术上讲,没有任何东西被删除;只是为了使这个模糊的逻辑更清晰。

the difference is subtle, in the second case, your changes appear to be as if you have just made these changes on the top of the most recent copy that you pulled from remote... but as you could see, in both cases your changes are definitely kept (otherwise what is the use of git!). 差别很小,在第二种情况下,您的更改似乎就像您刚刚从远程提取的最新副本的顶部进行了这些更改...但正如您所看到的,在这两种情况下您的更改绝对保留(否则git的用途是什么!)。

Should you merge or rebase? 你应该合并还是改组? it is long discussion & there are places where one is better than the other and there are best practices for this; 这是一个长时间的讨论,有一个地方比另一个好,并且有最好的做法; some useful comments are already mentioned in this page and for more info you could search online, just type "git merge vs rebase" and you will see tons of pages about it : ) 本页已经提到了一些有用的评论,有关您可以在线搜索的更多信息,只需输入“git merge vs rebase”,您将看到大量关于它的页面:)

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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