32

I execute the following sequence of commands:

git init rep
cd rep/
echo '111' > 1.txt
git add 1.txt 
git commit -m '1'
git checkout -b dev
echo '222' > 1.txt 
git checkout master
more 1.txt 

As a result of these commands I see

222

And I do not understand why. As you can see I create and go into the 'dev' branch. I do some changes there but I do not add and do not commit them. Why after going back from 'dev' to 'master' I do see the changes that I did in 'dev'? Shouldn't they stay in dev until I add, commit and merge them back to master?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    Changes are made in the work tree. `git add` stages the changes into the index. `git commit` takes a snapshot of all the tracked files in the index as a commit. A branch is a ref that points to a commit. In your case, the changes are still in the work tree. The branch doesn't know about them yet. – ElpieKay Nov 24 '17 at 11:55
  • Roman, please mark one of the answers as the accepted answers. There are multiple great answers among them. – Natan Oct 26 '22 at 13:38
  • I feel this question deserves hundreds of upvotes – eric May 18 '23 at 15:30

7 Answers7

30

All the untracked files does not get impacted when you move in between different branches. As they belong to your filesystem and, GIT is unaware that to which branch these files belong. So when you commit those files, then GIT is aware of which files belong to which branch. And can remove or add files in working area based upon your branch.

Raman Sharma
  • 1,940
  • 2
  • 11
  • 10
17

This is because you didn't commit your changes to branch dev. So the uncommitted changes is not tied to a parent commit yet.

Do

git checkout dev
git add .
git commit -m "changed 1.txt"

If you want to remove the changes do

git reset --hard master

EDIT

The dev and master branch are pointing to the same commit hash. Have a look inside the folder .git/refs/heads, this is where the branches are stored in seperate files. The content is the commit hash that the particular branch is pointing to. So the branch is simply a pointer to a commit.

In your particular case when you checkout master or dev, they both point to the same commit, and therefore the operation doesn't change the working tree. Otherwise you would get an error. Try changing something on the dev branch now, then you should get an error when git checkout master

smerlung
  • 1,459
  • 1
  • 13
  • 32
  • 3
    Your instructions worked well. Now the two branches have different versions of the file and changes made in the "dev" branch are not visible in the master (as I wonted and expected). However, I still do not understand why the changes that I did in "dev" migrated to "master". Isn't the case that not committed changes should disappear or stay in the original branch? – Roman Nov 24 '17 at 11:45
  • @Roman I have edited the question to answer your question – smerlung Nov 11 '20 at 09:06
11

As other has guided, either commit your changes or stash them. One simple solution is to stash your changes ( i.e., temporary saved) before checkout. example

git checkout -b dev
echo '222' > 1.txt 
git stash
git checkout master
# on return to dev, restore changes via following
git stash pop

now you are on the old state of documents.

Hafiz Muhammad Shafiq
  • 8,168
  • 12
  • 63
  • 121
4
git checkout -b dev
echo '222' > 1.txt 
git checkout master

The changes you operated on the 1.txt file in the lines above are not on any branch because they were not committed (they were not even added to the index).

A Git branch is just a pointer to a commit. When you changed the branch from dev to master you, in fact, didn't change the currently checked out commit. This is why Git didn't need to update the index or the content of 1.txt in the working tree.

axiac
  • 68,258
  • 9
  • 99
  • 134
1
  1. git checkout branch2 , where you did the modifications .

  2. git add .

  3. git commit -m "message"

  4. git push -u origin branch2

Check the local repository ,you will see the modifications only in branch2 and not in the master branch. Your master branch will remain unaffected.

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
0

The same thing happened to me but adding and committing files didn't help. Then I found out that I after creating the branch locally with checkout -b I must also push the branch remotely:

git push origin [name_of_your_new_branch]
Nikolay Shindarov
  • 1,616
  • 2
  • 18
  • 25
0

the same thing happened to me I even committed the changes but it didn't reflect them, then I tried running this command:

git status

and got a message indicating that there is nothing to commit, I even made some changes in my file and got: TRY SAVING YOUR FILE AND THEN COMMIT .

BlackSheep
  • 573
  • 2
  • 7
  • 19