Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:15:14+00:00 2026-06-10T05:15:14+00:00

For example, I create file a in the repo(suppose I’m on the master branch),

  • 0

For example, I create file a in the repo(suppose I’m on the master branch),
then I git add a and git commit.
After that I git branch copy and git checkout copy.
Finaly I create file b in the word directory then git add b.

Git seems to be smart when I checkout back to the master branch, and git ls-files, file b is not listed.

So I’m confused, since we only have one index file in a repo, how can git maintain different staging area for branches at the same time?

EDIT:

How to explain the files which are staged but not commited, is still remembered per branch?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:15:16+00:00Added an answer on June 10, 2026 at 5:15 am

    I’ve not dived into the implementation in detail but when you switch a branch, the index file is manually updated to reflect the content of the new HEAD.

    For example, I have to branches here master (with one file) and test (with two files).

    noufal@sanitarium% git branch
      master
    * test
    noufal@sanitarium% file .git/index
    .git/index: Git index, version 2, 2 entries
    noufal@sanitarium% git checkout master
    Switched to branch 'master'
    noufal@sanitarium% file .git/index
    .git/index: Git index, version 2, 1 entries
    

    It’s changed the index when the branch switching happened.

    Also, if you “manually” switching branches, git doesn’t update the index and gets confused. Continuing from above.

    noufal@sanitarium% more .git/HEAD
    ref: refs/heads/master
    noufal@sanitarium% echo "ref: refs/heads/test" > .git/HEAD
    noufal@sanitarium% file .git/index
    .git/index: Git index, version 2, 1 entries
    noufal@sanitarium% git status
    # On branch test
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       deleted:    b
    #
    

    In other words, the index has a missing file which is there in the current repository so it’s “staged for delete”.

    As for switching branches after staging, the index is a separate area which doesn’t change.

    noufal@sanitarium% git branch
    * master
      test
    noufal@sanitarium% ls
    x
    noufal@sanitarium% git status
    # On branch master 
    nothing to commit (working directory clean)
    noufal@sanitarium% git checkout test
    Switched to branch 'test'
    noufal@sanitarium% ls
    x
    noufal@sanitarium% echo "Something" > b
    noufal@sanitarium% git add b
    noufal@sanitarium% git status
    # On branch test   
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       new file:   b
    #
    noufal@sanitarium% git checkout master
    A       b
    Switched to branch 'master'
    noufal@sanitarium% git status                    # Also there in index on master branch.
    # On branch master 
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       new file:   b
    #
    noufal@sanitarium% git commit -m "Added b in master"
    [master 41d0c68] Added b in master
     1 file changed, 1 insertion(+)
     create mode 100644 b
    noufal@sanitarium% git status
    # On branch master 
    nothing to commit (working directory clean)
    noufal@sanitarium% git checkout test
    Switched to branch 'test'
    noufal@sanitarium% ls                           # Missing in the test branch although it was `git add`ed here. 
    x
    noufal@sanitarium%        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a file that is in specific directory. For example: C:\x\y\z\aTextFile.txt
I need an example pom file to create a jar file for my spring
PhoneGap - How to create a new file in assets/www/example/hallo.text my error is Error
I know that to create a file in c++ we use following code #include
I've notice this. Example: create an empty text file called for example ast.py $
Is there a way to create file template as to add it to a
I have a SQlite3 table that has typeless columns like in this example: CREATE
how to create file names from a number plus a suffix??. for example I
In this example I create three buttons 'one' 'two' 'three'. When clicked I want
I am trying to make a webpage with Ajax. Example: I create a Perl/CGU

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.