git:如何将先前提交中存在的丢失文件合并到最新提交

时间:2017-10-17 11:03:58

标签: git github version-control git-bash

原始提交包含5个文件:

a.txt
b.txt
c.txt
d.txt
e.txt

下一次提交会在a.txt底部添加行,删除b.txt并上传新文件f.txt。即。

a.txt    - additions from second commit
c.txt
d.txt
e.txt
f.txt    - new file from second commit

我想合并提交,以便我接受更改a.txt,从第一次提交中恢复b.txt并从最新提交中添加f.txt。回购现在应该是这样的:

a.txt    - additions from second commit
b.txt    - restored from first commit
c.txt
d.txt
e.txt
f.txt    - new file from second commit

我尝试使用git rebase --root -i执行此操作,并将原始提交保留为"选择"并将第二次提交更改为" squash"

pick commit1
squash commit2

但是在rebase完成后,我只剩下commit2

我做错了什么,或者我问的不可能?

1 个答案:

答案 0 :(得分:0)

如果您想在特定提交中获取给定文件的内容,只需运行:

git checkout <commit> -- file1 [file2 ...]

看起来你只需要这样做:

# get the version of b.txt from your previous commit :
git checkout <original commit> -- b.txt

# you will now see 'b.txt' as staged for commit :
git status

# create a new commit on top of the previous one :
git commit