15

When I am splitting a commit during interactive rebase, I often would like to extract specific files from the last commit.

My current process is to

  1. Copy the last commit message to my clipboard,
  2. git reset --soft HEAD^ (last commit is undone, changes are staged)
  3. Unstage files I wanted extract
  4. Re-commit (pasting in the copied commit message)
  5. Add/commit the remaining files
  6. Continue with rebase

I feel like this would be simpler if I could soft reset a specific file.

4 Answers 4

15

Are you trying to do git checkout $COMMIT_HASH some_file.ext?

You can use this to revert a file to its previous state, and git stages this change.

Sign up to request clarification or add additional context in comments.

3 Comments

This in combination with git commit --amend would get me halfway there, except the change itself is lost. In short, I want to split changes out of the previous commit without having to reset the whole commit.
No, checking out a single file does not wipe out your other changes. It just reverts that one file.
The only apparent problem (?) is that git checkout touches the work tree while git reset $commit -- $path does not.
10

It seems this does what you want :

git reset [-q] [commit] [--] paths… This form resets the index entries for all paths to their state at commit. (It does not affect the working tree, nor the current branch.)

This means that git reset is the opposite of git add .

1 Comment

One should add that this (git reset -- path) step is to be done after the git reset --soft HEAD^. The idea here is to step HEAD back one commit without changing anything in the index or work-tree, then to make one change to the index (but not the work tree) using the new, stepped-back HEAD.
0

I am not sure it's an optimal solution.

git reset HEAD~ git add . git commit (optional) git reset --hard HEAD 

Comments

0

An easy way could be

  1. Add all files

    git add .

  2. Now reset the files you do not want to commit

    git reset 'file1.ext' git reset 'file2.ext' ... so on

  3. Continue your commit and push

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.