gerrit
gerrit

Reputation: 26465

Why do local changes seem to exist equally in both branches?

I have different branches in my git repo. I checkout a new branch and apply a change. When I checkout back to my master branch, it still shows the change. Similarly, when I stage changes for commit in the new branch, then checkout the master branch, it again shows those changes as staged.

Why do changes in my branch regrid-more-flexible-times seem to be shown (either staged or unstaged) even when I switch back to my branch master? See below for an illustration.

$ git branch
* master
  regrid-more-flexible-times
  regrid-sst
$ git checkout regrid-more-flexible-times
Switched to branch 'regrid-more-flexible-times'
$ git status
# On branch regrid-more-flexible-times
nothing to commit (working directory clean)
$ vim regridworkflow.py
$ git status
# On branch regrid-more-flexible-times
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   regridworkflow.py
#
no changes added to commit (use "git add" and/or "git commit -a")

So far so good. I've made changes in my branch regrid-more-flexible-times. Now I need to head back to master to access the codebase without any changes.

$ git checkout master
M       mms/src/main/python/regridworkflow.py
Switched to branch 'master'
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   regridworkflow.py
#
no changes added to commit (use "git add" and/or "git commit -a")

Huh? Why does branch master show the unstaged changes I made while on branch regrid-more-flexible-times? Perhaps it is only aware of differences after I stage them?

$ git checkout regrid-more-flexible-times
M       mms/src/main/python/regridworkflow.py
Switched to branch 'regrid-more-flexible-times'
$ git add regridworkflow.py
$ git status
# On branch regrid-more-flexible-times
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   regridworkflow.py
#
$ git checkout master
M       mms/src/main/python/regridworkflow.py
Switched to branch 'master'
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   regridworkflow.py
#

...and why does branch master show the changes I staged while on branch regrid-more-flexible-times?

My aim is to be able to work an changes while on branch regrid-more-flexible-times, but to be able to checkout the master branch which does not have those changes. Why does the workflow above not do so, and how should I do instead to achieve this?

Upvotes: 0

Views: 100

Answers (2)

CodingWithSpike
CodingWithSpike

Reputation: 43718

Per the documentation:

git checkout <branch>

To prepare for working on , switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

This is why your unstaged changes remain when you checkout master.


You would either want to commit your changes to the branch then checkout master, or use git stash to save off the changes without committing them.

Upvotes: 0

choroba
choroba

Reputation: 241858

I've made changes in my branch regrid-more-flexible-times.

No, you've made changes to your wokring copy.

why does branch master show the changes I staged while on branch regrid-more-flexible-times?

There's only one staging area per working copy. They don't exist for each branch. See git stash on how to save changes without committing them.

Upvotes: 4

Related Questions