Reputation: 71
Recently, I had to recover my computer and reinstalled ALL software again.
Git is behaving in a manner I am not familiar with.
I did
$ git init
$ git branch -b newBranch
$ git checkout newBranch
After this I did some changes to a couple of files and added a new file.
I DID NOT $ git add -A
NOR $ git commit -m 'msg'
Upon,
$ git checkout master
all changes made on newBranch
reflect on master
:
Upon,
$ git status
all changes show up as un-staged (in red).
Previously, whenever I switched to a branch and did some changes, upon switching to another branch, I would NOT be able to see those changes anymore (staged/un-staged or committed/un-committed).
And only upon running
$ git merge sourceBranch destinationBranch
would those changes show on the other branches. This seems not to be the case
with my Git.
Is it
Upvotes: 1
Views: 80
Reputation: 71
Previous installation misled me as to actual commands and Git capabilities. Must read docs thoroughly.
Upvotes: 0
Reputation: 41975
Short answer: your local changes are not tracked by git until you add
or commit
them. If you switch branch without adding or committing, git does not overwrite these local changes, so you don't lose data too easily.
There are three separate domains:
L
)S
)R
)You have made changes in L
. These do not reflect in S
nor R
, the domains are separated. If you haven't told git to add
(to S
) or commit
(to R
), your changes in L
do not belong to any branch yet, only to the L
domain.
When you checkout
branches, from and to master
and newBranch
, git changes the current HEAD
, that is were local changes would be introduced into S
or R
, if you were to add
or commit
.
When you checkout
branches, git also attempts to reflect the checked out commit (which is the one at the tip of the branch you check out) onto the file system, domain L
. But, to prevent shooting yourself in the foot and losing unstaged and uncommited changes in L
, git does not overwrite these L
changes. Since they're not added or commited, you couldn't retrieve them otherwise.
Note: you may tell git to overwrite such local changes, by passing the -f
(force) flag to checkout
. You'd lose your local changes without warning.
Note also: switching branch while having local changes is not always possible, for example if the branch being changed to would apply changes that would overwrite some of your local changes. This is where I most often use -f
, when I know I want to lose the local changes.
This answer (mine) might help: https://stackoverflow.com/a/2569513/108802
Upvotes: 1
Reputation: 11
It's not broken or anything try adding those unstage and commit again.
Upvotes: 0
Reputation: 95978
So you're now working on newBranch
branch, you made changes but didn't commit anything yet.
Why it's allowed to checkout the master
branch?
Git allows that because switching to it won't override any of your local changes.
However, if you're now working on master
branch and change some files, Git will prevent you from doing that because local changes would be overwritten.
Upvotes: 0