[英]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:
如果更容易理解,这是一个事件列表:
git checkout -b fix/B --no-track master
git checkout -b fix/B --no-track master
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_A和commit_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.