英文:
Git pulling remote changes into local without creating a new merge commit
问题
我正在尝试学习Git,我已经分叉并克隆了一个仓库(比如叫做someRepo),并将mainRepo作为上游添加到我的分叉仓库(forkedRepo)中。这两个仓库都有一个develop分支。假设有人将一些提交(比如C1、C2)推送到mainRepo,而我有一些本地未推送的提交(比如L1、L2),我不打算推送它们。如何以一种干净的方式获取C1和C2,同时保留L1和L2?我之前尝试过使用git merge,但那会创建一个新的合并提交,我想避免这种情况。谢谢!
为了澄清,这是我的git远程仓库看起来像这样:
git remote -v
origin git@github.paypal.com:myFork/someRepo.git (fetch)
origin git@github.paypal.com:myFork/someRepo.git (push)
upstream git@github.paypal.com:mainRepo/someRepo.git (fetch)
upstream git@github.paypal.com:mainRepo/someRepo.git (push)
现在如果有人将C1、C2推送到mainRepo的develop分支上,你如何将它们获取到你的myFork的本地develop分支,并保持本地的提交L1、L2不变?
英文:
I am trying to learn git, I have forked and cloned a repo (say someRepo) and added the mainRepo as an upstream to my forked repo (forkedRepo) in my local. Both the repos have a develop branch. Say someone pushed some commits - say C1,C2 onto the mainRepo, and I have some local, unpushed commits say L1,L2 which I don't plan to push. How do I get C1 and C2 and still keep L1 and L2 in a clean way ? I tried git merge previously, but that ended up creating a new merge commit, which I want to avoid. Thanks !
to clarify, here is what my git remote looks like
git remote -v
origin git@github.paypal.com:myFork/someRepo.git (fetch)
origin git@github.paypal.com:myFork/someRepo.git (push)
upstream git@github.paypal.com:mainRepo/someRepo.git (fetch)
upstream git@github.paypal.com:mainRepo/someRepo.git (push)
Now if someone pushed C1, C2 onto develop of mainRepo, how do I get them onto my local develop of myFork, keeping my commits L1,L2 on my local develop intact
答案1
得分: 1
以下是翻译好的内容:
你最好的选择简要描述如下:
git merge L1
https://git-scm.com/docs/git-merge
正如你提到的,这将创建一个合并提交,这是一个良好的实践。这样最准确地表示您的历史事件,也是我个人首选的方式。为什么要避免合并提交?也许你还没有完全了解它们的好处,感觉它们“污染”了你的历史记录?如果是这种情况,我建议你开始学习有关分支和合并的更多信息,并尝试与它们一起工作。如果您计划定期执行此操作,我强烈建议将合并作为默认策略。
git rebase L1 --onto C1
https://git-scm.com/docs/git-rebase
简而言之,这将采取您的本地分支,并尝试在L1的基础上重新创建所有您的提交。如果在此过程中出现合并冲突,您必须解决它们。这模拟了所有更改都是在L1之后进行的情况。
git cherry-pick <commit>
https://git-scm.com/docs/git-cherry-pick
这会获取单个提交并将其放入您的分支。如果您只想挑选一个单独的更改,这尤其有用。
在官方文档中您会找到更多详细信息和示例。我还可以推荐 https://learngitbranching.js.org/ 用于学习。
英文:
Your best options briefly described are
git merge L1
https://git-scm.com/docs/git-merge
As you mentioned, this will create a merge commit which is good practice. This represents your historic events most accurately and would be my personal preferred way. Why do you want to avoid merge commits? Maybe you are not fully aware of the benefits and it feels they "pollute" your history? If that's the case, I would recommend working with them and start reading more about branching and merging. If you plan repeating this action on a regular basis, I would highly recommend merge as default strategy.
git rebase L1 --onto C1
https://git-scm.com/docs/git-rebase
In a nutshell, this takes your local branch and tries to re-create all your commits based on L1. If you find merge conflicts on the way, you have to fix them. This simulates that all your changes would have been made after L1.
git cherry-pick <commit>
https://git-scm.com/docs/git-cherry-pick
This takes a single commit and puts into onto your branch. Especially useful, if you want to pick a single change one time only.
You will find much more details and examples in the official docs. I can also recommend https://learngitbranching.js.org/ for learning.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论