Adrien BARRAL
Adrien BARRAL

Reputation: 3604

Delete a git commit pushed on a remote branch

We use git and GitFlow as versioning tools, and I'm facing a problem that I am quite sure some people have already encountered.

In a feature branch, a colleague did a commit in which a lot of useless files were inserted (all binary and IDE metadata files). This commit was pushed. Now this colleague submits a pull request, and I don't want to integrate all these useless files into develop.

Now, my git repository is quite huge, and a git clone becomes a long and boring process...

The first (and easy) solution is to remove these files from the feature branch, and commit without these files (or do a revert commit). But if I do that, my git repository will remain quite big because objects (files) will not be removed from the history (because git still knows about them!).

What I would like is to "cancel" this commit.

The following picture shows my problem (the topmost commit is the oldest):

Graph of git repository

How can I remove this commit and make my git clone quick again ?

PS: Thanks to GitGraphJS for their useful tools to draw git graphs!

Upvotes: 23

Views: 71032

Answers (2)

Aishwarya Patil
Aishwarya Patil

Reputation: 567

The below commands will remove the history of commits after a certain commit id, if you reset to that particular commit id

  1. (optional) This command will help you check the commit from which it started diverging from the source: git log --pretty=format:"%h %s" --graph
  2. Once you know the commit ID, you can do git reset --hard <commit_id>
  3. Then you push the changes to Github git push --force

Upvotes: 2

danglingpointer
danglingpointer

Reputation: 4920

There are several methods to delete or undo the commit. In that case, you can use git revert or git rebase or git reset.

One information, git rebase don't add one extra commit compared to using git revert. git revert adds one extra commit during the merge. If you're not familiar with using git rebase, I suggest you use git revert then.

Revert

git revert <commit-ID>

after that you commit the changes and push that commit to your origin.


Reset

You can delete the last pushed commit, after doing this you have to push your branch. You can decide either to merge or rebase the feature branch to development branch.

git reset –-hard HEAD~1

or

git reset HEAD^ --hard

Rebase

This command will lead to interactive section of editor, from there you can pick

 git rebase -i HEAD~

In the interactive section, it lists all the commits. Delete the one you want to get rid off. Finish the rebase and push force to the repo.

git rebase --continue then push your branch.

Upvotes: 38

Related Questions