2

I am relatively new to using Git. This is what I've done so far:

$ git branch
* master

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

$ git branch mywork
$ git checkout mywork
$ git branch
* mywork
  master

...modify some files...

$ git status
# On branch mywork
# 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:   file1.html
#   modified:   file2.html
#
no changes added to commit (use "git add" and/or "git commit -a")

Now if I switch to my other branch, I expect my changes to stay within this branch. However, it looks like the changes come with me:

$ git checkout master
M   file1.html
M   file2.html
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:   file1.html
#   modified:   file2.html
#
no changes added to commit (use "git add" and/or "git commit -a")

Am I doing something wrong? Or am I misunderstanding my use of Git?

Andrew
  • 227,796
  • 193
  • 515
  • 708
  • possible duplicate of [Why git keeps showing my changes when I switch branches (modified,added, deleted files) no matter if I run git add or not?](http://stackoverflow.com/questions/5531362/why-git-keeps-showing-my-changes-when-i-switch-branches-modified-added-deleted) – Chris Johnsen Jun 09 '11 at 05:08

4 Answers4

5

Your working directory and your repository are separate from each other, in the sense that code in your working directory is not associated with a branch until it is committed.

To further muddy the waters, git has introduced a new concept called the index, which is a staging ground between your working directory and repository. When you run git add on a file, what you're really doing is adding it to the index. Likewise, when you run git commit, you finally add the contents of the index to the repository.

git status distinguishes between the index and the working directory as follows.

  • Changes to be committed: indicates changes in the index
  • Changed but not updated: indicates changes in the working directory to files that have already been added to the repository (and not subsequently ignored)
  • Untracked files: indicates changes in the working directory to files that have not already been added to the repository

If you have a few changes that you want to save for the moment but that don't warrant their own branch, use git stash. This is git's mechanism for creating an extremely lightweight, single-commit branch. From the man page (emphasis is mine):

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Chris Frederick
  • 5,482
  • 3
  • 36
  • 44
3

Before switching back to your master branch, you need to add and commit your changes to the branch.

Use:

git checkout mywork
# Modify files

git add . # Adds all edited files to the repository
git commit -m "Message about the changes"

git checkout master

You can find lots of information about Git at The Git Book

ardavis
  • 9,842
  • 12
  • 58
  • 112
2

In somewhat plainer english than other (correct) answers: git checkout doesn't stomp files it doesn't know about. Since you haven't done git add on file1.html and file2.html, git doesn't know about them, and so doesn't remove them when you change branches.

If you were to add and commit them on branch mywork, they would go away when you check out master, and come back when you check out mywork.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87
1

that is because you haven't committed them in the mywork branch. once you commit them and checkout the master branch you will not find that changes

you will find this SO post very helpful

Community
  • 1
  • 1
Ahmed Kotb
  • 6,269
  • 6
  • 33
  • 52