简体   繁体   English

git 在从 master 分支后为新分支带来一些“旧”更改

[英]git bring some "older" changes to a new branch after branching from master

Today git made something I don't understand, and it happens to me from time to time, and I can live with this doubt.今天git做了一件我看不懂的事,而且时不时发生在我身上,我可以忍受这个疑惑。

I was working on a branch that forked from master yesterday.我昨天在从 master 分叉的一个分支上工作。 On that branch, the first changes I committed was renaming some files.在那个分支上,我提交的第一个更改是重命名一些文件。 Then I made some more commits, and today I did some more changes that I didn't committed.然后我做了一些更多的提交,今天我做了一些我没有提交的更改。 Instead, I decided to move those changes to a new branch, so I checked out from master without moving from my current branch neither committing those files with this command相反,我决定将这些更改移动到一个新分支,所以我从 master 签出而不从当前分支移出,也没有使用这个命令提交这些文件

git checkout -b fix/new-branch --no-track master

Then, once on that branch I committed, pushed and opened a MR to master.然后,一旦在那个分支上,我提交、推送并打开了一个 MR 到 master。 To my surprise the first commit on the other branch was there.令我惊讶的是,另一个分支上的第一次提交就在那里。 I can't understand how or why.我不明白如何或为什么。 If it is easier to understand this is a list of events:如果更容易理解,这是一个事件列表:

  1. I was on master, so I checked out to A我在master上,所以我检查了A
  2. I made my first commit (renaming files) let's call it commit_1_A我做了我的第一次提交(重命名文件)让我们称之为 commit_1_A
  3. More commits, commit_2_A and commit_3_A更多提交,commit_2_A 和 commit_3_A
  4. Made some changes, but don't commit them做了一些更改,但不要提交它们
  5. Checkout to a new branch from master without moving to master first: git checkout -b fix/B --no-track master从 master 结帐到一个新分支而不首先移动到 master: git checkout -b fix/B --no-track master
  6. Then commit, commit_1_B然后提交,commit_1_B
  7. Push the branch and open a MR推分店开个MR
  8. commit_1_A and commit_1_B are on the MR, but not commit_2_A or commit_3_A commit_1_A 和 commit_1_B 在 MR 上,但不在 commit_2_A 或 commit_3_A 上

According to your timeline, the graph of the local repository would look like this:根据您的时间线,本地存储库的图表如下所示:

* cea17b6 (HEAD -> fix/B, origin/fix/B) commit_1_B
| * 9fb360f (fix/A) commit_3_A
| * e302cca commit_2_A
| * e2852f4 commit_1_A
|/  
* 7dff81e (origin/master, master) Initial commit

We see that only commit_1_B should be in the MR for fix/B -> master .我们看到,对于fix/B -> master ,只有commit_1_B应该在 MR 中。 The most straight-forward explanation I see is that commit_1_A actually was (accidentally) committed to master and therefore is present in the branch fix/B .我看到的最直接的解释是commit_1_A实际上(意外地)提交给 master ,因此存在于分支fix/B中。

Since you don't mention a push of the master branch before creating the MR, it would result in both commit_1_A and commit_1_B to be included in the diff (as you mention).由于您在创建 MR 之前没有提到master分支的推送,因此它将导致commit_1_Acommit_1_B都包含在差异中(正如您所提到的)。 The actual graph in your case looks like this:您的案例中的实际图表如下所示:

* 2fde583 (fix/A) commit_3_A
* b0b940a commit_2_A
| * 5821436 (HEAD -> fix/B, origin/fix/B) commit_1_B
|/  
* 1a4d1f5 (master) commit_1_A
* a10f2da (origin/master) Initial commit

Which results from these commands (test-case):这些命令的结果(测试用例):

git init local-repo
git init --bare remote-repo
cd local-repo
touch file.txt
git add -A
git commit -m "Initial commit"
git remote add origin ../remote-repo
git push --set-upstream origin master
# assumed branching of fix/A
mv file.txt file2.txt
git add -A
git commit -m "commit_1_A"
git checkout -b fix/A          # actual branching of fix/A
touch file3.txt
git add -A
git commit -m "commit_2_A"
echo "test" > file3.txt
git commit -am "commit_3_A"
touch some-more-changes.txt
git checkout -b fix/B --no-track master
git add -A
git commit -m "commit_1_B"
git push --set-upstream origin fix/B
git log --oneline --graph --all

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

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