brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git stash with keep-index lets you test only what you staged

Git
~/tips/tip-git-stash-keep-index-test-staged

git stash push --keep-index

Hides unstaged edits so you can run tests on exactly what you are about to commit.

Say you stage half your changes with git add -p, but your tests run against the whole working tree. To know whether the staged half works by itself, stash the other half for a moment.

git stash push --keep-index
make test
git commit -m "Add export button"
git stash pop

--keep-index stashes your changes as usual, but leaves what’s in the index in place. After the first line, your working tree contains exactly what you staged and nothing else. Run your tests, commit if they pass, and git stash pop brings the unstaged work back.

This is also the trick pre-commit tools use to check only the staged version of your files.

One thing I’d watch out for: the stash still contains the staged changes as well. So after you commit and pop, git sometimes tries to apply changes that are already in your commit and reports a conflict, especially when you edited the same lines in both halves. Usually it merges fine, but read the output of git stash pop. If it does conflict, the stash isn’t dropped, so nothing is lost.

New untracked files aren’t stashed by default and stay in your working tree. Add -u if they would affect the test.

Further reading: git-stash documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link