【发布时间】:2016-12-16 16:47:55
【问题描述】:
我正在使用 gitflow 分支模型进行开发。
我从develop 分支到feature/X,在feature/X 的第一次提交中,我还删除了一些文件(使用git rm <file>)。
现在,在该分支中进行了几次其他提交之后,我意识到我需要之前删除的文件。
这里有一个简短的示例来说明我要澄清的意思:
git flow init -d
echo "file contents" > file.txt
git commit -m "Added file.txt"
git checkout -b feature/X
git rm file.txt
echo "foo" > foo.txt
git add --all
git commit -m "Deleted file.txt and added another file"
<some more commits in feature/X>
git log -u
...
commit 04948fc4fc36d83901a0862b057657f3ccb9cf0d
Author: <...>
Date: Wed Aug 10 12:26:58 2016 +0200
Deleted file.txt and added another file
diff --git a/file.txt b/file.txt
deleted file mode 100644
index d03e242..0000000
--- a/file.txt
+++ /dev/null
@@ -1 +0,0 @@
-file contents
diff --git a/foo.txt b/foo.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/foo.txt
@@ -0,0 +1 @@
+foo
...
我不只是想在新的提交中重新添加文件,以避免在将 feature/X 合并到 develop 时出现问题,而 develop 分支中的 file.txt 发生了一些变化。
有什么方法可以从之前的提交中删除file.txt?
我尝试了git reset <sha-of-previous-commit> file.txt,但这并没有将文件恢复到我的工作副本中。
编辑 1:
我知道重写历史的不利方面已经被推送到远程。但是我知道除了我之外没有人在 feature/X 上进行过任何提交,因此尽管它已经被推送,但我重写历史记录应该是安全的。
【问题讨论】:
标签: git version-control git-rewrite-history