我一直在本地存储库上工作,并多次推送到git服务器,而我在其上部署工作的服务器仍在所有这些提交之后。
很快,情况是这样的:
local: commit n+5
git server: commit n+5
dev server: commit n
现在我只想将提交号n + 5拉到开发服务器(不包括提交n + 1,n + 2,n + 3和n + 4)。
我在Google上搜索并找到了一些解决方案,其中包括Stackoverflow上的this one。
我尝试了git reset --hard <commit>
,但没有成功,出现了以下错误:
git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
git reset --hard 77de3a5
fatal: ambiguous argument '77de3a5': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
我想尝试the other proposed solution(使用git fetch
和git merge
),但听到它时我感到困惑
注意:git merge将获取所有更改及其祖先, 因此,如果是最早的非合并提交,则此方法有效。
恐怕这种方法将包括(拉/合并)在提交n + 5之前的所有提交。
有什么方法可以只提取一个提交(不包括先前的提交)?
PS:我知道最干净的方法是使用不同的分支,但是当您有点混乱时该怎么办!
答案 0 :(得分:1)
当然,要获取一次提交:
git fetch [remote] [branch/commit] --depth=1
该孤立的提交然后可以引用为FETCH_HEAD。在更新浅层子模块时,我经常这样做,但是在其他情况下,具有隔离的无历史记录的提交可能没有用。
关于reset --hard错误,该失败是因为您没有执行git fetch并且服务器还不知道存在77de3a5。
如果您只是想使开发服务器的历史记录保持整洁,您可能会想要:
git fetch
git merge --squash [commit#]
或者,如果您想避免意外引入多余的合并提交:
git fetch
git merge --ff-only [commit#]
最后,如果您只希望一次提交引起的更改
git fetch
git cherry-pick [commit#]