What is the fastest way to unstage parts of a new file in git?

When I want to split up a new file into several commits I can git add -N <file> and then interactively stage lines using git gui. When I make a mistake while staging, however, git gui won’t let me unstage individual lines because it is a new file (which seems like a bug to me). Of course I can always unstage the whole file and start over again, but I am wondering whether there is a more efficient way to do so.

I am using git 1.7.5.

  • Gitlab Push Failed error
  • Flatten old history in Git
  • How do I merge back into master without losing my work on github?
  • Exclude files from git svn clone
  • How to import GIT repository into another one, with history?
  • How can I do `git push --recurse-submodules=on-demand` when the parent repo is on the `develop` branch with this new git version 2.13?

  • To clarify, this question is specific to new aka untracked files!

  • Creating a 'push prefix' for git
  • How to configure Mac OS X term so that git has color?
  • Heroku Push Failed
  • How to restart git repo?
  • How do I check git log for a “fast-forward” merge?
  • Jenkins can not clone Git repository over Git/SSH on Windows
  • 3 Solutions collect form web for “What is the fastest way to unstage parts of a new file in git?”

    From the command line type:

    git reset -p
    

    This will let you selectivelty unstage hunks from the index using the standard command-line interface for managing hunks.. This is the opposite of git add -p.

    UPDATE

    OK, it would appear that you cannot selectively stage different hunks when the file is new. Given that git-gui and the standard git hunk editor both do not allow this, it probably isn’t possible.

    Using fugitive you can easily do so. Just invoke the :Gdiff command and then stage and unstage whatever you want. You can even edit the index file directly. Of course, this solution requires you to be fairly comfortable with vim and vimdiff

    I just ran into the same issue: I had just committed a new file, and I wanted to unstage some of the lines in that file and have those as a separate commit. The solution I worked out was:

    • Start on the commit that adds the new file
    • Remove the lines you wanted to unstage in the source file, stage those changes and git commit -m 'REVERTME'
    • Create a new commit that reverts that latest commit: git revert HEAD
    • Use git rebase -i to fixup the first new commit, and optionally reword the revert commit.
    Git Baby is a git and github fan, let's start git clone.