4

I'm just interested in how git "hides" files between branches. I was looking for it but didn't find anything. So let me describe what I mean:

I have to branches master && red

Switch to the red branch and create a new file and also commit it

git checkout red
touch test.js
git add test.js
git commit -m "Added new file for test"
ls 

And i got list of files in my repo on the red branch

README.md app.js index.html new-red.js new-test.js red.js test.js

Now let switch to the master branch and tyle ls

git checkout master
ls

files on a master branch

README.md app.js index.html new-red.js new-test.js red.js

So how git "hides" the test.js files between branches?

Thanks

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • What do you mean? Git does *not* hide files. It *replaces* existing files in your working directory with a branch's contents. That's why git won't let you switch away from a branch with uncomitted changes - the changes would be overwritten and lost – Panagiotis Kanavos Nov 23 '15 at 11:06
  • @PanagiotisKanavos ok, how does git hides contents between branches? In a red branch i commited a test.js file, but didint merge it with master, how git hides the test.js file for master branch? –  Nov 23 '15 at 11:07
  • I just said that it *doesn't*. It deletes your old files and writes new ones on top of them. I suspect you are actually asking "where does git store repository files"? The answer is "in the .git folder which you should never touch". Perhaps you should check a tutorial on Git? – Panagiotis Kanavos Nov 23 '15 at 11:10
  • So you mean all the time while i am changing between pages git add new files according to branches ? ./git/refs/heads here is the last check sum for all branches –  Nov 23 '15 at 11:14

2 Answers2

3

What you intended to ask is "Where does Git store all the data that is not currently in the working directory tree?".

The short answer is "In the .git directory."

For the long answer, see the documentation.

Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58
  • Just wondering what this adds that is not contained in my answer? – houtanb Nov 23 '15 at 11:27
  • @HBHB my 2c: your answer says that git doesn't hide the file. In fact, one could argue that this is exactly what it does. Instead of contradicting the OP, this answer reframes the question, which in itself helps add clarity. – GreenAsJade Nov 23 '15 at 11:33
0

Git doesn't hide the file. Each branch represents a certain development history and the files you see in your git repository are reconstructed from the git history (found in your .git directory) leading up to your current commit. If the current commit is on a different branch, well it just recreates the files that exist at that commit on that branch.

Slightly tangential: you see those files because you have a non bare repository with a GIT_WORK_TREE set to the same folder. This working tree could be located elsewhere on your computer and if you were to change the current commit of the git repository, the files in that working tree would be updated.

For more info on how git reconstitutes the files, see this.

Community
  • 1
  • 1
houtanb
  • 3,852
  • 20
  • 21
  • 1
    All the time git recreates files depends on current branches ? –  Nov 23 '15 at 11:17
  • @nAz I don't understand your question. When you change your commit, whether it be to a different branch or not, git recreates the files that existed at that commit. No files are hidden. What was there (and tracked) before is removed and the files that existed in the commit you just checked out are written. – houtanb Nov 23 '15 at 11:20
  • 1
    The answer to @nAz 's question is "yes". Each time you change branch, git _recreates_ the files in the working copy. – GreenAsJade Nov 23 '15 at 11:29