简体   繁体   English

Git倒置临时区域

[英]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. 谢谢。

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 使用交互式rebase切换提交的顺序
  4. Mixed reset 混合重置
  5. Soft reset 软复位

It can be typed out manually pretty fast, especially if you use Vim for commit messages: 它可以非常快速地手动输出,特别是如果你使用Vim提交消息:

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

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 add的内容)

     git diff-files -p > /tmp/unstaged.patch 
  2. Generate a patch for what you've already added to the index against your current HEAD 针对当前HEAD为已添加到索引的内容生成补丁

     git diff-index --cached -p HEAD > /tmp/staged.patch 
  3. Reset your index and working directory to your HEAD 将索引和工作目录重置为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. 根据您的更改的确切性质,步骤4和/或5可能会导致您需要手动解决的一些合并冲突,但我不确定是否有一种干净的方法可以完全避免这种可能性。

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… 您可以使用git stash来完成第1步和第4步,而不是上面的命令,但我不确定这会给你带来什么......

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. 此外,您可能需要首先查看git diff-*git apply命令的手册页,以查看是否有其他选项可供您使用。

Based on gtd's answer and the ability to script inverting the commits this is what I'm using now: 根据gtd的答案和脚本反转提交的能力,这就是我现在使用的:

[alias]                                                                                                                                                                                                              
    swaplast = !git tag _invert && git reset --hard HEAD~2 && git cherry-pick _invert _invert~1 && git tag -d _invert                                                                                            
    invertindex = !git commit -m tmp1 && git add -A && git commit -m tmp2 && git swaplast && git reset HEAD~1 && git reset HEAD~1 --soft

Posted on my blog here: http://blog.ericwoodruff.me/2013/12/inverting-git-index.html 发布在我的博客上: http//blog.ericwoodruff.me/2013/12/inverting-git-index.html

This is what I use to solve this issue. 这就是我用来解决这个问题的方法。

First git reset which will remove all the files from 'staging' to 'files not staged for commit'. 第一个git reset ,它将把所有文件从'staging'删除到'for stage for commit'。 The files that are in both 'staging' and 'files not staged for commit' will keep your most recent changes that are currently in your 'files not staged for commit' “暂存”和“未提交的文件”中的文件将保留当前位于“未提交提交的文件”中的最新更改

then 然后

git add /path/to/file the specific files you need to add to staging git add /path/to/file您需要添加到登台的特定文件

Not exactly a short-cut, but it gets the job done 不完全是捷径,但它完成了工作

Inversely, After you git reset , you could git checkout /path/to/file for the files that are currently 'not staged for commit' which you don't want to add to staging. 反过来,在git reset ,你可以git checkout /path/to/file来查找当前“不进行提交”的文件,你不想将它们添加到登台。 This will remove specific files from 'not staged to commit' 这将从'not staged to commit'中删除特定文件

then run git add . 然后运行git add . which will add all files in 'not staged for commit' to 'staging' - whichever is easier for your situation 这将把'not staged for commit'中的所有文件添加到'staging'中 - 以您的情况更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM