Reputation: 76902
I did a pull request but after that I made some commits to the project locally which ended polluting my pull request, I tried to remove it but without any luck.
I found some similar questions on StackOverflow but I can't apply what's in there. It's my first pull request on GitHub so it's kinda strange to me how all of this works.
The highlighted commit is the one I need to keep and remove all the other stuff. It becomes the fourth commit in the history because I make some merge stuff.
Can someone please explain what's going on and how to fix this problem?
Upvotes: 263
Views: 412909
Reputation: 141946
You have several techniques to do it.
I recommend of reading this answer:
How can I move HEAD back to a previous location? (Detached head) & Undo commits
It will explain in details what we want to do and how to do it (revert).
Here is the most simple solution to your problem:
# Checkout the desired branch
git checkout <branch>
# Undo the desired commit
git revert <commit>
# Update the remote with the undo of the code
# The force is a tricky flag since it will force the push but
# your administrator can block it, so if it's not an option you
# can delete the old branch and push it again
git push origin <branch> --force
The revert command will create a new commit with the undo of the original commit.
Upvotes: 202
Reputation: 9647
People wouldn't like to see a wrong commit and a revert commit to undo changes of the wrong commit. This pollutes commit history.
Here is a simple way for removing the wrong commit instead of undoing changes with a revert commit.
git checkout my-pull-request-branch
git rebase -i HEAD~n
// where n
is the number of last commits you want to include in interactive
rebase.
Replace pick
with drop
for commits you want to discard.
Save and exit.
git push --force-with-lease
(safer force push, pointed out by @George)
Upvotes: 361
Reputation: 6673
From your branch fire below commands
git reset --soft origin/master
git commit -m "Detail of Your last commit that you want to see"
git push -f
Upvotes: 5
Reputation: 671
If accidentally pushed unnecessary files and wants to remove them from PR then
1. git reset <commit ID wherever you want to jump>
2. git restore <file name you want to remove> or git add <all the file
names you want to add to commit>
3. git commit -m “new commit message”
4. git push -f //necessary to push forcefully (if anything is there to pull)
Upvotes: 0
Reputation: 2991
This is what helped me:
Create a new branch with the existing one. Let's call the existing one branch_old
and new as branch_new
.
Reset branch_new
to a stable state, when you did not have any problem commit at all.
For example, to put it at your local master's level do the following:
git reset —hard master git push —force origin
cherry-pick
the commits from branch_old
into branch_new
git push
Upvotes: 5
Reputation: 325
If you're removing a commit and don't want to keep its changes @ferit has a good solution.
If you want to add that commit to the current branch, but doesn't make sense to be part of the current pr, you can do the following instead:
git rebase -i HEAD~n
git reset HEAD^ --soft
to uncommit the changes and get them back in a staged state.git push --force
to update the remote branch without your removed commit.Now you'll have removed the commit from your remote, but will still have the changes locally.
Upvotes: 14
Reputation: 4112
So do the following ,
Lets say your branch name is my_branch and this has the extra commits.
git checkout -b my_branch_with_extra_commits
(Keeping this branch saved under a different name)gitk
(Opens git console)git checkout my_branch
gitk
(This will open the git console )reset branch to here
" git pull --rebase origin branch_name_to _merge_to
git cherry-pick <SHA you copied in step 3. >
Now look at the local branch commit history and make sure everything looks good.
Upvotes: 1