1

I am aare, i forgot how to GIT. Accidentally i removed all untracked files from my branch and the only way was to clone repo again. After having completly clean repo with master branch I started to create new branch...

... but as I have mentioned, probably i forgot how to GIT.

I am creating branch by

git checkout HEAD~1 -b personal/$USER/featureX2

and then

git pull origin master

After doing some changes in script on that branch and saving them in eclipse (then u see in git status with red font what should be added to commit), I shouldn't be able to checkout back to master without committing it. But...

I checkout without the problem, even more, the unsaved changes are also on the master branch as I see in a git status.

What I am doing wrong? Or when I should not be able to checkout to another branch without comitting my change?

Elcardia
  • 135
  • 10
  • What do you mean by "saving them on that branch"? – choroba Dec 14 '16 at 12:11
  • I have editted it – Elcardia Dec 14 '16 at 12:19
  • If you have modified a file in your branch in a commit that master doesn't have, you won't be allowed to switch since master has newer version of the file that conflicts. In case your modified file is ahead of the newest version of the master then you will be able to switch. This is probably a duplicate of http://stackoverflow.com/questions/8526279/git-allows-for-branch-change-with-unstaged-changes . – afxentios Dec 14 '16 at 12:39

2 Answers2

2

Git is behaving as expected here. Git doesn't track the Untracked files (red font ...). So, when trying to checkout master, it prevents as there may have changed in same file/lines (conflicts).

And when doing checkout from one branch to another, untracked files goes along with.

You can commit the changes or save the untracked files temporary (stash).

$ git checkout branch-1
# do some changes here

$ git add .
$ git stash save 'my-temporary-change'  # stash -> save the changes in temporary box
# now untracked files are gone and save to stash stack

$ git checkout master

# if you need to take the temporary changes, just by 'stash pop' you can get them back in any branch
$ git stash list       # see all stash stack list
$ git stash pop        # pop the last one from stash stack, pop = apply + drop
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
2

Actually Checking out from one branch to another is allowed when the change are not committed most of the time ,except that there may by a conflict.
If you did that,successfully,then because your working directory is not clean,you can see that changes at every branch.
Once you commit them on one branch,you will only see the changes on that branch~

disinuo
  • 75
  • 9
  • 1
    +1 This explains what just has happened to me. Once I've committed my changes back on the branch, changes were no more visible on the master. – developer10 Feb 22 '19 at 11:53