default git behavior I don't understand

I have not decided if I like the following behavior regarding files / folders that are not under version control.

In particular, it seems strange that un-versioned files seem to follow you when you checkout different branches. It seems like said files should only exist in the branch they were created in.

  • What does git sync do in VSCode
  • Error deploying Play Framework 2.4 on Heroku
  • How can I download a previous git commit in Netbeans?
  • How to include a excluded file in sub directory in Git?
  • Java classpath issue when I specify the directory in the command
  • How does git-svn know which branch to dcommit to?
  • Can someone please help me understand why / if this is desirable behavior?

    For example:

    shopkins@shax:~/tmp/test$ ls
    hello.txt
    shopkins@shax:~/tmp/test$ git branch  
    -
    * master
    my_branch
    shopkins@shax:~/tmp/test$ 
    shopkins@shax:~/tmp/test$ git checkout my_branch 
    Switched to branch 'my_branch'
    shopkins@shax:~/tmp/test$ mkdir adir
    shopkins@shax:~/tmp/test$ touch adir/my_branch.txt
    shopkins@shax:~/tmp/test$ git add adir/
    shopkins@shax:~/tmp/test$ git commit -a -m "added adir with my_branch.txt"
    [my_branch d36964c] added adir with my_branch.txt
    0 files changed, 0 insertions(+), 0 deletions(-)
    shopkins@shax:~/tmp/test$ git checkout my_branch
    Switched to branch 'my_branch'
    shopkins@shax:~/tmp/test$ tree
    .
    |-- adir
    |   |-- my_branch.txt
    |   `-- orphan.txt
    `-- hello.txt
    
    1 directory, 3 files
    
    create mode 100644 adir/my_branch.txt
    shopkins@shax:~/tmp/test$ touch adir/orphan.txt
    shopkins@shax:~/tmp/test$ git checkout master
    Switched to branch 'master'
    shopkins@shax:~/tmp/test$ ls
    adir  hello.txt
    shopkins@shax:~/tmp/test$ tree
    .
    |-- adir
    |   `-- orphan.txt
    `-- hello.txt
    
    1 directory, 2 files
    

    EDIT
    As it turns out, nothing was changed in the files between the branches in the following first edit. Thanks for the help everyone!

    EDIT
    It seems that git does not write modification of versioned files when checkouts occur. In the following example, another.txt is not under version control:

    shopkins@shax:~/tmp/test$ ls -l
    total 4
    drwxr-xr-x 2 shopkins shopkins 4096 2011-02-21 21:49 adir
    -rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:47 another.txt
    -rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:49 hello.txt
    shopkins@shax:~/tmp/test$ git checkout my_branch
    Switched to branch 'my_branch'
    shopkins@shax:~/tmp/test$ ls -l
    total 4
    drwxr-xr-x 2 shopkins shopkins 4096 2011-02-21 21:49 adir
    -rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:47 another.txt
    -rw-r--r-- 1 shopkins shopkins    0 2011-02-21 21:49 hello.txt
    shopkins@shax:~/tmp/test$ 
    

  • `git rebase` entire branch-tree
  • How to set git diff parameter in Github desktop gui
  • How to identify Git object type based on its SHA1 code?
  • Git subrepositories
  • Explicit and safe git pull in Intellij IDEA
  • Can't type password in Git Bash
  • 3 Solutions collect form web for “default git behavior I don't understand”

    Git doesn’t even know that the un-versioned files exist. Technically they aren’t even part of the repository. That is why they are called “un-versioned files.” Git does not modify, delete, store, or keep track of any file unless it is told to.

    The classic situation where this is desirable is if your software generates temporary files as part of its build process, or if the compiled executables are left in the same directory as the source code. Obviously, if someone is cloning your repository so they can download your software from you, you would want all those files to be “left behind.” But more importantly, you would not want them cluttering up the version history. It doesn’t matter when these files change, only when the source code changes. So seeing diffs of these files with every commit will only make it harder to find the information you actually want.

    well this can make builds faster as un-added output hangs around between branch switches and you don’t have your make output to some external directory.

    Why would git delete a file it can’t restore?
    Git just leaves alone the files it doesn’t know about.

    Git Baby is a git and github fan, let's start git clone.