1

I've been using Git for a few months now. I finally have the hang of committing changes and pushing changes to remotes, but I'm not clear on what happens to a file in a branch. They do not work the way I expect.

Let's say I have file index.html. If I perform the following steps, I would expect to have a completely blank index.html file, but that's not the case.

  1. Working in master branch
  2. Create new branch test
  3. Checkout test
  4. Open index.html
  5. Add "test" to file
  6. Close file
  7. Checkout master branch again
  8. Delete branch test

If I open index.html, that test is still in my file. I expect that, as I never committed the changes to the file, nor merged the test branch into the master branch, my index.html file should revert back to how it was before I created the test branch.

I must be thinking of this wrong. Can someone enlighten me?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Bardo_J
  • 41
  • 1
  • 7

2 Answers2

2

The behavior of git when switching branches is to leave modified files unchanged. This makes it easy to switch to a branch and commit the changes to the target branch. From the docs

Local modifications to the files in the working tree are kept, so that they can be committed to the . (Link)

If you want to get your working area onto the current state of master then you can do the following after checking out master

git reset --hard 
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • thanks. so if i wanted to create a new branch to modify a file, test it and then decide I basically wanted to "undo" everything I did in that branch, I'm using git reset --hard? If so, am I thinking of branches incorrectly or is my work flow suspect? – Bardo_J Jan 13 '14 at 14:45
  • @Bardo_J i don't want to say you're thinking of branches incorrectly. It's more likely that git is just different that the most other SCC systems you've used before. It requires a different way of thinking. Took me prob 3 months to get in the right mindset myself. – JaredPar Jan 13 '14 at 14:52
  • @Bardo_J if you want to undo changes then `git reset` is the command to go to, if you want to change the branch you are commiting to then `git checkout`. But in many ways you can use both commands to achieve very similar ends – JaredPar Jan 13 '14 at 14:53
  • OK! I think figured out why it's not working as I expected. If I create a branch, make a change and don't commit it, my files behave as I've described in my original question. If I stage and commit those changes in a new branch, check out the master branch and then delete the feature branch without merging the changes, my files return to their pre-feature branch state. Hopefully this helps someone in the future. – Bardo_J Jan 13 '14 at 15:14
1

"index.html" will be reverted if you reset local changes with git reset --hard MASTER or with git checkout --force. These operations actually modifies working tree.

Otherwise changes in the working tree will be left.

Take a look at this question to get into terms like "HEAD", "working tree" and so on: Difference between HEAD / Working Tree / Index in Git

Community
  • 1
  • 1
  • Thanks! So what is the point of a branch, then? My understanding was that you could create a branch, make modifications which don't apply to your MASTER branch until you merge them in? – Bardo_J Jan 13 '14 at 14:41
  • Point of branch is nice-to-read-and-remember reference to a specific commit. Maybe in your flow you have forget to do `git commit` to save settings in the modified file? And you're understanding it correctly, this commit will be only in `test` branch and not in `master`. – ДМИТРИЙ МАЛИКОВ Jan 13 '14 at 14:43