Git invert staging area

后端 未结 4 1490
滥情空心
滥情空心 2021-02-13 16:14

I have got changes in my staging area, and others not staged yet (some files have changes both in and out the staging area). I would like to invert the content of the st

4条回答
  •  囚心锁ツ
    2021-02-13 16:41

    There’s probably more than one way to do this, but I think I would take this approach – there’s currently no pre-built shortcut to this, but you could pretty easily write your own script to follow this process:

    1. Generate a patch for the stuff that is currently in your working directory but not in your index yet (things you haven’t done git add for)

      git diff-files -p > /tmp/unstaged.patch
      
    2. Generate a patch for what you’ve already added to the index against your current HEAD

      git diff-index --cached -p HEAD > /tmp/staged.patch
      
    3. Reset your index and working directory to your HEAD

      git reset --hard HEAD
      
    4. Apply your unstaged patch to both your working directory and your index, resulting in those changes being staged

      git apply --index /tmp/unstaged.patch
      
    5. Apply your staged patch against only your working directory

      git apply /tmp/staged.patch
      

    Depending on the exact nature of your changes, steps 4 and/or 5 may result in some merge conflicts that you need to resolve by hand, but I’m not sure there’s a clean way to completely avoid that possibility.

    You could probably use git stash to accomplish steps 1 and 4 instead of the above commands, but I’m not sure that would really gain you anything…

    Also, you may want to review the man pages for the git diff-* and git apply commands first to see if there are other options that might make sense for you to use.

提交回复
热议问题