-1

Here's the sequence of commands I had run:

Here, I'm on master.

$ git branch
* master

Creating a file on the master branch, and committing it.

$ echo "hello world, from master." > helloworld.sh

$ git add helloworld.sh
warning: LF will be replaced by CRLF in helloworld.sh.
The file will have its original line endings in your working directory.

$ git commit -m "Adding helloworld.sh"
[master e15289b] Adding helloworld.sh
warning: LF will be replaced by CRLF in helloworld.sh.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 helloworld.sh

$ git status
On branch master
nothing to commit, working directory clean

Creating a new branch called firstbranch.

$ git branch firstbranch

$ git checkout firstbranch
Switched to branch 'firstbranch'

$ ls
helloworld.sh

Adding some code to helloworld.sh:

$ echo "hello world, from firstbranch" >> helloworld.sh

$ cat helloworld.sh
hello world, from master.
hello world, from firstbranch

Switch back to master:

$ git checkout master
M       helloworld.sh
Switched to branch 'master'

Check the contents of helloworld.sh on the master branch:

$ cat helloworld.sh
hello world, from master.
hello world, from firstbranch

This totally surprised me! I thought the changes in firstbranch will not be reflected here until I do a merge. I've not even committed my changes in firstbranch, but it still appears in master branch. What's going on here?

Srikanth
  • 11,780
  • 23
  • 72
  • 92
  • 1
    Because you've not committed changes, and the modified file remain modified. you must commit your changes in order to put modifications to git, or you must stash them. if you use `git status` on both branches you'll notice that file is modified in both. – Jepessen Jun 13 '14 at 13:37
  • 1
    This question would definitely have been asked on Stack Overflow before. If anyone can find a good canonical, please mark this as a duplicate. –  Jun 13 '14 at 13:41

2 Answers2

3

You didn't commit your changes to the file before switching branches. The proper way to do this would have been to

echo "hello world, from firstbranch" >> helloworld.sh
git commit
git checkout master

Highly Recommended Reading

  • Additionally, git prevented you from losing changes. If you run `git status` you will see that helloworld.sh now contains unstaged changes. The state of master has not been affected. – pmr Jun 13 '14 at 13:43
1

Because you haven't committed your changes in firstbranch, when you switched your branch it kept the changes around. Think of any uncommitted changes as a commit on top of the current revision history that hasn't been applied to a specific branch yet. The effect this has is that any branch you change to where there isn't an issue applying the changes to the branch, it shows as a modification to the branch.

If after you created firstbranch you then made changes to that file on master and committed them, when you then made changes on firstbranch but didn't commit them before moving back to master, you might have a conflict that prevents you from changing branches, because again it's like a commit that isn't applied yet, and in that case there would be a merge conflict.

css
  • 944
  • 1
  • 9
  • 27
  • 1
    A dirty working copy ***is not like a commit in any way***, and is not a very good way of thinking about why modifications to a file remain in the working copy when switching branches. –  Jun 13 '14 at 13:45