Reputation: 31365
I'm still experimenting with Git.
Right now I have only a local master
branch and a remote origin/master
branch on Gitlab.
What I've been doing so far:
master
git commit -m "added something new"
git push origin master
I wanted to create a new branch, so I did:
git checkout -b newBranch
After this command I'm already "checked out" on my new branch, right? And I could confirm that with git branch -a
So I created a new file (using VSCode file explorer) example.txt
(on my new branch).
And then I did:
git checkout master
So I could go back to my master
branch.
And at that point I was expecting that I would no longer be able to see the file, since it was created using a different branch. But the file is still there.
What am I missing?
Upvotes: 1
Views: 13241
Reputation: 547
Another Possible Solution:
You can use stash command of git.
What git stash do?
git stash is used to clipboard for your changes.
If you have to switch context - e.g. because you need to work on an urgent bug - you need to get these changes out of the way. You shouldn't just commit them, of course, because it's unfinished work.
Example:
git stash
Your working copy is now clean: all uncommitted local changes have been saved on this kind of "clipboard" that Git's Stash represents. You're ready to start your new task (for example by pulling changes from remote or simply switching branches).
Lets say:
Now urgently you need to switch another branch master then before switch just apply following command.
git add example.txt
git stash
Now when you switch master. Then example.txt file will disappear.
When you go back your branch newBranch then apply following command.
git stash apply
then you can see you file example.txt.
Upvotes: 0
Reputation: 45659
The file you created has not been staged or committed, so it is an untracked file. Untracked files do not yet belong to any branch(es); they exist on your working tree and remain there when you switch from branch to branch.
(To fully understand this, it may help to familiarize yourself with the three general storage areas in git - the worktree, the index (sometimes called staging area), and the database. Going into detail about that would kind of be out of scope for answering this question, but I do recommend reading up on this topic.)
There is a word of warning that goes with that: a file that is committed on one branch might not exist on another branch. For example, if you go back to newBranch
and commit your file
git checkout newBranch
git add example.txt
git commit
now example.txt
exists on newBranch
but not on master
. If you check out master
, the file will go away as you expected.
That means that if, on master
, you create another example.txt
(in the same directory so that the path is exactly the same), this new file is untracked even though the same path refers to an existing, committed file on another branch.
If you then tried to checkout to newBranch
git should refuse with a warning that you have data which would be lost by checking out.
Really this is just one special case of "trying to change branches with uncommitted changes", but it is worth being aware of given your current task.
Upvotes: 5