So say I create a new branch from master, called branchA. I make changes in few files. I get a new task allotted. I switch to the master branch, create a new branch called branchB. I make changes in branchB, push the code. Later I switch to branchA, I dont see my old changes (I had not added or committed the files). So whats the procedure here, as I'm new to GIT. Should I add/stage or add/stage and commit files in a branch before switching ? Thank you
Asked
Active
Viewed 38 times
-1
-
You should add then commit changes before switching to mater. You can also use `git stash` command to just stash changes. Using `git stash apply` you can bring your stash back. – AminA2 Mar 17 '22 at 05:40
-
1Changes you have not *committed* are *not in Git*. When you switch branches with changes active, those changes continue to be *not in Git*. When you then commit on the other branch, those changes are now "in" the other branch (now they *are* in Git) and so they're backed out (left only in the commit at the end of the other branch) when you switch back. This glosses over a LOT of detail; see [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/q/22053757/1256452) for the details. Note that `git stash` works by making commits. – torek Mar 17 '22 at 08:58
2 Answers
1
Before switching to master, you should add
and then commit
changes.
If you are not willing to make a commit
, you can stash
changes and bring them back later.
To stash changes you can use this command:
git stash
To bring back the changes in your last stash you should use this command:
git stash apply
This command will list all of your stashes:
git stash list
You can use the id of each stash in the list to bring them back.

AminA2
- 188
- 1
- 12
0
You can stash your previous changes to save them for later
to save
git stash
to apply the saved stash
git stash apply
for more details

Sparsh Jain
- 641
- 5
- 16