Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git new file appears in all branches

I thought a file created on one branch will not appear in any other branches until I merge or rebase the branch?

Example:

I have two branches:

  1. master
  2. new_contact_page

I checkout the new_contact_page branch.

$ git checkout new_contact_page

Then I create a new file.

$ vi contact_page.html

Without doing any Git commands, I switch back to my Master branch.

$ git checkout master

Then I see that this contact_page.html file is also in my Master branch.

$ ls   (contact_page.html shows up in the list!)

Shouldn't the file only exist in new_contact_page branch?

like image 914
Don P Avatar asked Sep 09 '12 18:09

Don P


2 Answers

Git will never touch any files that aren't actually in your repository. (untracked files)

You need to git add and git commit the file (into one branch) first.

like image 182
SLaks Avatar answered Oct 12 '22 19:10

SLaks


This is where is is important to understand the index (or staging area).
As long as you don't stage (git add) files, they remain "untracked" (or "private"), and won't be modified by a "git checkout".

That is different from "unstaged" which references tracked file (previously committed in the local Git repo) with local modifications not yet added to the index.

You can read more in "You could have invented git (and maybe you already have!)"

like image 22
VonC Avatar answered Oct 12 '22 19:10

VonC