Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a git commit pushed on a remote branch

Tags:

git

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!

like image 270
Adrien BARRAL Avatar asked Jun 08 '17 16:06

Adrien BARRAL


People also ask

How do I remove a pushed commit from a remote branch?

To delete commits from remote, you can use the git reset command if your commits are consecutive from the top or an interactive rebase otherwise. After you delete the commits locally, push those changes to the remote using the git push command with the force option.

Can I delete a commit after push?

Just delete the line containing the commit you want to remove to delete that commit.

Does deleting remote branch delete commits?

What Happens If I Delete a Git Branch? When you delete a branch in Git, you don't delete the commits themselves. That's right: The commits are still there, and you might be able to recover them.


1 Answers

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.

like image 101
danglingpointer Avatar answered Oct 02 '22 15:10

danglingpointer