简体   繁体   中英

Git branch and ls

I'm not sure if this is the right place to ask this, please redirect me if not.

I'm new to git and while learning it I stumbled upon this. How does git branch branchName and 'ls' work with each other. For eg: If I have a master and test branch and test branch has an extra testFile when compared to master branch. Now, while in the master branch, if I ls , I'll wont see the testFile but after switching to the test branch and ls , I'll see the testFile

kiran@kiran-desktop:/media/kiran/Linux_Server/dev$ git branch
  master
* test
kiran@kiran-desktop:/media/kiran/Linux_Server/dev$ git checkout master
M   editor/editor_parts/syntax/operator_syntax.js
Switched to branch 'master'
kiran@kiran-desktop:/media/kiran/Linux_Server/dev$ ls
cgi-bin     index.php                    misc           underConstruction
editor      jquery-1.5.2.min.js          php.php        userManage
fileManage  jquery-ui-1.8.11.custom.css  projectManage  userPages
images      login                        test.php
kiran@kiran-desktop:/media/kiran/Linux_Server/dev$ git checkout test
M   editor/editor_parts/syntax/operator_syntax.js
Switched to branch 'test'
kiran@kiran-desktop:/media/kiran/Linux_Server/dev$ ls
cgi-bin     index.php                    misc           test.php
editor      jquery-1.5.2.min.js          php.php        underConstruction
fileManage  jquery-ui-1.8.11.custom.css  projectManage  userManage
images      login                        testFile.txt   userPages
kiran@kiran-desktop:/media/kiran/Linux_Server/dev$ 

But pwd from both branches shows the same location.

So, how does switching branches change the output of ls ( which as I understand is a function of linux) ?

Git, unlike in SVN what you know keeps branches in different directories, keeps only the current working branch in the repository directory.

All the branches (Actually, all the objects) are stored inside the '.git' folder in the root of the repo and only the files belonging to the specific branch are present while you have checked out a specific branch. (and those files that are not added to the repo)

git checkout switches you from one branch to the other. To do this, it replaces the files in the checked out repository with the ones from the branch.

Your repository is effectively the .git subdirectory.

If you do a ls -a , you'll see it:

% ls -a -1
.git
.gitignore
...

Tracked files are stored in there. You normally only see the currently checked out branch. When you checkout a different branch, git grabs the files from .git and you can see them with ls .

Have a look at the answers to this question for more information about how git works: Where can I find a tutorial on Git's internals?

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