3

Is it necessary to commit to git repository before checkout? I've heard it is, because I can lose the changes. However, after modifying a file and checking out, I could easily come back to my previous branch and I didn't lose any work. The file had M status.

They state here that M means Merged (that's weird, I think it notifies me about modifications).

$ git checkout master
M   README
Switched to branch 'master'
$ cat README
This is the README file.
This line was added in the working directory while in the test branch.

From 'Git Checkout Without Committing: WITHOUT A Conflict' section. What does M README mean?

Thanks for help.

user107986
  • 1,461
  • 1
  • 17
  • 24

1 Answers1

2

A git checkout won't process if any current modification could be erased.

https://git-scm.com/book/en/v2/book/01-introduction/images/areas.png

(image from "Getting Started - Git Basics")

The example mentioned actually uses git checkout -m (and no commit), which means the modification is merged:

-m
--merge

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context.

However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

It is best to commit (or to stash) current changes before a checkout, but checkout -m can at least merge those changes instead of failing (or erasing them in case of checkout -f (see also "What's the difference between “git reset” and “git checkout”?").

What does M README mean

That is the unstaged (and uncommitted) modification, the work in progress.
A git checkout -m allows for that modification to be merged in the destination branch, instead of making a simple git checkout fail.
See also "What’s In The Index? What’s Changed?".

https://git-scm.com/figures/18333fig0201-tn.png

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @user107986 that is the idea: add a conflict in the test branch before a `git checkout -m master`: the change is merged, and the merge conflict is shown. – VonC Oct 19 '15 at 20:43
  • Deleted comment and put it in the question. – user107986 Oct 19 '15 at 20:44