DanielB
DanielB

Reputation: 197

Changes in one branch appear in master

I´m new to git and I´m having this weird thing. I checkout to a branch, do some changes, then I checkout to master and the changes I made in the other branch are there. I´m not sure if I misunderstand the concept or I´m doing something wrong. The git version is 1.7.11-preview20120710 from msysgit.

This is a sequence of commands I made, I remove the file text.txt in mybranch, then in master the file is removed too.

db@DB-PC /f/Projects/Controls (master) 
$ ls
AcNumericSingleToolStripTextBox.vb  NumericSingleTextBox.vb

db@DB-PC /f/Projects/Controls (master)
$ touch text.txt

db@DB-PC /f/Projects/Controls (master)
$ git checkout -b mybranch
Switched to a new branch 'mybranch'

db@DB-PC /f/Projects/Controls (mybranch)
$ ls
AcNumericSingleToolStripTextBox.vb  NumericSingleTextBox.vb  text.txt

db@DB-PC /f/Projects/Controls (mybranch)
$ rm text.txt

db@DB-PC /f/Projects/Controls (mybranch)
$ ls
AcNumericSingleToolStripTextBox.vb  NumericSingleTextBox.vb

db@DB-PC /f/Projects/Controls (mybranch)
$ git checkout master
D       Controls/text.txt
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 5 commits.

db@DB-PC /f/Projects/Controls (master)
$ ls
AcNumericSingleToolStripTextBox.vb  NumericSingleTextBox.vb

Text.txt is not in master anymore.

Edit for depot

I had the problem before creating that example text.txt. Now I renamed AcNumericSingleToolStripTextBox.vb to NumericSingleToolStripTextBox.vb in mybranch, and I can see the change in master:

db@DB-PC /f/Projects/Controls (master)
$ ls
AcNumericSingleToolStripTextBox.vb  NumericSingleTextBox.vb

db@DB-PC /f/Projects/Controls (master)
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 7 commits.
#
nothing to commit (working directory clean)

db@DB-PC /f/Projects/Controls (master)
$ git checkout -b mybranch
Switched to a new branch 'mybranch'

db@DB-PC /f/Projects/Controls (mybranch)
$ ls
AcNumericSingleToolStripTextBox.vb  NumericSingleTextBox.vb

db@DB-PC /f/Projects/Controls (mybranch)
$ mv AcNumericSingleToolStripTextBox.vb NumericSingleToolStripTextBox.vb

db@DB-PC /f/Projects/Controls (mybranch)
$ ls
NumericSingleTextBox.vb  NumericSingleToolStripTextBox.vb

db@DB-PC /f/Projects/Controls (mybranch)
$ git checkout master
D       Controls/AcNumericSingleToolStripTextBox.vb
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 7 commits.

db@DB-PC /f/Projects/Controls (master)
$ ls
NumericSingleTextBox.vb  NumericSingleToolStripTextBox.vb 

Upvotes: 3

Views: 1066

Answers (1)

Brian Ustas
Brian Ustas

Reputation: 65499

In your example, Git was never told to track text.txt.

Before you checkout of master, you must tell Git to track it.

$ git add text.txt

Once text.txt is staged, commit the addition to the branch.

$ git commit -m 'Added text.txt'

While on mybranch, tell Git that you want to remove text.txt. Associate that change with mybranch by committing the change.

$ git rm text.txt
$ git commit -m 'Removed text.txt'

After checking out to the master branch, Git will restore its state and text.txt.

Upvotes: 4

Related Questions