If the uncommitted changes are a mix of untracked and tracked changes
What are untracked changes?
When you created e.g a new file. VSCode e.g displays a U
next to the file in the file explorer.
What are tracked changes?
When you make changes to files that were committed to the repo earlier (in previous commits).
Quick explanation of procedure
So imagine you are on branch A
, but you want to commit only changes to existing files to branch A
, while the newly created file (untracked) should be committed to a new branch B
. It's possible to use stashing with a few tricks, explained step by step.
Add script to .git/config
Inside the .git
folder there is a config
file. Open it up and you will see something like this:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/...
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
change the config file to:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[alias]
stash-untracked = "!f() { \
git stash; \
git stash -u; \
git stash pop stash@{1}; \
}; f"
[remote "origin"]
url = https://github.com/...
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
Now you will be able to use the following command while you are on branch A.
git stash-untracked
You will see that the new file disappeared, if you are using a editor like VSCode (it's now stashed)
While still on branch A stage and commit the changes to the existing files:
git add .
git commit -m "committing tracked changes to current branch"
Next step is creating a new branch B (with checkout -b
you visit it immediately)
git checkout -b newBranchName
When using stash pop
the stashed changes get added to your current branch.
git stash pop
The only thing left is to stage and commit the changes on the new branch B
git add .
git commit -m "created new file"