Git 只拉一次提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32489300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:22:07  来源:igfitidea点击:

Git pull just one commit

git

提问by DarkteK

Actually the git repository and local files are exactly the same.

其实git仓库和本地文件是完全一样的。

But the other website is far away from 5 commits, so I haven't pull in a while and I don't want to do it neither.

但是另一个网站离 5 次提交还很远,所以我有一段时间没有拉,我也不想这样做。

So now I wanna do some change in my local files and then push that into a new commit to git repository, THEN only be able to PULL that one commit and not all the others ... can I do this??

所以现在我想对我的本地文件进行一些更改,然后将其推送到 git 存储库的新提交中,然后只能提取该提交而不是所有其他提交......我可以这样做吗?

I don't even want to delete the commits there, just I want to be able to pull 1 commit, I hope you can help me

我什至不想删除那里的提交,只是我希望能够拉出 1 个提交,希望你能帮助我

采纳答案by chrismillah

If you want to pull one commit, you can simply 'reset' to that commit..

如果你想拉一个提交,你可以简单地“重置”到那个提交..

git reset --hard <commit>

You could also make a new branch foooff that commit and pull only that branch down to your new environment. This will help maintain your codebase as you can continue to work on your original branch without having to think about affecting the new site.

您还可以从该提交中创建一个新分支foo并仅将该分支下拉到您的新环境中。这将有助于维护您的代码库,因为您可以继续在原始分支上工作,而无需考虑影响新站点。

git checkout -b foo

this is shorthand for

这是简写

git branch foo
git checkout foo

You can then pull that branch onto whatever machine with

然后你可以将该分支拉到任何机器上

git clone -b *foo* http//dude@:bitbucket.org

or something like

或类似的东西

git clone -b *foo* ssh://[email protected]/path/to/repo.git

回答by Matthieu Moy

git pullis essentially a shorthand for git fetch(download remote commits into remote-tracking branches) and then git merge(merge your HEAD, i.e. the current commit, with the ones you just downloaded).

git pull本质上是git fetch(将远程提交下载到远程跟踪分支)然后git merge(将您的HEAD,即当前提交与您刚刚下载的提交合并)的简写。

You can get the flexibility you expect by decoupling both steps:

通过将两个步骤解耦,您可以获得您期望的灵活性:

  • First, run git fetch, then inspect the history you just downloaded (if you work on the masterbranch, git fetchshould have downloaded remote commits in branch origin/masterwhich you can inspect with git log origin/master).

  • Then, merge the commit you want using git merge <commit>. Note: git mergewill get all the changes in <commit>and its ancestry, so this works if <commit>is the oldest non-merged commit. If you do not have any unpushed commits, this will "fast-forward", i.e. it won't create a new commit but just advance HEADto this commit. If you're not happy with git merge, you have other options like git rebase, git cherry-pick, ...

  • 首先,运行git fetch,然后检查您刚刚下载的历史记录(如果您在master分支上工作,git fetch应该已经在分支中下载了远程提交origin/master,您可以使用 进行检查git log origin/master)。

  • 然后,使用 合并您想要的提交git merge <commit>。注意:git merge将获得所有更改<commit>及其祖先,因此如果<commit>是最旧的未合并提交,则此方法有效。如果您没有任何未推送的提交,这将“快进”,即它不会创建新的提交而只是前进HEAD到此提交。如果您对 不满意git merge,您还有其他选择,例如git rebase, git cherry-pick, ...