Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last commit and push

Learning GIT in general and GitKraken at the same time. I have done small change in in one file - aa.cpp , have commit and push to remote repository with the help of GitKraken. Suddenly I found that I have pushed all files that was in project directory that I don't like.

Now I need to remove unwanted files from repository. I prefer to delete last push from remote repository and try to commit and push once again. How to delete last commit using GIT commands. How to make the same with GitKraken?

like image 505
vico Avatar asked Nov 19 '16 13:11

vico


People also ask

How do you remove last commit and push in git?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .

Can I delete a commit after push?

To remove a commit you already pushed to your origin or to another remote repository you have to first delete it locally like in the previous step and then push your changes to the remote. Notice the + sign before the name of the branch you are pushing, this tells git to force the push.

How do I remove last push from remote?

To undo the last commit from a remote git repository, you can use the git reset command. command. This will undo the last commit locally. command to force push the local commit which was reverted to the remote git repository.


Video Answer


2 Answers

If you have already pushed this commit, then it is possible that someone else has pulled the branch. In this case, rewriting your branch's history is undesirable and you should instead revert this commit:

git revert <SHA-1>
git push origin branch

Here <SHA-1> is the commit hash of the commit you want to remove. To find this hash value, simply type git log on your branch and inspect the first entry.

Using git revert actually adds a new commit which is the mirror image of the commit you want to remove. It is the preferred way of undoing a commit on a public branch because it simply adds new information to the branch.

If you are certain that you are the only person using this branch, then you have another option:

git reset --hard HEAD~1

followed by

git push --force origin branch

But you should only use this option if no one else is sharing this branch.

like image 133
Tim Biegeleisen Avatar answered Sep 26 '22 23:09

Tim Biegeleisen


The way I go about it is by typing git status, which allows us to verify the branch we're currently on, followed by:

git log

Then, you'll see something like this:

commit aa09a82fb69af2d1aebde51d71514f7a03c3a692
Author: User <[email protected]>
Date:   Fri Nov 4 15:36:22 2016 -0400

    Fix issue with vertical scroll being forced

commit 411771837efe3ed555395e77fd35105a500ab758
Author: User <[email protected]>
Date:   Thu Nov 3 15:50:42 2016 -0400

    Add user notifications

commit f43b262f4e02b5a7268280e1230d44e36d1e547b
Author: User <[email protected]>
Date:   Thu Nov 3 12:11:00 2016 -0400

    All your base are belong to us

So, this tells us that commit aa09a82f is your last one, and commit 41177183 is the one before it, then:

git reset --hard 41177183

...brings us back to that commit, retaining the remote backup. With another git status to make sure that everything is all set for the double push (I'm personally a bit obsessive compulsive about verifying my current branch, especially when multitasking):

git push origin :<branch_name>
git push origin <branch_name>

At this point, you should be all set, but it's always good to follow that up with:

git fetch --all --prune
git branch -av

...which cleans up your branch list and shows you both local and remote to compare the commit messages.

Also, if working with a team, make sure that they're aware of this before moving forward. You don't want them to pull or push the branch on their end before you remove the last commit and push.

like image 23
Steven Ventimiglia Avatar answered Sep 24 '22 23:09

Steven Ventimiglia