如何在我的分支中只存储多个已更改文件中的一个?


当前回答

我会使用git存储保存补丁。我不觉得交互很烦人,因为在交互过程中,可以选择将所需的操作应用于整个文件。

其他回答

类似的情况。做了承诺,意识到这不好。

git commit -a -m "message"
git log -p

根据这些回答,这对我有帮助。

# revert to previous state, keeping the files changed
git reset HEAD~
#make sure it's ok
git diff
git status
#revert the file we don't want to be within the commit
git checkout specs/nagios/nagios.spec
#make sure it's ok
git status
git diff
#now go ahead with commit
git commit -a -m "same|new message"
#eventually push tu remote
git push

这里的每个答案都很复杂。。。

“藏起来”怎么样

git diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash

这将弹出文件更改:

git apply /tmp/stash.patch

与隐藏一个文件并将其弹出完全相同的行为。

git add .                           //stage all the files
git reset <pathToFileWillBeStashed> //unstage file which will be stashed
git stash                           //stash the file(s)
git reset .                         // unstage all staged files
git stash pop                       // unstash file(s)

由于在Git中创建分支很简单,您可以创建一个临时分支并将各个文件检入其中。

如果不想指定包含隐藏更改的消息,请在双破折号后传递文件名。

$ git stash -- filename.ext

如果它是一个未跟踪的/新的文件,则必须先将其暂存。

此方法适用于git2.13版本+