Code606 Code606 - 1 year ago 63
Git Question

Git - Master branch keeps updating when working on another branch

So I am learning Git here, basically I'm just using a simple .txt file for practice. Now I create the file on my master called file.txt Then I create a new branch called editFile. I switch to this branch and edit the file with a couple more lines of text. I'm adding and commiting this file in the editFile branch. However when I checkout back to master, the changes/edit is appearing in the file.txt on master

How do I get the changes to be only showing in the second branch as pretty much every tutorial has it this way?

For context;

$gedit file.txt (add "This is first line." to file).
$git add file.txt
$git commit -m "Adding first instance of file.txt"

$cat file.txt
This is the first line.

$git checkout -b editFile
$gedit file.txt (add "This is a second line, file edited" to file.)
$git commit -am "Edited file.txt to include new line"

$git checkout master
$cat file.txt
This is the first line.
This is a second line, file edited

However shouldn't the file.txt in master only have the first line as I have not merged the new branch yet? Is there something I'm really missing here.

Any help is greatly appreciated and I thank you in advance.

Answer Source

git checkout -b editFile will fail to change branches if branch editFile already exists try git checkout -B editFile which will OVERWRITE the old editFile branch.