简体   繁体   中英

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?

Undoing and re-applying a series of commits is called a rebase . To pull in changes and rebase instead of merge them you can use the --rebase option:

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

You can use git cherry-pick concept:

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                 # 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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