Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git invert staging area

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 staging area and the changes which are not staged. Does a shortcut exist in order to do that, without doing more complex actions like local side-branch commits, or diffs, or stashes [etc.]? Thanks.

like image 958
moala Avatar asked Jan 31 '13 15:01

moala


People also ask

How do you revert a commit to staging?

Staged files are those which go into your next commit. If you accidentally added files to the staged area, you can undo this by typing git restore --staged <file> , so in this case, it would be git restore --staged lib.

What is the git staging area?

These files are also referred to as "untracked files." Staging area is files that are going to be a part of the next commit, which lets git know what changes in the file are going to occur for the next commit. The repository contains all of a project's commits.

How do I move changes to staging area in git?

The closest that I know how to do is to copy all of the files that were changed in the commit to somewhere else, reset the branch to the commit before the commit that you're trying to move into the staging area, move all of the copied files back into the repository, and then add them to the staging area.


2 Answers

Here’s how I do it:

  1. Commit the index to a temporary commit
  2. Commit the remainder to a secondary temporary commit
  3. Switch the order of the commits with interactive rebase
  4. Mixed reset
  5. Soft reset

It can be typed out manually pretty fast, especially if you use Vim for commit messages:

git commit -m tmp1
git add . # optionally with `git add -u` if there are deletions
git commit -m tmp2
git rebase -i HEAD~2 # swap the order of the commits; `ddp:wq` in vi
git reset HEAD~1
git reset HEAD~1 --soft
like image 82
gtd Avatar answered Sep 28 '22 10:09

gtd


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.

like image 26
twalberg Avatar answered Sep 28 '22 08:09

twalberg