刚接触 Git 和 GitHub,我做错了吗?

发布于 2024-11-17 09:50:49 字数 331 浏览 1 评论 0原文

我对一个现有文件 test.cs git add 进行了更改

  1. git commit -m “我对 test.cs 的更改”
  2. git pull origin master
  3. >它拉下 SomeOtherFile.cs (由其他人更改,不是新的)
  4. > >我检查构建...)
  5. git push

当我转到 github 时,我的推送下有两个提交,一个用于“test.cs”,一个用于 SomeOtherFile.cs

这是预期的行为吗?

谢谢..

I make a change to one existing file test.cs

  1. git add .
  2. git commit -m "my change to test.cs"
  3. git pull origin master
  4. > it pulls down SomeOtherFile.cs (changed by someone else, not new)
  5. > I check the build... )
  6. git push

When I go to github, there are two commits under my push, one for 'test.cs', and one for SomeOtherFile.cs

Is this expected behavior?

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2024-11-24 09:50:49

当您执行 git add . 时,您会将工作目录中的任何文件的更改添加到索引中。如果您只想为 test.cs 创建提交,请改为:

git add test.cs

When you do git add ., you will add changes to any files in your working directory to the index. If you only want to create a commit for test.cs, do instead:

git add test.cs
烟柳画桥 2024-11-24 09:50:49

所以你在这里遇到的是合并和变基之间的区别。当您执行以下操作时:

git pull origin master

您创建了一个合并提交,该提交合并了 master 中的更改。该合并提交是空的,但它已创建。合并的这种行为是预期的,但在我看来是不可取的。创建提交是为了保存两个分支(您的本地分支和 Origin Master)之间的差异,并且无论是否存在任何差异都会创建提交。如果你改为发布,

git pull --rebase origin master

你就会进行变基。变基会查找本地和 Origin Master 共同的最后一次提交,将自该提交以来的更改保留到临时存储中,然后对 Origin Master 的更改进行分层(来自 NotYou 的 SomeOtherFile.cs 提交),最后重播您的更改在新更新的线路之上。此重放是逐个提交完成的,因此如果您提前 10 个提交,那么它将发生 10 次。

如果没有冲突,那么这一切都会立即发生,无需干预。如果存在冲突,那么您将在中间停止,要求纠正问题,然后您发出 a

git rebase --continue

以使流程继续。这里发生的情况是,冲突是通过重新定义原始提交来处理的,而不是通过添加合并提交来处理。但它保持文件历史记录干净,而合并并不像您所看到的那样。

So what you are running into here is the difference between a Merge and a Rebase. When you did the:

git pull origin master

You created a merge commit which merged the changes in from master. That merge commit was empty, but it was created. This behavior from merge is expected, but in my opinion undesirable. The commit was created to hold the differences between the two branches (Your local and Origin Master), and is created regardless of the existence of any differences. If you had instead issued

git pull --rebase origin master

You would have done a rebase. A rebase looks for the last commit your local and Origin Master have in common, sets aside into temporary storage your changes since that commit, then layers on the changes from Origin Master (the SomeOtherFile.cs commit from NotYou), and finally replays your changes on top of that new updated line. This replaying is done commit by commit, so if you're 10 commits ahead it will happen 10 times.

If there are no conflicts, then this all happens instantly and without intervention. If there is a conflict, then you will be stopped in the middle, asked to correct the problem, and then you issue a

git rebase --continue

to have the process move on. What happens here is that conflicts are handled by redefining your original commit, rather than by adding a merge commit. But it keeps the file histories clean, which merge does not as you have seen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文