开发者

Git: Aren't changes supposed to stay within their branch?

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 som开发者_开发知识库e 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?


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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜