so I have this branch, let's call x01, I did some work there, and when I was gonna push to origin (git push --set-upstream-to=origin/<branch> <my branch>
), I received the message:
hint: Updates were rejected because the tip of your current branch is behind
hint: its distant counterpart. Complete the remote changes (e.g. ‘git pull’)
hint: before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘force push –help’ for details.
so I did run a git pull
and was able to push to master, after a few minutes I saw a test failing, so I made a quick correction and pushed again, now my commits look like this:
"MY ORIGINAL COMMIT" hashcode
"not my stuff" hashcode
"not my stuff" hashcode
"not my stuff" hashcode
"not my stuff" hashcode
"FIXING THE TEST" hashcode
I don't want those commits that aren't mine to be present in the commit history for this branch, I was able to return back to my original commit with git reset --hard HEAD~5
but I don't know how to move from here to be able to push only my stuff
ps: git status
Your branch is behind 'origin/....' by 5 commits, and can
be fast-forwarded.
(use "git pull" to update your local branch)
If I run git pull, I will be back at the same problem
EDIT: appears that I haven't been clean on my situation, so my workflow was like this
there's the master branch
I run a git checkout -b my_branch
to create my branch
did some work on my_branch, and pushed. meanwhile, some other people pushed stuff to master Now I have a fix that I need to implement on my_branch, did the fix
when I'm trying to push the fix, I'm receiving this error: Your branch is behind by 5 commits, and can be fast-forwarded.
I did a git pull --rebase
and finally was able to do a git push
however, now my git history for this branch is like this:
"MY ORIGINAL COMMIT"
"commit from other people"
"commit from other people"
"commit from other people"
"commit from other people"
"MY SECOND COMMIT"
what I want is this:
"MY ORIGINAL COMMIT"
"MY SECOND COMMIT"
how do I remove the commits that aren't mine from the history?
CodePudding user response:
I would create a new branch for this:
- Make
git status
clean, e.g. commit your current uncommitted changes to a temp branch with:git switch -c temp_branch
git add --all
git commit -am "Better safe than sorry"
git reset --hard hash_for_"MY ORIGINAL COMMIT"
- point to the last place you know you definitely wantgit switch -c my_new_better_branch
- create a new branch- Apply necessary changes:
- a new commit, and/or
git cherry-pick hash_for_"MY SECOND COMMIT"
git push
- a new branch without any of the "not my stuff" commits
Please note that doesn't rewrite history - which can go wrong.