1

I am quite new to Git and I joined a company as an intern. I (stupidly) started all of my branches by pulling our staging environment. So when I compare on github, there are like a hundred commits, and then mines. The issue is that there are waaaaay too many conflicts to push these branches on master.

That's why I'm desperately trying to find a way to start a branch from a specific commit without having the previous (i.e. the ones coming from the staging pull) ones, only the latest. For example :

git pull origin staging
[I did my stuff]
git commit -m "some stuff done"
[some other stuff]
git commit -m "other stuff done"]

I would like to go back to the commit "some stuff done" without having everything that the "pull origin staging" has done and also keeping "other stuff done".

I have already checked and tried the solutions on this topic but it still keeps the old commits.

Thank you !

bastien-r
  • 69
  • 6

1 Answers1

0

This sounds like a situation where you need git cherry-pick.

I would go back to master, where I understand you want your branch to start from, create a fresh branch, then cherry pick your commits.

  • git checkout master
  • git checkout -b my_branch
  • git cherry-pick commit1
  • git cherry-pick commit2

where commit1 and commit2 are the sha1 hashes for your first and second commits.

If the master branch is where you were when you did the two commits, you'll need modify the above commands a bit.

  • create the new branch from the specific starting point you want:
    • git checkout -b my_branch origin/master or some other sha1 hash instead of origin/master.
  • do the two cherry picks
  • Fix master by resetting it back to origin/master:
    • git checkout master
    • git reset --hard origin/master.
  • Go back to your branch to keep working:
    • git checkout my_branch.

Hope this helps.

joanis
  • 4,360
  • 4
  • 21
  • 30
  • Thanks it kind of worked, I still had a ton of conflits to take care of but it did the trick. I just wanted to add that `git reflog show --no-abbrev my-branch` was really helpful to see which commits were mines in each branch – bastien-r Sep 27 '17 at 11:44