Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git stash to patch with untracked files

Tags:

git

How can I make Git stash with untracked files, push it to patch and restore it in another computer.

git stash save -u feature
git stash show -p > patch
git apply patch

But path hasn't untracked files

like image 787
ButuzGOL Avatar asked Apr 02 '14 16:04

ButuzGOL


People also ask

Does stash work on untracked files?

Another common thing you may want to do with stash is to stash the untracked files as well as the tracked ones. By default, git stash will stash only modified and staged tracked files. If you specify --include-untracked or -u , Git will include untracked files in the stash being created.

How do I make an untracked file patch?

To make the untracked files visible to the git diff command, we staged them (using git add ) and then used the following command to create the patch: git diff --patch --staged; git diff [--options] --cached [<commit>] [--] [<path>...]

Can I push with untracked files?

yes you can push only the files which you have commited before. you can leave the untracked files and adapt them later.


1 Answers

A normal git stash creates a stash bag that consists of two commits: the index, and the work-tree.

Using -u or -a (or their other spellings) creates a three-commit stash bag. The third commit contains (only) the untracked (-u) or untracked-and-ignored / "all" (-a) files, i.e., it omits all tracked files.

If you need this in the form of a patch, the trick is to create two patches:

  • changes to tracked files: git stash show -p (what you have so far), plus
  • the entire contents of the third commit, as a patch.

The easiest way to get the second bullet item is to git diff that third commit against an empty tree. Git always has an empty tree in every repository whose ID is the magic number 4b825dc642cb6eb9a060e54bf8d69288fbee4904. So a diff between that tree, and stash^3, will consist of a series of git patches that add the untracked files:

git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3

You can then simply combine the two patches into one:

git stash show -p > patch
git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3 >> patch

(See the last link above for a way to avoid hard-coding the magic number for the empty tree. Also, if you just want to view the u commit, use git show: git show stash^3, for instance.)

like image 161
torek Avatar answered Oct 26 '22 13:10

torek