Home > Back-end >  Git : creating new branch while working on another branch will erase all changes?
Git : creating new branch while working on another branch will erase all changes?

Time:03-17

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related