Getting to grips with git – the stash, working directory, index, local repo, remote repo

When starting git it’s really helpful to know there are a bunch of locations your source can exist. These are the:

  •  stash
  • working directory
  • index
  • local repo
  • remote repo

I don’t know if this cheat sheet helps or just overwhelms but some may find it useful: http://www.ndpsoftware.com/git-cheatsheet.html#loc=workspace

 

git stash

The docs say:

git-stash - Stash the changes in a dirty working directory away

https://git-scm.com/docs/git-stash

but there are plenty of ambiguities here.

stash

What exactly does stash mean?

And what’s a dirty working directory?

Say you’ve got this:

in your current directory your gut reaction might be that it will store your changes to both A and B. After all you’ve changed both and they’re both in your working directory.

That’s not the case:

B is ignored. You need to add it to the repo to stash it.

To stash B, use git stash -u

 

Note: a dirty working directory would seem to indicate a changed file in the current working directory. It isn’t.

dirty state means modified tracked files and staged changes

https://git-scm.com/book/en/v1/Git-Tools-Stashing

working directory is a repository created with the git init command.

https://stackoverflow.com/questions/36201342/git-where-exactly-is-the-working-directory

https://stackoverflow.com/questions/53744931/clarifying-the-working-directory-vs-current-directory-in-git

 

Useful discussions:

https://stackoverflow.com/questions/20642980/does-git-dirty-mean-files-not-staged-or-not-committed-glossary-conflict

 

View stash

List stashes with:

git stash list

View contents of stash with:

git stash show -p stash@{0}

 

Apply stash

Apply stash with:

git stash apply stash@{0}

Note that this won’t remove the stash from the list.

 

If you attempt to stash it again, you’ll just add another identical entry into your stash list.

To remove it from the list use:

git stash drop stash@{0}

 

https://git-scm.com/book/en/v1/Git-Tools-Stashing

 

Leave a Reply

Your email address will not be published. Required fields are marked *