如何在我的分支中只存储多个已更改文件中的一个?
当前回答
有时,我在提交分支之前对其进行了不相关的更改,我想将其移动到另一个分支并单独提交(如master)。我这样做:
git stash
git checkout master
git stash pop
git add <files that you want to commit>
git commit -m 'Minor feature'
git stash
git checkout topic1
git stash pop
...<resume work>...
请注意,第一个stash和stash pop可以取消,您可以在结账时将所有更改转移到主分支,但前提是没有冲突。此外,如果您要为部分更改创建新分支,则需要隐藏。
假设没有冲突和新分支,您可以简化它:
git checkout master
git add <files that you want to commit>
git commit -m 'Minor feature'
git checkout topic1
...<resume work>...
甚至不需要Stash。。。
其他回答
当git stash-p(或git add-p with stash--keep index)太麻烦时,我发现使用diff、checkout和apply更容易:
仅“隐藏”特定文件/dir:
git diff path/to/dir > stashed.diff
git checkout path/to/dir
然后
git apply stashed.diff
我已经回顾了这个问题的答案和评论以及一些类似的线索。请注意,为了能够隐藏任何特定的跟踪/未跟踪文件,以下命令都不正确:
git stash-p(--patch):手动选择大块,不包括未跟踪的文件git stash-k(--keep index):隐藏所有跟踪/未跟踪的文件,并将它们保存在工作目录中git stash-u(--包括未跟踪的):隐藏所有已跟踪/未跟踪的文件git stash-p(--patch)-u(--include untracked):无效命令
目前,能够隐藏任何特定跟踪/未跟踪文件的最合理方法是:
暂时提交不想隐藏的文件添加并隐藏弹出临时提交
在回答另一个问题时,我为这个过程编写了一个简单的脚本,这里有SourceTree中执行该过程的步骤。
更新(2015年2月14日)-我重写了一点脚本,以更好地处理冲突的情况,现在应该将其显示为未合并的冲突,而不是.rej文件。
我经常发现,与@bukzor的方法相反更直观。也就是说,要进行一些更改,然后只隐藏那些已进行的更改。
不幸的是,git没有提供git存储——只有索引或类似的内容,所以我编写了一个脚本来实现这一点。
#!/bin/sh
# first, go to the root of the git repo
cd `git rev-parse --show-toplevel`
# create a commit with only the stuff in staging
INDEXTREE=`git write-tree`
INDEXCOMMIT=`echo "" | git commit-tree $INDEXTREE -p HEAD`
# create a child commit with the changes in the working tree
git add -A
WORKINGTREE=`git write-tree`
WORKINGCOMMIT=`echo "" | git commit-tree $WORKINGTREE -p $INDEXCOMMIT`
# get back to a clean state with no changes, staged or otherwise
git reset -q --hard
# Cherry-pick the index changes back to the index, and stash.
# This cherry-pick is guaranteed to succeed
git cherry-pick -n $INDEXCOMMIT
git stash
# Now cherry-pick the working tree changes. This cherry-pick may fail
# due to conflicts
git cherry-pick -n $WORKINGCOMMIT
CONFLICTS=`git ls-files -u`
if test -z "$CONFLICTS"; then
# If there are no conflicts, it's safe to reset, so that
# any previously unstaged changes remain unstaged
#
# However, if there are conflicts, then we don't want to reset the files
# and lose the merge/conflict info.
git reset -q
fi
您可以将上面的脚本保存为路径上的某个位置的git stash索引,然后将其作为git stach索引调用
# <hack hack hack>
git add <files that you want to stash>
git stash-index
现在,存储包含一个新条目,该条目仅包含您已暂存的更改,而您的工作树仍然包含任何未暂存的更改。
在某些情况下,工作树的更改可能取决于索引的更改,因此当您隐藏索引更改时,工作树更改会发生冲突。在这种情况下,您将得到通常的未合并冲突,可以使用gitmerge/gitmergetool/等解决。
在这种情况下,我使用gitadd-p(交互式)、gitcommit-m blah,然后在必要时存储剩余的内容。
免责声明:以下答案适用于2.13之前的git。对于2.13及以上的数字,请查看下面的另一个答案。
警告
正如评论中所指出的那样,这将所有的东西都放进了储藏室,无论是暂存的还是未暂存的。--keep索引只是在存储完成后保留索引。这可能会在以后弹出存储时导致合并冲突。
这将隐藏您以前未添加的所有内容。只需git添加要保留的内容,然后运行它。
git stash --keep-index
例如,如果要将旧提交拆分为多个变更集,可以使用以下过程:
gitrebase-i<最后一次提交>将某些更改标记为编辑。git重置HEAD^git add<您要在此更改中保留的文件>git stash—保留索引必要时修理一下。不要忘记git添加任何更改。git提交吉特藏弹根据需要,从第5步开始重复。git rebase—继续
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别