简体   繁体   English

在git pull中的所有提交之后,如何将本地提交移动到?

[英]How can I move a local commit to after all commits in a git pull?

Here's my situation. 这是我的情况。 I've been working on a feature and made a local commit. 我一直在开发功能并进行本地提交。 I carried on working. 我继续工作。 Meanwhile, a colleague pushed changes that I need. 同时,一位同事推动了我需要的更改。 Some of those changes are in the same files I'm working on. 其中一些更改位于我正在处理的相同文件中。

I currently have some code I'll need to stash before I pull from origin. 我目前有一些代码,在从源头提取之前需要存放。
Is there a way to pull the code so my colleagues code exists before my last local commit in the commit history? 有没有一种方法可以提取代码,以便同事代码在提交历史记录中的最后一次本地提交之前就存在?
I know for a fact that my local commit is older than the ones I'm going to pull. 我知道,我的本地提交比我要提交的提交更早。
I'm going to end up with something like this: 我将得到这样的结果:

older commits -> my commit -> origin commit 1 -> origin commit 2 -> popped stash

But what I want is: 但是我想要的是:

older commits -> origin commit 1 -> origin commit 2 -> my commit -> popped stash

The only way I can see how to do this is to undo my local commit, stash the whole lot, then pull then pop the stash. 我能看到的唯一方法是撤消本地提交,将全部存储,然后拉然后弹出存储。 Is there a way to do this that will maintain my old commit? 有什么办法可以保持我的旧提交?

I just saw there is a tag here for git-rewrite-history , so it might be possible? 我刚刚看到这里有git-rewrite-history的标签,所以可能吗?

Undoing and re-applying a series of commits is called a rebase . 撤消和重新应用一系列提交称为rebase To pull in changes and rebase instead of merge them you can use the --rebase option: 拉的变化和rebase ,而不是merge它们,你可以使用--rebase选项:

git stash
git pull --rebase origin master
git stash pop

You can use git cherry-pick concept: 您可以使用git cherry-pick概念:

First copy the commit-hash of commit-hash-before-my-commit , my-commit , original-commit-1 , original-commit-2 from git log output. 首先从git log输出中复制commit-hash-before-my-commitmy-commitoriginal-commit-1original-commit-2commit-hash-before-my-commit

$ git log                 # First copy the commit hashes

$ git checkout -b b1      # create a new branch b1 (just for experiment)

$ git reset --hard <commit-hash-before-my-commit>

$ git cherry-pick <original-commit-1>
$ git cherry-pick <original-commit-2>
$ git cherry-pick <my-commit>
$ git stash pop

Now, git log should show you the commit history as you want. 现在, git log应该显示您想要的提交历史。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM