How to remove commits from a pull request
Asked Answered
C

8

252

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.

enter image description here

my git log enter image description here

Can someone please explain what's going on and how to fix this problem?

Chiropteran answered 23/3, 2016 at 2:38 Comment(5)
You need to rebase, and only "pick" the "added from github" commit (i.e. comment out the lines for every other commit)Harragan
rebase what branch onto what branch can you please add more explanation.Chiropteran
what branch = the branch you're working on, onto what branch = the branch you're pull requesting intoHarragan
I explained what @Robbie Averill hinted in my answer. It's strange that nobody wrote an answer explaining this until now.Agler
related: #62952401Raffia
E
200

You have several techniques to do it.

I recommend of reading this answer:

TL;DR; (from the recommended answer)

enter image description here

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.

Exultant answered 23/3, 2016 at 6:5 Comment(5)
Does it work even when we forked the project from some another person and wanna remove some comments in pull request?Palestra
to revert all commit from pull request you only need Merge pull request commit id. # Undo the desired commits from pull request git revert <Merge pull request commit id> -m 1Loudspeaker
You are right @Deeppak, just clarifying that the -m stands for the parent of the merge so you might need to use -m n instead of 1Exultant
@Palestra if you're working on a forked project, just use git push --force after the git revert <commit> command. This will revert the commit on the PR.Beard
push --force might be disabled by the git adminExultant
A
337

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.

  1. git checkout my-pull-request-branch

  2. git rebase -i HEAD~n // where n is the number of last commits you want to include in interactive rebase.

  3. Replace pick with drop for commits you want to discard.

  4. Save and exit.

  5. git push --force-with-lease (safer force push, pointed out by @George)

Agler answered 18/7, 2018 at 11:14 Comment(15)
this worked for me like a charm. I had bunch of commits which shouldnt have been in the PR. I just interactive rebased it, dropped those commits and pushed them backSpectrograph
You should sync with remote master before starting a pr anyway, no?Agler
To push I had to do: git push origin HEAD:myBranch --force. But otherwise great and helpful.Lingenfelter
@Lingenfelter I omitted that part intentionally as it's out of scope. If you didn't set the tracking branch you either need to set it or, like you did, push explicitly.Agler
This is also useful when you want to fix an open PR in GitHub, you just need to be able to do a forced pushCordova
Worked like a charm. Also it is better if you can cherry-pick the commit that you want to separate from the PR to another branch.Liveried
What do you mean by "Replace pick with drop for commits you want to discard."?Irairacund
@jorijnsmit they mean after running git rebase -i HEAD~<n>, at the top of the file there will <n> lines of commits, with each line containing the text pick <commit id>. The commits default to pick, so for every commit you want to drop, change the text pick to dropKristinkristina
Silly question - is the "~" a part of the git rebase -i HEAD~n command?Asher
Not at all. Yeah, it is a part of the command. @DougKimzeyAgler
This works like a treat. I created a branch that carried over a bunch of commits from the previous branch. So straightforward, unlike alot of git stuff!Month
For all those who don't know how to perform 4. step - Save and exit: type :wq! and press Enter key.Sergu
I find it easier to rebase onto the target branch, which is usually the one you branched from: f.x. git rebase -i origin/main this will show all the commits on your branch compared to the target branch.Asleep
Using git push --force could overwrite changes not seen on your local. To prevent removing others work by accident, use git push --force-with-lease.Asleep
@Asleep Thanks, I learned this now, editing the answer.Agler
E
200

You have several techniques to do it.

I recommend of reading this answer:

TL;DR; (from the recommended answer)

enter image description here

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.

Exultant answered 23/3, 2016 at 6:5 Comment(5)
Does it work even when we forked the project from some another person and wanna remove some comments in pull request?Palestra
to revert all commit from pull request you only need Merge pull request commit id. # Undo the desired commits from pull request git revert <Merge pull request commit id> -m 1Loudspeaker
You are right @Deeppak, just clarifying that the -m stands for the parent of the merge so you might need to use -m n instead of 1Exultant
@Palestra if you're working on a forked project, just use git push --force after the git revert <commit> command. This will revert the commit on the PR.Beard
push --force might be disabled by the git adminExultant
L
14

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:

  1. use git rebase -i HEAD~n
  2. Swap the commit you want to remove to the bottom (most recent) position
  3. Save and exit
  4. use git reset HEAD^ --soft to uncommit the changes and get them back in a staged state.
  5. use 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.

Li answered 28/3, 2019 at 14:47 Comment(1)
Be very careful when using git push --force. It will completely (and irreversibly) override everything that's on the remote with the contents of your local computer.Fraternity
T
5

This is what helped me:

  1. Create a new branch with the existing one. Let's call the existing one branch_old and new as branch_new.

  2. 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

  3. cherry-pick the commits from branch_old into branch_new

  4. git push
Trina answered 17/6, 2020 at 12:29 Comment(0)
N
5

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
Noami answered 12/10, 2022 at 8:48 Comment(0)
N
1

So do the following ,

Lets say your branch name is my_branch and this has the extra commits.

  1. git checkout -b my_branch_with_extra_commits (Keeping this branch saved under a different name)
  2. gitk (Opens git console)
  3. Look for the commit you want to keep. Copy the SHA of that commit to a notepad.
  4. git checkout my_branch
  5. gitk (This will open the git console )
  6. Right click on the commit you want to revert to (State before your changes) and click on "reset branch to here"
  7. Do a git pull --rebase origin branch_name_to _merge_to
  8. git cherry-pick <SHA you copied in step 3. >

Now look at the local branch commit history and make sure everything looks good.

Naturalism answered 23/3, 2016 at 3:12 Comment(0)
I
1

To remove an unwanted commit from a branch in Git, you can use the following steps:

  1. Checkout the branch: Make sure you're on the branch where you pushed the unwanted code:
git checkout <branch-name>

Replace <branch-name> with the actual name of your branch.

  1. Revert the commit: Use git revert to create a new commit that reverses the changes made by the unwanted commit:
git revert -n <commit-hash>

Replace <commit-hash> with the hash of the unwanted commit (you can find this in your Git log).

  1. Edit and amend: Open an editor (e.g., nano or vim) to edit the new commit message, then save and close the file:
git commit --amend -m "Reverted unwanted code"

This will update the commit message with a new one.

  1. Force-push: If you've already pushed the unwanted code, you'll need to force-push the updated branch to overwrite the previous commits:
git push origin <branch-name> --force
Incrocci answered 23/4 at 8:11 Comment(0)
P
0

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)
Paulinepauling answered 10/8, 2022 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.