在Git Commit中还原对文件的更改,但将其保留在工作树中



我刚刚提交了对大约4个文件的一些更改。在将此提交发送到远程之后,我注意到此提交中包含了几个额外的文件。如何进行

  1. 确保远程文件中不包含更改
  2. 确保生产服务器也不包含这些文件(我已经在那里拉过了(
  3. 在我的本地工作树中对这两个文件进行原始更改

换句话说,就这些文件而言,我希望恢复更改。

如果重要的话,这是最新的承诺。这之后再也没有了。此外,这些是新创建的文件,而不是正在更改的文件。

编辑:

当我执行git show时,我可以看到我在提交中实际想要的文件有如下内容:

diff --git a/mysite/apps/common/models.py b/mysite/apps/common/models.py
index 9d86707..1a53b94 100755
--- a/mysite/apps/common/models.py
+++ b/mysite/apps/common/models.py

而以某种方式错误包含的文件具有

diff --git a/mysite/apps/payment/migrations/0011_auto_20221006_1850.py b/mysite/apps/payment/migrations/0011_auto_20221006_1850.py
new file mode 100644
index 0000000..26320ba
--- /dev/null
+++ b/mysite/apps/payment/migrations/0011_auto_20221006_1850.py

它们是从哪里来的?

不要通过要求将其保留在工作树中来过度复杂化。如果您进行了一次还原(并将其丢失在工作树上(并推送,以便在您现在需要的地方对其进行更正((,那么您可以找到如何以您想要的方式将其还原,这是用完成的(还原后(

git checkout HEAD~ -- file1 file2

然后你可以用正确的提交消息、正确的文件和其他东西来提交。

顺便说一句,这并不是你要求的不能实现。。。这是可以做到的,但我不明白当东西已经在生产时,你为什么要这样做。

好吧!如果您不想在远程/生产服务器中,则不能将它们保留在提交中。如果您认为有一些文件错误地添加到您的上次提交中,那么您可以通过以下操作恢复到上次暂存状态:

git reset --soft HEAD^

这将恢复上次提交,并将您的头移到上一次提交,同时保持所有文件处于暂存状态。从那里,您可以通过git reset <file>删除不需要的文件。

好吧,让我们试着回答你的问题:

1. Make sure that the remote does not contain changes in these files.
2. Make sure that the production server also does not contain these files (I already did a pull there)
3. Have the original changes to these two files in my local working tree.

请确保远程不包含这些文件中的更改

我不认识你们的供应链管理供应商。如果您有UI服务,只需打开commit的链接并浏览文件即可。如果你可以访问远程服务器,你可以:

ssh me@otherhost "cd repo && git log -n 10"
OR
ssh me@otherhost "cd repo && git show --pretty='' --name-only <commit>"

浏览和显示git repo中的文件,而无需克隆

确保生产服务器也不包含这些文件(我已经在那里拉过了(

您还可以连接到远程生产服务器,并使用查找所需的提交

git log --oneline --decorate --oneline --graph <branch>

如果您知道在生产服务器上提交哈希,您可以使用follow

git show --pretty="" --name-only <commit hash>

获取已更改文件的列表

在我的本地工作树中对这两个文件进行原始更改

因此,如果您的更改已发布(提交(,并且您没有其他本地更改(而是只有git-stash-then和git-stash-list,git-stash-apply(,则您可以将本地HEAD重置为以前的提交(父级(。

git reset --hard HEAD~ #if you didn't create any commits
# If you made some commits after publishing
# You should to rewrite linear history of branch
# Or make revert commit
# Case with rewriting history of your branch, let's name your branch as devel
1. find your published commit, let's name it as `git tag unstable <unstable_published_commit>`
2. Move temp_devel to parent of an unstable - `git branch -f temp_develop unstable~`
3. Checkout on temp_devel - `git checkout temp_devel`
4. "Pull" your changes from unstable commit - `git cherry-pick -n <unstable_commit>` and `git status` and remove unwanted files
5. git commit -m 'Your message'
6. Will be created a new commit with your two files
7. Rewrite history of `devel`, `git rebase -i HEAD devel`
8. You are in an interactive mode right now
9. You can remove or edit some of commits, read an instruction
10. `:wq` in `vim` to save and close your file
11. If rebase will be successful you can publish your `devel` branch with correct commit with two files. Otherwise just run - `git rebase --abort` to return everything as it was.

if you know that you doing at 8 step, than use git rebase --continue to continue your rebase state

它会删除您的本地更改和索引更改(要小心,使用git状态来确保(

您还可以使用.gitignore文件来存储在将更改添加到索引阶段时要忽略的文件或目录的列表。

如果您需要带有有效负载数据的夫妇文件,您可以使用gitcherry-picn-n<publishedcommit_hash>要获得相同的更改并使用git checkout--filename或git reset--filename从索引阶段排除不需要的文件,请查看git状态帮助消息

最新更新