0

Let's say we have two branch "master" and "feature".

Usually, when I am on master branch with some changes, and I want to swtich to "feature" by "git checkout feature". Then I would get the warning "commit the local changes".

But for this Gitlab project that I am working on, this behavior is allowed.

git checkout master   
M       README.md
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

What happened here? And how to prevent this behavior? I cannot live with it because in this way, the separation between branches is gone.

I wonder if it has something to do with the "M" before README.md, or some git configurations, or gitlab.

david z
  • 7
  • 3

1 Answers1

0

This message means you have made an edit to README.md that wasn't commit yet. You can confirm exactly what changed by running git diff. For example:

$  git status
On branch turnaround
nothing to commit, working tree clean
$ echo " " >> README.md
$ git status
On branch turnaround
Changes not staged for commit:
    modified:   README.md

no changes added to commit
$ git checkout master
M   README.md
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git diff
diff --git a/README.md b/README.md
index 7ff15db09bc6..fae151008c97 100644
--- a/README.md
+++ b/README.md
@@ -12,3 +12,4 @@ This project will provide a reasonable estimate of the Opensource.com editorial
 - Notes from my readme file lalala
+

That last + shows the change, which is the blank space I added to the file.

The way git works preserves uncommitted changes between branch changes. This ensures you don't accidentally erase information you meant to commit somewhere. Two options:

  • Use git stash to remove this change but be able to access it later
  • Run git reset --hard which will delete the change and reset you to the last commit on this branch (aka $HEAD)

And if you want to ensure you're in sync with the upstream master (version on GitLab), use: git reset --hard origin/master

mbb
  • 3,052
  • 1
  • 27
  • 28