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

前端 未结 4 1785
旧巷少年郎
旧巷少年郎 2021-02-12 10:36

When I want to split up a new file into several commits I can git add -N and then interactively stage lines using git gui. When I make a m

相关标签:
4条回答
  • 2021-02-12 11:14

    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...

    0 讨论(0)
  • 2021-02-12 11:21

    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.

    0 讨论(0)
  • 2021-02-12 11:30

    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.
    0 讨论(0)
  • 2021-02-12 11:33

    Another easy way to sort this is to cut everything out of the file except for one line that you want in the first commit. Stage it and then add back the rest of the file. From then on you can stage hunks/lines as usual

    0 讨论(0)
提交回复
热议问题