Sometimes I want to take some changes that I’ve made but haven’t yet committed yet and put the into the git stash, which I probably use a lot more than I should. When will you learn to just git commit -mwip
, Caleb?
Either way, Git has my back. git stash --keep-index --include-untracked
is almost the opposite of the above. It creates a stash commit for only files that wouldn’t otherwise be committed: unstaged changes and untracked files.
--keep-index
leaves all changes already added to the index (i.e., staged) intact--include-untracked
stashes files in the working tree that have not yet been tracked by Git, and then runsgit clean
to remove untracked files
The above command is really useful in cases when you want to make a commit of a subset of your working tree. Say you’ve set up the perfect commit and want to run your tests to be sure that it works or you’re about to switch branches but want to take some changes with you.
It occurs to me that this would be a good alias. Maybe I’ll run
git config --global alias.reverse-commit "stash --keep-index --include-untracked"
Here’s where I get in trouble for oversimplifying but hopefully these are helpful definitions and not troublesome
There’s a fair bit of jargon here. Git is a very powerful tool that is really abstracted to too low a level for end-users, which is why so let me attempt to break it down.
- changes refers to any portion of files that Git doesn’t have as part of an existing commit. For our purposes here I’m including files that aren’t known to git (see tracked below)
- committed refers to files that have been added to with
git add
and committed to the repository withgit commit
- Oh no I said a new jargon so also repository (usually) refers to the files within a folder that represents a project, and which have as a sibling a
.git
directory. In simple terms, it’s the root folder of your project if your project uses git. The repository also comprises what Git internally uses to track the changes, files, branches, etc. - index is the data structure that Git uses to track the current state of files that it is tracking. If a file has already been committed, there are changes to that file that have not yet been committed, and some or all of those changes have been added with
git add
, those changes have been indexed. Changes that have not been added are not indexed. - tracked / untracked state of a file refers to whether or not the file has been added to git with
git add
, even if the file has changes or hasn’t been committed yet. The file has been added to the index. - staged / unstaged is similar to tracked / untracked but refers to the state of changes, which might include an entirely new or deleted file but more often means that some lines in the file have been changed.
- working tree is all of the files in your repository besides
.git
- stash is what
git stash
creates commits against. It’s a stack (the data structure of a linear list of items which is Last In, First Out; the opposite of a queue) of commits created when you rungit stash
.