Example:
branch-one
| assets # directories
| views
| core
branch-two
index.html # only one source file
When switching between these two branches (ie on branch-one to branch-two), all of the directories from branch-one are being copied over.
Example:
> # on branch-one
> mkdir assets views core
> git add .
> git status
On branch branch-one
No commits yet
nothing to commit (create/copy files and use "git add" to track)
> git commit -m "directories"
On branch branch-one
Initial commit
nothing to commit (create/copy files and use "git add" to track)
> git checkout -b branch-two
Switched to a new branch 'branch-two'
> git status
On branch branch-two
No commits yet
nothing to commit (create/copy files and use "git add" to track)
> ls
assets/ core/ views/
Now in my branch-two, all of the directories are copied over (which I don't want).
Question:
How do I move between branches without the source files (or directories) between them being copied over?
As in this SuperUser answer by slhck:
Git does not track directories; it only tracks files.
If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via git add if it's empty.
Therefore the problem is not about commit history or branches at all: Git will remove the files but will ignore the directories (for which it has no concept). They're not copied, they're simply disregarded where they exist in your directory.
Instead, you might need your own workflow to run git clean
—this will permanently delete all untracked directory contents, which are by definition not backed up in git, so read the docs and use extreme caution.
To simply delete empty subdirectories, use this unix SE answer by Christophe Drevet-Droguet:
find . -type d -empty -delete
It's not the the directories are being copied between branches, it's just that they aren't being deleted when you switch branches, because Git doesn't have a reason to delete them, yet.
The problem is that your directories are empty. Try creating a file in one of the directories, add it and commit it to one branch. Then switch to the other branch and you'll see that directory disappear.
To avoid this in the future, don't create a directory until you are ready to commit something inside of it.
Here is the key line in your git status
output:
No commits yet
Note how this line repeats even after you run git commit
. This is because git commit
was unable to create a new commit, as there were still no files to commit. 1
As others noted, Git does not store empty directories. That means these are merely untracked directories (albeit ones with no files in them). They just stick around between changes of branches.
Once you make some file(s) in some directory, and make commits with those, Git will generally remove the directory once it becomes empty, as you switch from a branch that has some files in that directory to one that does not have any such files.
1 This has a few extra side effects. In particular, it means you never got branch-one
created either. A branch name exists by storing the hash ID of some commit. With no commits, no branch names can exist at all. Git nonetheless wants to be "on" some branch, so you're on branch branch-one
and then later on branch branch-two
, yet neither branch exists, So as you switch between nonexistent branches. nothing happens.
You can create an initial empty commit using git commit --allow-empty
. This allows you to create as many branch names as you like—all selecting this initial empty commit—but there's no real point to that either, and it won't help with the empty directory issue.
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.