Cyriac George Cyriac George - 1 year ago 112
Git Question

Git checkout doesnt revert files

Using git on mac (sierra); git version 2.8.4 (Apple Git-73)

git checkout <master>

command doesn't revert the file changes which are made on another branch.

Here's what i did.

cd project_folder
git clone -o origin -b master
git branch -b new_branch
git checkout new_branch
<make changes to an existing file>
git checkout master

Am expecting to see a clean version of master without the changes that were made in new_branch. But the changes that i made is carried forward when i switch between branches.

Previously (long time back) this used to work like what i expected. Am i missing something?

I did fair amount of google & stackoverflow searches. Didn't yield a result i was looking for. Closest results that i got are, but they didn't help me much.
2. Git branch switching does not change code folder files

Answer Source

This is the expected behavior, and is what I normally see when using Git. From the documentation for git checkout:

To prepare for working on <branch>, 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>.

If you need to switch branches and your working directory is not clean, here are a couple of options:

1) you can make a temporary commit on your current branch:

git commit -m 'WIP'

I say "temporary" but really this is just like any other commit. The only difference is that when you return to this branch you will amend this commit once you have finished the task via:

git commit --amend -m 'Finished WIP'

2) Stash away your changes via

git stash

Stashing will create two commits, one for the working directory and one for the stage. When you want to bring these changes back you can apply these changes via:

git stash apply
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download