Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

git - Files in a branch is automatically adding to main branch

I am learning git. I have created a new branch named 'development' and in that branch when I am creating a file it is automatically adding to the main branch even I did not staged that file.

In the main branch there was two files named one.txt and two.txt.

I just checkout to development branch by

git checkout development

Then I created a file named three.txt by echo three > three.txt in the development branch. I did nothing after creating the three.txt file. I even didn't staged the file.

After crating the file I checkout to the main branch and I saw three.txt there. But how it is possible?

I don't want to have the file in main branch before I merge it.

This is happening like sync system. If I staged a file form any branch it is automatically staged in the main branch and vice-versa. What is the solution? How can I solve this problem? Help me please.

OS: Windows 10

git version: 2.32.0

I used both power-shell and git-bash and getting same result.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you run git status you'll see this file in next section:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)

Also you can see hint here what to do next with you file, (you can stash your file as well).

Technically your file doesn't belong to any branch yet, b/o you didn't commited it yet, so git will keep track of this file with purpose don't lose your changes even when you change branch.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...