0

I create a branch from my master. However, when I checkout into the branch and make changes, these changes appear in my master branch when I checkout back to master. Another very weird thing is that git does not force me to commit when I make changes and do checkout. How can I have a branch that is independent from the master?

I have created many branches before, but I have never run into this problem before.

Punisher
  • 654
  • 5
  • 21
  • This is because *branch names* aren't really important in Git. What matter are *commits*, and you make commits from the *index* (aka staging area). Your work-tree is there for you to be able to see and work with files, but these are not the files that Git commits—those files are in the index! None of this makes any sense until you really grok this index thing. See also https://stackoverflow.com/q/22053757/1256452 – torek Mar 07 '20 at 09:16

1 Answers1

1

As long as you don't commit the changes, they will come with you to other branches that you checkout (either on the working tree or on index if you have already added something there).

Now, this question, I don't understand what you mean: How can I have a branch that is independent from the master? Just in case, you can have a branch that is completely independent of master by creating an orphan branch.... but I'm not sure that is what you are asking about.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • How can I change git so that it requires me to commit my changes before switching branches (I had this behavior before, but this repository is behaving differently)? Sorry for the ambiguity in my question. By independent, I want my changes in the branch not to move over to the master when I switch branches. – Punisher Mar 07 '20 at 04:55
  • Ok.... I don't think git forces you to commit in order to move. Git will _refuse_ to switch to a different branch if the file that is modified and uncommited on your working tree is different between HEAD and what you want to checkout. In other words, git does not want to attempt to have to go through a merge on uncommitted code. But that's about it.If that file is exactly the same between HEAD and the branch or revision that you are asking git to checkout, git will checkout and bring over the changes that you have on your working tree.... which makes lots of sense. – eftshift0 Mar 07 '20 at 04:57
  • And your changes will stay on that branch when you switch to master _when you commit_. As long as you don' t commit, git will allow you to move along with them anywhere you go. If you would like to put those changes somewhere so that you can work on a clean master, you should consider using the stash. – eftshift0 Mar 07 '20 at 05:01
  • Got it. Thanks for the explanation! – Punisher Mar 07 '20 at 05:02