Search code examples
gitgit-pullgit-rewrite-history

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?


Solution

  • 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