Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8716345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:03:27+00:00 2026-06-13T06:03:27+00:00

There are a few commits on a remote branch that i would like to

  • 0

There are a few commits on a remote branch that i would like to get rid of. For example if the history looks like:

A->B->C->D

I would like to delete C and D to give me:

A->B

where B is now HEAD?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:03:28+00:00Added an answer on June 13, 2026 at 6:03 am

    I am going to lay out all the different ways to delete commits for you and when you should use them.
    Before you do any of these I highly recommend you copy your branch to another branch just to be safe.

    Do this by doing git checkout -b copy_branch and then switch back to your original branch by doing git checkout the_branch_i_want_to_delete_from_again

    If you have already pushed, as in your case, you can skip to #3, but if you haven’t pushed you can look at 1 and 2.

    1) I have not pushed yet or am working alone and the commit(s) I want to remove are the most recent commits:

    If i have:
    A—B—C—D

    and I want to delete C and D.

    Then do git reset --hard sha_of_B

    which results in:
    A—B

    If you have already pushed, as in your case, you could still do it this way then do a git push --force origin the_branch, but this is not recommended as you could mess other people up working on this project. You should follow #3 instead.

    2) I have not pushed yet or am working alone and the commits(s) I want to remove are in the middle of my branch:

    If i have:
    A—B—C—D

    and I want do delete C.

    git rebase -i sha_of_B_the_commit_before_the_one_i_want_to_delete
    

    which opens up the extremely useful interactive rebase screen:

    pick sha_of_C C
    pick sha_of_D D
    

    As git prompts you “# If you remove a line here THAT COMMIT WILL BE LOST.” that is what we are going to do.

    I delete the line pick sha_of_C C, which leaves me with:

    pick sha_of_D D
    

    I save it in vi with :wq, which results in:

    A—B—D

    If you have already pushed, as in your case, you could still do it this way then do a git push --force origin the_branch, but this is not recommended as you could mess other people up working on this project. You should follow #3 instead.

    3) I have already pushed and I am working with other people:

    If I have:
    A—B—C—D
    and I want to delete C and D.

    Then do

    git revert sha_of_D
    

    Note that you may have to resolve conflicts here and then commit.
    Then do

    git revert sha_of_C
    

    Note that you may have to resolve conflicts here and then commit.

    This results in:

    A—B—C—D—Reverted_D—Reverted_C

    This is safe to push as you are really just adding a commit that reverses all of C’s and D’s changes, essentially deleting it.

    You should generally avoid doing a git push --force at all costs unless absolutely necessary. But If you are about to do a push force, use protection, you should definitely make a copy of your branch before doing so. It is generally a good rule to just stick to #3 if you have already pushed at all.

    Hopefully this is helpful.

    Dan

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like there a few working solutions for using custom true type fonts
I have merged with other branch, and there are few commits after merge, before
I have an application that's locked down using forms authentication. There are few nightly
There are a few windows service projects in our codebase that follow this model,
Many times it happens that I have few commits on my local Hg repository
Is there anyway to remove several consecutive commits in a branch ? Let's says
I have a branch called feature and it has a few commits. The branch
We have quite a few repositories with submodules and we would like to report
in my page there are few hidden field which are filled with the correct
I have a webpage where there are few text fields to be filled up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.