Rose Perrone
Rose Perrone

Reputation: 63506

git stash save -u and -p

I'm trying to split up one commit into five commits. My plan was to use git stash save -p and stash everything except what I want in the first commit. Then commit the changes, git stash apply, stash everything except what I want in the second commit, etc. Yet my original commit contains newly added files, and it appears that I can't select what I want to stash using -p and also stash untracked files using -u at the same time.

$ git stash save -u -p "take 4"
Can't use --patch and --include-untracked or --all at the same time

Is there a way to do this?

Upvotes: 6

Views: 859

Answers (2)

Pistos
Pistos

Reputation: 23792

As mentioned, git add -p is definitely the way to go for the original problem.

Regarding the secondary problem, though:

Can't use --patch and --include-untracked or --all at the same time

In my case, I wanted to include some untracked files, but also use --patch to pick some tracked stuff to stash. Let's call the combination of these "diff A". What I did was do the inverse set first: stash away the code that didn't belong to diff A, and that cleared the way to use git add the_untracked_file and then git stash push to get both tracked and untracked stuff into one stash.

Upvotes: 1

Borealid
Borealid

Reputation: 98459

This is a textbook use case for git add -p.

Do the following:

  • Check out the commit you want to split up.
  • git reset HEAD~. This will "undo" the commit without losing its changes. They will be in the working tree, unstaged.
  • Five times:
    • git add -p (and add the changes you want in this "phase"). You'll need to git add any files which were added in the original commit, when you come to the patch you want them to be part of.
    • Optionally, git diff --staged to see what you're about to commit and/or git diff to see what you're not about to commit.
    • git commit

This will give you five separate commits. You may alternatively use git add -i instead of git add -p. This use case is not an intended one for git stash.

The way this works is by making use of Git's "staging area" (also called "the index"). You have a mess of changes in your working copy. You use git add to "stage" the changes. Then git commit (WITHOUT the -a flag) will only commit the staged changes.

Upvotes: 3

Related Questions