I am working in visual-studio-code and using git. I am a solo programmer in the system I develop. I make all my work in a single git branch and use git just for code safe purposes (actually it was a management demand to use it). I started some programming task, changed several files, and got a more urgent task, then another task and so on. Now it is almost clear that most probably I will not return back to the started and unfinished task. So several files stuck changed and not commited in my editor. I think, maybe I could commit those changes to some another (new) git branch, and remove them from my working branch? What is the right way to make this?
Thank you.
CodePudding user response:
Well you would
- stash your unwanted changes (git stash)
- make and switch to your new branch (git checkout -b "branch_name")
- unstash (git stash pop)
- add & commit (git commit -am "what I should have done in the first place")
- switch back (git checkout main)
I would also remember this experience as a first-hand lesson on why you should use branches more.
CodePudding user response:
You have several options here. Perhaps the easiest would be to git stash
your work. This will tell Git to generate 2 (or maybe 3) commits under the hood which will keep track of what your working directory and stage look like right now, while taking the branch back to the original state of the HEAD commit.
Another option would be to create git add
the files you have changed, and then create a new branch:
# from current branch
git add <all files>
git branch temp_branch
From this point, you may change to some other branch and keep working.
CodePudding user response:
In your case you should use git stash command to save untidy work from your current working directory.
Have a look at the thread below: What is the intended use-case for git stash?