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


当前回答

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

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 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 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

我也想做同样的事情,但当我再次思考时,我意识到我不想为了保存一个文件而麻烦重重。对我来说,把它复制到其他地方并添加一些注释来提醒我为什么要把它保存在那里会容易得多。

不要误解我的意思:有很多原因可以解释为什么你只想隐藏一个文件,而且总的来说,使用版本控制软件总是最好的做法。但还是要确保你没有浪费时间。在我的情况下,我只想保留一个文件并放弃所有其他更改,然后在切换到新分支后将其弹出。所以cp/mv足够好。

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)

有时,我在提交分支之前对其进行了不相关的更改,我想将其移动到另一个分支并单独提交(如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。。。