7

I am still in the process of learning Git and when I felt I have mastered it all..I encounter a problem which makes me feel a novice again...

I have a master and a branch.For some 'God only knows' reason when I perform "Git status" on both the branches they are showing the same changes.

And when I commit these changes to one of them..I don't see them in the other..

Master*: Git status
On branch Master
Changes not staged for commit:
      modified:   Config/A.txt 
Untracked files:
       logs/B.txt


**Branch-1*: Git status**
On branch Branch-1
Changes not staged for commit:
      modified:   Config/A.txt 
Untracked files:
       logs/B.txt   

you be the God,for a while, :)

raj'sCubicle
  • 350
  • 2
  • 13
  • 1
    [Please don't put tags in question titles](https://stackoverflow.com/help/tagging) – Liam Jun 02 '17 at 14:14
  • You *have not* committed these changes. You have not even *staged* them *for* commit. – torek Jun 02 '17 at 14:14
  • I hope you won't take this the wrong way, but this question indeed suggests a relative novice to git. The description of what git will do, given by Edmundo, is accurate; but what you really need to do is familiarize yourself with git concepts like how the work tree, index, and database relate to adding/staging, committing, branches, etc. Internally git isn't like most version control systems, and until you learn its fundamentals its behavior will be very mysterious. – Mark Adelsberger Jun 02 '17 at 14:48
  • @torek do you mean that if any file changes made,when Branch-A is checked out, and not staged yet, will be available for Branch-B to stage and commit as well?..Sry I am coming from a TFS background and I am not yet tuned to GIT architecture.. – raj'sCubicle Jun 02 '17 at 14:49
  • It's more complicated than that, because there are, at all times, *three* "current" versions of every file available: the `HEAD` commit version (which is read only, you cannot change it), the *index* or *staging area* version, and the work-tree version. Various commands can copy *from* any committed version *to* the index and/or work-tree, or copy from *from* the index to work-tree or work-tree to index. `git checkout` ... is complicated (much too complicated to fit in a comment!). – torek Jun 02 '17 at 15:07
  • However, from a high level view, `git checkout ` means "switch from the current commit to the tip of ". This means that the commit named by `HEAD` *may* change (depending on which commit is the tip of ). If the commit named by `HEAD` changes, some files in the index *may* have to change; if those have to change, the `git checkout` step will also have to change the corresponding files in the work-tree. If so, the checkout will abort *if* the index and/or work-tree have uncommitted changes *to those files*. – torek Jun 02 '17 at 15:10
  • For the detailed explanation, see https://stackoverflow.com/q/22053757/1256452 (but take note of the first bit and don't worry if the rest of it seems overly complicated: basically, you can just try the `git checkout` and see if it succeeds). – torek Jun 02 '17 at 15:11

2 Answers2

9

I guess you are switching from one branch to the other and running status, right? When you have uncommitted changes on your work tree and you ask git to switch, git will bring over the changes to the other branch you check out (at least, it tries to). There are some checks it performs and if the changes can't be applied correctly on the other branch, the switch is interrupted.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 'There are some checks it performs and if the changes can't be applied correctly on the other branch, the switch is interrupted'..can you give an example of this.. – raj'sCubicle Jun 02 '17 at 14:31
  • @raj'sCubicle - If the checkout would modify a file in which you have unstaged changes, for example, the checkout will error out by default – Mark Adelsberger Jun 02 '17 at 14:49
5

If you don't make a commit and you go to checkout master (or other branch) git will show you the same changes in both branch. But when you do the commit, these changes won't show for git in other branch, just in the branch where you are working.

  • Why though this seems terrifying? I thought the whole point was local work in branch, and when I checkout another branch everything is encapsulated to that branch? Why is everything carrying over? – eric May 18 '23 at 15:28