简体   繁体   English

eclipse git confusions:如何合并和推送?

[英]eclipse git confusions: How to merge & push?

I can never seem to figure out how to use git. 我似乎永远无法弄清楚如何使用git。 I just do random commands and it works eventually. 我只是做随机命令,它最终起作用。 This is what I usually do when I want to push: 这就是我想要推动时通常做的事情:

  • Fetch from upstream 从上游获取

  • Commit 承诺

  • Push to upstream 推送到上游

However, the above steps don't work sometimes. 但是,上述步骤有时不起作用。 For example, when I do fetch from upstream, my local branch doesn't get updated. 例如,当我从上游获取时,我的本地分支不会更新。

How do I merge the remote branch into my local branch? 如何将远程分支合并到我的本地分支? Is there a difference between pull and fetch? pull和fetch有区别吗?

"git pull" is equivalent to "git fetch" followed by "git merge" “git pull”相当于“git fetch”后跟“git merge”

What you want to do is: 你想要做的是:

git commit -am "your message here"

This commits your current changes locally, and only locally. 这将在本地提交您当前的更改,并且仅在本地提交。 Now, assuming branch "foo": 现在,假设分支“foo”:

git pull origin foo

This does a "git fetch" followed by a "git merge". 这会进行“git fetch”,然后是“git merge”。 Everything in this step is done to your local branch, and still has no effect on the remote branch. 此步骤中的所有内容都将完成到您的本地分支,但仍然对远程分支没有影响。 Depending on how your git is setup, you might need to specify the branch. 根据git的设置方式,您可能需要指定分支。

If you have a merge conflict, you will have to fix the files listed, then add them back: 如果您有合并冲突,则必须修复列出的文件,然后将其添加回来:

git add path/to/resolved.file

When you are done, push everything you have from your local changes, to the remote server: 完成后,将本地更改中的所有内容推送到远程服务器:

git push origin foo

If you pull before committing, that might cause your local branch to not be updated. 如果在提交之前提取,则可能导致本地分支无法更新。

tl;dr, always commit before pulling! tl;博士,在拉之前总是提交!

Well to understand differences betwene git fetch and git pull look here . 好了,了解betwene git的提取和git拉一下差别在这里

In the simplest terms, "git pull" does a "git fetch" followed by a "git merge". 用最简单的术语来说,“git pull”执行“git fetch”后跟“git merge”。

You can do a "git fetch" at any time to update your local copy of a remote branch. 您可以随时执行“git fetch”来更新远程分支的本地副本。 This operation never changes any of your own branches and is safe to do without changing your working copy. 此操作永远不会更改您自己的任何分支,并且可以安全地执行而无需更改您的工作副本。 I have even heard of people running "git fetch" periodically in a cron job in the background (although I wouldn't recommend doing this). 我甚至听说有人在后台的cron作业中定期运行“git fetch”(虽然我不建议这样做)。

Working with git in eclipse might be a little bit tricky, but if you understand git basics, you should cope with it. 在eclipse中使用git可能有点棘手,但是如果你理解git基础知识,你应该应对它。 I recommend you to read one of git tutorials , so you will be able to understand basic git operations. 我建议你阅读一个git 教程 ,这样你就能理解基本的git操作。

The first step to learn git: Don't use egit. 学习git的第一步:不要使用egit。

Now, you are fetching and pulling from upstream. 现在,你正从上游获取和拉动。 That makes me think that several people have write-access to the same repository, so we probably don't want to be doing a whole lot of merges to complicate history. 这让我觉得有几个人对同一个存储库有写访问权限,所以我们可能不希望进行大量的合并来使历史复杂化。 It would be best to do this. 最好这样做。 I'm assuming you have already committed a changes or a set of changes to your local master branch that you want to place on the upstream repository which has been pushed to by someone else while you were making your commits. 我假设您已经提交了一个更改或一组更改到您希望放置在upstream存储库中的本地master分支,这些更改是在您提交时由其他人推送的。

First, we fetch the new changes but don't use them yet. 首先,我们获取新的更改但尚未使用它们。 This updates upstream/master to the new head of the upstream master branch. 这将upstream/master更新为上游master分支的新负责人。

git fetch upstream master

Now, we need to pull in these changes. 现在,我们需要引入这些变化。 This command rewrites history, and we are assuming that you have not published your changes anywhere yet. 此命令会重写历史记录,我们假设您尚未在任何位置发布更改。

git rebase upstream/master master

If you have published your changes, this messier command is the one you should use (do not use both, just use this one!) 如果你已经发布了你的更改,那么你应该使用这个更混乱的命令(不要同时使用它们,只需使用这个!)

git merge upstream/master master

Now, we can push: 现在,我们可以推动:

git push upstream master

The first two steps can be shorted to git pull --rebase and git pull for the rebase and merge versions respectively. 前两个步骤可以分别缩短为git pull --rebasegit pull for rebasemerge版本。

If you are already on the master branch, most of those second arguments are superfluous but I wrote them in for clarity. 如果你已经在master分支上,那么大多数第二个参数都是多余的,但为了清楚起见我写了它们。 Notably, giving a second argument to git-rebase or git-merge will simply check out that branch before doing the operation. 值得注意的是,给git-rebasegit-merge提供第二个参数只会在执行操作之前检查该分支。 Supplying master to fetch and push is only necessary if you don't have the refs set up to automatically fetch and push master. 只有在没有将refs设置为自动获取和推送master时,才需要提供masterfetchpush

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

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