Search code examples
gitlaravelbitbucketgit-pullgit-fetch

Git: git pull - start from specific commit until the latest commits skipping 1 commit?


How can I pull from a branch to start fetching on a specific commit?

Suppose, I have these branches: branch1 and oldbranch1

branch1 is the rollback version of oldbranch1

And suppose, oldbranch1 have these commits:

  • commit_5
  • commit_4
  • commit_3 (merge somebranch to oldbranch1)
  • commit_2
  • commit_1

And on branch1:

  • commit_2
  • commit_1

Now I want to get the updates from oldbranch1 skipping the commit_3 from oldbranch1 as it contains wrong merges that deletes the changes from commit_2 and commit_1

How can I do it? is there any magic commands for this?


Solution

  • Skipping a commit means mainly a git rebase --interactive:

    • you make a tmp branch on top of oldbranch1 (git branch tmp oldbranch1)
    • you rebase -i branch1 that tmp branch, replaying each commit except commit3 that you can drop
    • you reset --hard branch1 to the new tmp HEAD.

    The last step means: moving (forcing) branch1 HEAD to tmp, but since the rebase only added new (rewritten) commits to branch1, you should be able to simply do:

    git checkout branch1
    git merge tmp 
    

    That should make a fast-forward merge of tmp to branch1, moving simply branch1 HEAD to where tmp is.

    Then a simple git push (or git push -u origin branch1 if branch1 was never pushed before) will be enough.