Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: adding file adds to all branches

Tags:

git

branch

I'm new to Git.

i created a branch off of master. I'm working in that branch. i then go and create a new file in that new branch. I then switch back to master to view something.

i see that file that i just added when i switch over to master as a file to add? I don't want to add that file to the master branch. Aren't those branches supposed to be completely independent of each other? I added it to the new branch, not the master branch. How can i separate the branches?

like image 736
user1161137 Avatar asked Feb 13 '17 23:02

user1161137


People also ask

Does git add add to all branches?

If you git add a file, but then switch branches before committing, the staged addition will be carried over to the new branch (just like any other staged changes).

How do I add files to all branches?

You use git add . to copy all files in the current directory or any sub-directory into the index,2 so that they can be committed. This includes this new file x that you just created in your work-tree. Your index is now ready for committing.

Does git automatically add new files?

Yes you can say git add *. py Make sure you have done git init before doing this. To add all files in a folder you can simply say git add .

How do I exclude files from git add?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.


1 Answers

If you git add a file, but then switch branches before committing, the staged addition will be carried over to the new branch (just like any other staged changes). You probably wanted to commit before switching branches.

Any uncommitted changes, including files that are not added to the repository, are preserved when switching the working copy between branches.

Just making a new file does not automatically add it to the repository, you have to do a git add myFile (and eventually commit that).

Or, looking at it the other way, even if the file stays around in your working copy when you switch branches, it won't become part of those other branches unless you explicitly add and commit them there, either.

like image 135
Thilo Avatar answered Sep 23 '22 16:09

Thilo