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 7915013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:18:54+00:00 2026-06-03T14:18:54+00:00

I clearly do not understand git at all. This is what I’m getting: git

  • 0

I clearly do not understand git at all. This is what I’m getting:

git branch  (outputs that I'm on master)
git checkout -b foo
echo "next line" >> file (file is an existing file)
git add file (stages)
git checkout master
git status (shows that file has "next line" and is staged!!)
git commit (commits the changes that were staged on branch foo!!)
git checkout foo

Here is the kicker. foo now doesn’t show any changes made to file in the working directory OR staged.

So looks like – any changes you make, including modifying files and staging, happen to ALL branches. and when you COMMIT to a specific branch, those changes are discarded on all other branches except the one you committed on.

Is this actually what is going on? Can someone make this make sense to me? It sounds like completely screwy behavior and clearly I don’t get the design idea that makes this a sensible thing to do.

Edit for explicit example:

$ mkdir element
$ cd element
$ git init
Initialized empty Git repository in /home/dan/element/.git/
$ echo "one" >> one
$ git add one
$ git commit -m msg
[master (root-commit) 36dc8b0] msg
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 one
$ git checkout -b fire
Switched to a new branch 'fire'
$ echo "next line" >> one
$ git checkout master
M       one
Switched to branch 'master'
$ cat one
one
next line
$

Which patently contradicts this from the git pro book:

This is an important point to remember: Git resets your working directory to look like the snapshot of the commit that the branch you check out points to. It adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like on your last commit to it.

  • 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-03T14:18:56+00:00Added an answer on June 3, 2026 at 2:18 pm

    It doesn’t matter what branch you’re on when you add a file, only when you commit it. So if you do this:

    git add file
    git checkout master
    git commit
    

    You have committed the file to the master branch.

    Here’s a complete example, with output. We start with a new repository:

    $ git init
    Initialized empty Git repository in /home/lars/tmp/so/repo/.git/
    

    At this point, we’re on the master branch and we haven’t yet added any files. Let’s add a file:

    $ date > file1
    $ cat file1
    Fri May 11 13:05:59 EDT 2012
    $ git add file1
    $ git commit -m 'added a file'
    [master (root-commit) b0764b9] added a file
    1 files changed, 1 insertions(+), 0 deletions(-)
    create mode 100644 file1
    

    Great, we now have a branch (master) with one commit. Let’s create the new branch:

    $ git checkout -b foo
    Switched to a new branch 'foo'
    $ git branch
    * foo
      master
    $ ls
    file1
    

    Now we’ll add a line to file1.

    $ date >> file1
    $ git status
    # On branch foo
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       modified:   file1
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    

    This shows that the file has been modified, but not yet staged. Let’s stage the file and commit it:

    $ git add file1
    $ git commit -m 'made a change'
    [foo 761bed9] made a change
     1 files changed, 1 insertions(+), 0 deletions(-)
    

    And re-run git status:

    $ git status
    # On branch foo
    nothing to commit (working directory clean)
    

    At this point, the file looks like this:

    Fri May 11 13:05:59 EDT 2012
    Fri May 11 13:07:36 EDT 2012
    

    If we switch back to the master branch, we’ll see the earlier version of the file without the second line:

    $ git checkout master
    Switched to branch 'master'
    $ cat file1
    Fri May 11 13:05:59 EDT 2012
    

    Changes to a file are isolated to the branch on which they were committed.

    In your updated example, this…

    $ git checkout master
    

    …does not generate an error because at this point, the version of ‘one’ in both master and fire is identical. The changes in the working directory would apply equally well to either version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that this response clearly states that this is not possible without a
I have googled this question, but I do not understand clearly what is an
Can someone help me understand the scoping rules in Java? This is clearly not
http://code.google.com/appengine/docs/python/tools/uploadingdata.html is not clearly understand. Where i should call the bulkloader.py or appcfg.py? Should
This is clearly not the case though. My JS : $(.job_charge.item-block).live({ mouseenter: function(){ $(this).find('.edit-and-delete').stop(true,true).fadeIn();
Clearly, when GROUP BY clause used, columns that are not aggregate function should be
I'm trying to understand the python compiler/interpreter process more clearly. Unfortunately, I have not
I want through matlab tutorial but I did not understand it clearly. Could anyone
I understand that the answer to this question may depend on registry settings and
I clearly do not understand jQuery asynchronous execution properly. I have the following issue.

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.