简体   繁体   中英

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.

I was working on a branch that forked from master yesterday. 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

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

Then, once on that branch I committed, pushed and opened a MR to 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
  2. I made my first commit (renaming files) let's call it commit_1_A
  3. More commits, commit_2_A and 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
  6. Then commit, commit_1_B
  7. Push the branch and open a MR
  8. commit_1_A and commit_1_B are on the MR, but not commit_2_A or 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 . 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 .

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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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