I got myself into some git-funk here. I need to git-fu myself out of this.
I joined a new team and created a feature branch:
git checkout -b feature_branch
Made some changes and then committed/pushed them up to the branch.
git commit -am "Changes"
git push origin feature_branch
Someone left a review on my PR, so I made the changes, and then checked out to master and rebased my branch before committing/pushing again to that branch:
// from feature_branch make some changes
git commit -am "New changes"
git checkout master
git checkout feature_branch
git rebase origin/master
git push feature_branch
Once I did this, I noticed my PR (on Github) picked up someone elses' commit. I was then informed that the typical method within this new team is to checkout to master and merge back into my branch INSTEAD of rebasing.
Here is the funky part now -- I started mucking around with git reset --hard
and picked the commit I wanted that was before that commit from someone else.
All was well, or so I thought. I then pushed that up and it seemed to have removed that other persons' commit from my PR.
I checked this morning and now there are a bunch of other commits from someone else that got picked up.
So now I'm in this weird state. I look at my PR and there are almost 30 commits (with 6 from different people). The actual diff (files changed) are only the files that I touched, which is good, but the history itself looks ridiculous.
What's the best approach to clean this up? Everything is suggesting to use git rebase
, however, I was advised not to use rebase.
Unfortunately, I need to keep this branch. What's the best way to clean it up and remove all the other commits except only mine? Just reset it completely and then cherry-pick the changes back onto the branch?
Please help :|
EDIT: Here's an example of what the history looks like:
Commits on Jul 30, 2018
<SOMEONE ELSES>
Commits on Jul 31, 2018
<SOMEONE ELSES>
<MY ORIGINAL COMMIT>
<SOMEONE ELSES>
Commits on Aug 1, 2018
<SOMEONE ELSES>
<MY COMMIT [Merge branch master into my feature branch]>
<MY COMMIT>
<SOMEONE ELSES>
<MY COMMIT>
<MY COMMIT>
<SOMEONE ELSES>
<MY COMMIT>
etc etc
Your best bet to clean stuff up is probably to use git rebase -i
to rebase on a previous change already in you branch1 before all the stuff you want to clean up, and then delete the changes you don't want from the list of changes in the editor (leaving just the changes you do want). That will rewrite the changes to include just your changes and get you back into a "clean" (relatively) state.
Once you've done that, you can then git merge
from master (or wherever) as recommended by your git flow.
1Possibly the change you originally branched from if you want to clean out everything you prevously rebased/merged into your branch, or possibly some later refpoint. The earlier the point you choose, the more stuff you (can) clean out, but also potentially the more work you need to do.