Reputation: 93
Sorry if this question is ignorant I have just started using Git. Yesterday I created a local repository with a master branch and then created a new branch called 'firstbranch'. I would do some practice work in 'firstbranch' then add/commit the changes and then merge it into the master branch. Which is the idea. However, today I picked up where I left off but now, I am making changes in 'firstbranch' and without merging or even committing, it is making the changes in master. Is there something I am doing wrong, or is this supposed to happen? I would obviously like to make the changes JUST to 'firstbranch' without updating master in case I decide to scrap the work.
Upvotes: 0
Views: 1065
Reputation: 45819
In git there are three distinct types of storage: the work tree, the index, and the database. It's very important to understand how these interact.
In general, the work tree is where you directly interact with the files (i.e. you see them in file explorer or in your IDE; you work on them in your text editor). At the top of the work tree there's usually a .git/
directory, which contains (among other things) the index and the database. The index is where changes are staged (by git add
). The database contains your project history (it's where commits and refs live).
In particular, branches (which are a type of ref) exist in the database and nowhere else. (The index and work tree can be "checked out to a branch", but that's more a status of the work tree than anything that concerns the branch itself.)
What I think you must be observing is that you make some changes, then (without committing them) check out a different branch, and the changes are still present. This is because they are on the work tree. They don't yet belong to any branch. In general checking out a branch may require git to apply changes to the work tree, and if those changes would affect files that have local changes then the checkout will be rejected; but if the checkout is allowed, then the work tree changes are brought along. This is meant to be for your convenience, though it can be confusing.
If you want to get your local work-in-progress "out of the way" so you can check out another branch exactly as it is in the database, you can either
(1) use git stash
to put the local changes into temporary commits from which you can retrieve them later (with git stash pop
), or
(2) use git worktree
to check the other branch out to a separate filesystem path
You also could use various options with git show
and git diff
to examine a given branch's head commit (among other things). I suggest reviewing the git documentation for these commands.
Upvotes: 1