Reputation: 1995
I have a file in my master git branch called 'Readme.' The file has the following text:
This is the master branch
I made a topic branch called 'justatest' and edited the 'Readme' file inside of it. I edited this file (while the justatest branch was checked out) to say the following:
This is the master branch This text should only be seen in the 'justatest' branch
When I am in the 'justatest' topic branch it appears correctly, but when I switch to the master branch, here's what the file says:
This is the master branch This text should only be seen in the 'justatest' branch
As you can see, it's making the changes in the master branch even when I have a topic branch checked out! WHY???
The worst part is that if I don't like the changes I have made in the topic branch and I delete it, the changes REMAIN in the master branch!! Where's the safety in that???
Upvotes: 2
Views: 68
Reputation: 129782
When you make edits, they are not part of any branch. These changes are only in your working folder. Changing branches while you have some outstanding work not yet committed is allowed - as long as they don't conflict with the committed version of the files.
In your case it's even simpler. Since you just made the branch, both master and this new branch point to the same commit. Once you add and commit your changes, switching branches will have the behaviour you are expecting.
git add -A
git commit -m "my changes"
git checkout master
your file will be what it was before you branched.
You can flip between 2 last branches you had checked out by repeatedly doing
git checkout -
this is also true of cd
.
Take a look at http://progit.org/book for info about the working directory and index.
Upvotes: 1
Reputation: 18531
If you commit the changes in the topic branch, then you can checkout the master branch and find your changes are only visible in the topic branch until you merge them back to master.
Example:
[matthewh@folio temp]$ git branch
* master
[matthewh@folio temp]$ cat Readme
This is the master branch
[matthewh@folio temp]$ git checkout -b justatest
Switched to a new branch 'justatest'
[matthewh@folio temp]$ echo "This text should only be seen in the justatest branch" >> Readme
[matthewh@folio temp]$ git commit -am "Modified readme"
[justatest 1afc6c7] Modified readme
1 files changed, 1 insertions(+), 0 deletions(-)
[matthewh@folio temp]$ git branch
* justatest
master
[matthewh@folio temp]$ cat Readme
This is the master branch
This text should only be seen in the justatest branch
[matthewh@folio temp]$ git checkout master
Switched to branch 'master'
[matthewh@folio temp]$ cat Readme
This is the master branch
[matthewh@folio temp]$ git merge justatest
Updating 88aa000..1afc6c7
Fast-forward
Readme | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[matthewh@folio temp]$ cat Readme
This is the master branch
This text should only be seen in the justatest branch
Upvotes: 1