1

I have an empty git repo in which first I create a hello.txt file with content "pulkitsharma". After this I added it to staging and then commited the changes in master branch. Then I create another branch named as "new_branch" and updated the content of hello.txt with "pulkitsharma\npulkitsharma6569@gmail.com" and didn't staged for commit. Now after this when I checkout to master branch the content of hello.txt is updated automatically . Can anyone tell us why this is happening because i think that during checkout to master branch there should be
an error.Why is output of git checkout master is "M hello.txt Switched to branch 'master' "

git init
vi hello.txt
git add .
git commit -m "hello.txt added to master branch"
git checkout -b new_branch
vi  hello.txt
git checkout master
Pulkit Sharma
  • 390
  • 2
  • 14
  • https://stackoverflow.com/a/246298/7976758 – phd Oct 20 '21 at 07:14
  • To avoid this and keep changes on the first branch (so that you have a clean status on the second one after the checkout), commit your changes before switching branches. If you later want to resume working on the first branch and the commit is unfinished work, just undo it with `git reset HEAD^` – Romain Valeri Oct 20 '21 at 07:33
  • Always run `git status` before you do anything permanent. (Technically, commits are only *semi*-permanent, so even if you goof this up, it's OK. It's just that it takes more work to fix up the mistake than it does to *avoid* the mistake in the first place). Many people find that setting up their environment to constantly display (parts of) the `git status` output is helpful. – torek Oct 20 '21 at 19:05
  • To get this kind of constant-status-display in a Unix-style shell, look for things that can do that for that particular shell. Bash and zsh have "style" options that do this, for instance. – torek Oct 20 '21 at 19:06

1 Answers1

2

git checkout is clear:

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>.

So your local modification (unstaged) to hello.txt remains even when you switch back to master.

The OP adds in the comments:

Now I accidently checkout to master branch.
Then instead of showing up error and asking of either commit or stash why does it update the files in master branch (without any extra commit in master branch)

As mentioned in "How to stop git auto-update when switching branches?", you would need to:

  • either commit (git commit -a -m "…") before doing git checkout master.
  • or git stash if you don't want to do a "temporary" commit.
    Later on, do git checkout new_branch && git stash pop to retrieve your uncommitted code.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • https://git-scm.com/docs/git-switch would have the same behavior. – VonC Oct 20 '21 at 06:50
  • Why does the local modifications are commited automatically instead of showing hello.txt file should be staged? – Pulkit Sharma Oct 20 '21 at 07:33
  • @PulkitSharma Why not? It makes sense, it works. What seems to be the problem? – Romain Valeri Oct 20 '21 at 07:38
  • @RomainValeri the problem is when i locally modify(unstaged) hello.txt in branch new_branch and then checkout to master branch why does the hello.txt updated automatically without any commit in master branch. – Pulkit Sharma Oct 20 '21 at 07:45
  • Because git is designed like this. What *problem* does it cause? If you want these changes to **stay** on the first branch, commit them. What's unclear? – Romain Valeri Oct 20 '21 at 07:49
  • @RomainValeri the problem is , suppose i update locally a file when i'm in some branch other than master and now i accidently checkout to master branch . Then instead of showing up error and asking of either commit or stash why does it updates the files in master branch ( without any extra commit in master branch ) – Pulkit Sharma Oct 20 '21 at 08:06
  • That's a matter of practice, I guess. Take the habit of checking your status before switching branches. – Romain Valeri Oct 20 '21 at 08:18
  • @PulkitSharma I have edited my answer to address your comment. – VonC Oct 20 '21 at 08:48