I have been trying to set-up repository with gitlab CE, as a part of the set-up created a repo and been playing around with it, when I encountered, that after I make some modification to files and switch branch using checkout, I'm allowed to switch even though I have unstaged files, which was different from my previous experience that I was not allowed to checkout until I either did a commit or stash.
This experience beats the purpose of seamlessly switching branches without having to worry about unintended creeping in.
The steps are highlighted below.
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ ls -ltr
total 1
-rw-r--r-- 1 somasundaram.s 1049089 13 Apr 4 16:28 README
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ git branch new-branch
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ git checkout new-branch
Switched to branch 'new-branch'
somasundaram.s@user /d/projects/repositories/newrepo (new-branch)
$ touch newfile
somasundaram.s@user /d/projects/repositories/newrepo (new-branch)
$ ls -ltr
total 1
-rw-r--r-- 1 somasundaram.s 1049089 13 Apr 4 16:28 README
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:37 newfile
somasundaram.s@user /d/projects/repositories/newrepo (new-branch)
$ git checkout master
Switched to branch 'master'
somasundaram.s@user /d/projects/repositories/newrepo (master)
$ ls -ltr
total 1
-rw-r--r-- 1 somasundaram.s 1049089 13 Apr 4 16:28 README
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:31 hi
-rw-r--r-- 1 somasundaram.s 1049089 0 Apr 4 16:37 newfile
Its not a bug.
The is how git behave.
In the following diagram you can see the
3 states
.Git has three main states that your files can reside in:
They are all shared between your branches. but when you checkout branch you change the
HEAD
so you end up with thestaging area && working directory
shared between your repository even when you checkout branches.How to checkout different branch with clean working directory and stage area?
If you wish to checkout clean branch without any "leftovers" in your working directory and staging are you can create a new
worktree
which will result in shared view of your repository (all the content is shared) but with a different working directory and staging area.From git v2.5
Now do whatever you want in any of your branches. It will create 2 separate working folders separated from each other while pointing to the same repository.
Using wortree you don't have to do any
clear
orreset
in order to remove all your staged and untracked content.Here is demo of how to do it:
That's a feature, not a bug, and has been that way ever since, as far as I know. If git thinks that it can safely carry along your local modifications, it does so.
If you want to get rid of them,
git reset --hard
.Git won't change files that are not currently tracked in the repository. In your example, you only created an untracked file (
newfile
).So the Git's behavior is absolutely normal.
If you
git add newfile
without commiting the changes, Git won't allow you to switch to master branch.For example, this will be handled by Git:
For in depth explanations: https://stackoverflow.com/a/8526610/882697