简体   繁体   中英

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 ?

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.

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 <user@useremail.com>
Date:   Fri Nov 4 15:36:22 2016 -0400

    Fix issue with vertical scroll being forced

commit 411771837efe3ed555395e77fd35105a500ab758
Author: User <user@useremail.com>
Date:   Thu Nov 3 15:50:42 2016 -0400

    Add user notifications

commit f43b262f4e02b5a7268280e1230d44e36d1e547b
Author: User <user@useremail.com>
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.

I do hope you're working on a separate branch, so not the master (or develop if your using git-flow). For my examples I'll use my-broken-branch as branch name ;)

My steps would be:
Remove the remote branch, we're going to push the updated version later.

git push origin :my-broken-branch

Next remove the last commit from the local branch. HEAD^1 refers to the commit one earlier than current.

git reset HEAD^1

Now go ahead and add just the files you need and commit as you're used to. Finally push your branch to remote. It will recreate the branch there and nobody has to know about the altered commit.

git push origin my-broken-branch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM