[git] Delete commits from a branch in Git

I would like to know how to delete a commit.

By delete, I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch.

I read git help, and I think the command I should use is git reset --hard HEAD. Is this correct?

This question is related to git git-rebase git-reset

The answer is


Here's another way to do this:

Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit". I think the command line is:

git reset --hard COMMIT_ID

Since you just checked out your branch from remote, you're not going to have any local changes to worry about losing. But this would lose them if you did.

Then navigate to your repository's local directory and run this command:

git -c diff.mnemonicprefix=false -c core.quotepath=false \
push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

This will erase all commits after the current one in your local repository but only for that one branch.


If you didn't publish changes, to remove latest commit, you can do

$ git reset --hard HEAD^

(note that this would also remove all uncommitted changes; use with care).

If you already published to-be-deleted commit, use git revert

$ git revert HEAD

Source: https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8

Delete the last commit

For example your last commit

git push origin +aa61ab32^:master

Now you want to delete this commit then an Easy way to do this following

Steps

  1. First reset the branch to the parent of the current commit

  2. Force-push it to the remote.

git reset HEAD^ --hard

git push origin -f

For particular commit, you want to reset is following

git reset bb676878^ --hard

git push origin -f

Assuming you have not pushed to the remote repository, you could re-clone the repository. This has been my method of choice a few times.


Forcefully Change History

Assuming you don't just want to delete the last commit, but you want to delete specific commits of the last n commits, go with:

git rebase -i HEAD~<number of commits to go back>, so git rebase -i HEAD~5 if you want to see the last five commits.

Then in the text editor change the word pick to drop next to every commit you would like to remove. Save and quit the editor. Voila!

Additively Change History

Try git revert <commit hash>. Revert will create a new commit that undoes the specified commit.


I have already pushed. Need to return some commits back remotly. Have tried many variations, but only this from Justin via git bush is working fine for me:

git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force

To delete in local branch, use

git reset --hard HEAD~1

To delete in a remote branch, use

git push origin HEAD --force

The mistake:

I git rebase -i --root'ed my branch, ignorantly thinking I could reword the first commit differing from the master (the GitHub for Windows default view is the comparison to master, hiding it's entirety).

I grew a Silicon Valley beard while 900+ commits loaded themselves into Sublime. Exiting with no changes, I charged my battery then proceeded to shave, as all 900+ individual commits nonchalantly rebased - resetting their commit times to now.

Determined to beat Git and preserve the original times, I deleted this local repository and re-cloned from the remote.

Now it had re-added a most recent unneeded commit to master I wished to remove, so proceeded like so.

Exhausting the options:

I didn't wish to git revert - it would create an additional commit, giving Git the upper hand.

git reset --hard HEAD did nothing, after checking the reflog, the last and only HEAD was the clone - Git wins.

To get the most recent SHA, I checked the remote repository on github.com - minor win.

After thinking git reset --hard <SHA> had worked, I updated another branch to master and 1... 2... poof! the commit was back - Git wins.

Checking back out to master, time to try git rebase -i <SHA>, then remove the line... to no avail, sad to say. "If you remove a line here THAT COMMIT WILL BE LOST". Ah...glossed over new feature troll the n00b in the 2.8.3 release notes.

The solution:

git rebase -i <SHA> then d, drop = remove commit.

To verify, I checked out to another branch, and voila - no hiding commit to fetch/pull from the master.

https://twitter.com/holman/status/706006896273063936

Good day to you.


git reset --hard

git push origin HEAD --force

If one or more of the commits is tagged, delete the tag(s) first. Otherwise the tagged commit is not removed.


// display git commit log    
$ git log --pretty=oneline --abbrev-commit

// show last two commit and open in your default editor
// then delete second commit line and save it
$ git rebase -i HEAD~2

Reference: How to delete a commit in git, local and remote


use git revert https://git-scm.com/docs/git-revert .It will revert all code then you can do next commit.Then head will point to that last commit. reverted commits never delete but it will not affect on you last commit.


Removing an entire commit

git rebase -p --onto SHA^ SHA

Obviously replace "SHA" with the reference you want to get rid of. The "^" in that command is literal.

http://sethrobertson.github.io/GitFixUm/fixup.html#change_deep


What I do usually when I commit and push (if anyone pushed his commit this solve the problem):

git reset --hard HEAD~1

git push -f origin

hope this help


git rebase -i HEAD~2

Here '2' is the number of commits you want to rebase.

'git rebase -i HEAD`

if you want to rebase all the commits.

Then you will be able to choose one of these options.

p, pick = use commit

r, reword = use commit, but edit the commit message

e, edit = use commit, but stop for amending

s, squash = use commit, but meld into previous commit

f, fixup = like "squash", but discard this commit's log message

x, exec = run command (the rest of the line) using shell

d, drop = remove commit

These lines can be re-ordered; they are executed from top to bottom. If you remove a line here THAT COMMIT WILL BE LOST. However, if you remove everything, the rebase will be aborted. Note that empty commits are commented out

You can simply remove that commit using option "d" or Removing a line that has your commit.


delete local commit

As you can see on above image i want to delete revert"test change 2" commit(SHA1 ID: 015b5220c50e3dfbb1063f23789d92ae1d3481a2(you can get SHA1 ID by using gitk command in git bash)).

For that i can use(all below command work on local only. you need to push after delete):

  1. git reset --hard 515b5220c50e3dfbb1063f23789d92ae1d3481a2 //it back-up you to that commit (SHA1 ID of test change 4 commit is 515b5220c50e3dfbb1063f23789d92ae1d3481a2)
  2. git reset --hard HEAD~1 // it back-up you before one commit.
  3. git reset --hard HEAD^ // To remove the last commit from git

after delete:

after delete commit


If you've already pushed, first find the commit you want to be at HEAD ($GIT_COMMIT_HASH_HERE), then run the following:

git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force

Then each place the repo has been cloned, run:

git reset --hard origin/master

If you have not yet pushed the commit anywhere, you can use git rebase -i to remove that commit. First, find out how far back that commit is (approximately). Then do:

git rebase -i HEAD~N

The ~N means rebase the last N commits (N must be a number, for example HEAD~10). Then, you can edit the file that Git presents to you to delete the offending commit. On saving that file, Git will then rewrite all the following commits as if the one you deleted didn't exist.

The Git Book has a good section on rebasing with pictures and examples.

Be careful with this though, because if you change something that you have pushed elsewhere, another approach will be needed unless you are planning to do a force push.


Reset on local branch

git reset --hard HEAD~<Number of commit> So git reset --hard HEAD~3

Force push to origin

git push -f origin

If you just messed up your last commit (wrong message, forgot to add some changes) and want to fix it before pushing it to a public repo why not use:

git commit --amend -m "New message here"

If you have newly staged changes they'll be combined with the last commit (that you're trying to get rid of) and will replace that commit.

Of course if you amend a commit after you've pushed it, you're rewriting history so if you do that be sure to understand the implications.

You can also pass the '--no-edit' option instead of '-m' if you would prefer to use the previous commit's message.

Docs: http://git-scm.com/docs/git-commit.html


Here I just post one clear pipeline to do so

Step1: Use git log to get the commit ID.

git log

enter image description here

Step2: Use git reset to go back to the former version:

git reset --hard <your commit id>

Another possibility is one of my personal favorite commands:

git rebase -i <commit>~1

This will start the rebase in interactive mode -i at the point just before the commit you want to whack. The editor will start up listing all of the commits since then. Delete the line containing the commit you want to obliterate and save the file. Rebase will do the rest of the work, deleting only that commit, and replaying all of the others back into the log.


[Quick Answer]

You have many alternatives, for example:

  • Alternative 1:

    git rebase -i <YourCommitId>~1
    

    Change YourCommitId for the number of the commit which you want to revert back to.

  • Alternative 2:

    git reset --hard YourCommitId
    git push <origin> <branch> --force
    

    Change YourCommitId for the number of the commit which you want to revert back to.

    I don't recommend this option because you can lost your work in progress.

  • Alternative 3:

    git reset --soft HEAD~1
    

    You can keep your work and only undo the commit.


I'm appending this answer because I don't see why anyone who has just tried to commit work would want to delete all that work because of some mistake using Git!

If you want to keep your work and just 'undo' that commit command (you caught before pushing to repo):

git reset --soft HEAD~1

Do not use the --hard flag unless you want to destroy your work in progress since the last commit.


In my case, my magic code for this pupose is this one:

git reset --hard @{u}

Test it and tell me. I have tried a few different ones, but this one was the only that helped me.


git reset --hard commitId

git push <origin> <branch> --force

PS: CommitId refers the one which you want to revert back to


If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:

git reset HEAD~1

This will return your repository to its state before the git add commands that staged the files. Your changes will be in your working directory. HEAD~1 refers to the commit below the current tip of the branch.

If you want to uncommit N commits, but keep the code changes in your working directory:

git reset HEAD~N

If you want to get rid of your latest commit, and do not want to keep the code changes, you can do a "hard" reset.

git reset --hard HEAD~1

Likewise, if you want to discard the last N commits, and do not want to keep the code changes:

git reset --hard HEAD~N

All the commands above restore the state of your work tree and index as they were before making the commit, but do not restore the state of the repository. If you look at it, the "removed" commit is not actually removed, it is simply not the one on the tip of the current branch.

I think that there are no means to remove a commit with porcelain commands. The only way is to remove it from the log and reflog and then to execute a git prune --expire -now.


Say we want to remove commits 2 & 4 from the repo.

commit 0 : b3d92c5
commit 1 : 2c6a45b
commit 2 : <any_hash>
commit 3 : 77b9b82
commit 4 : <any_hash>

Note: You need to have admin rights over the repo since you are using --hard and -f.

  • git checkout b3d92c5 Checkout the last usable commit.
  • git checkout -b repair Create a new branch to work on.
  • git cherry-pick 77b9b82 Run through commit 3.
  • git cherry-pick 2c6a45b Run through commit 1.
  • git checkout master Checkout master.
  • git reset --hard b3d92c5 Reset master to last usable commit.
  • git merge repair Merge our new branch onto master.
  • git push -f origin master Push master to the remote repo.

git reset --hard HEAD~1
You will be now at previous head. Pull the branch. Push new code. Commit will be removed from git


For me rebase did the trick

$ git rebase -i HEAD~98

# Delete everything except for the most recent commit on the shown editor caused by "rebase"

$ git push origin -f production-static

NOTE: After forcing the push to reduce my comments. I then pushed another set of files and then I tried pulled with the same "repository & branch" on another computer. Surprisingly there is no conflict. This became my way to reduce repository size while avoiding pull conflicts on my other locals that uses the same git


If you want to keep the history, showing the commit and the revert, you should use:

git revert GIT_COMMIT_HASH

enter the message explaining why are you reverting and then:

git push  

When you issue git log you'll see both the "wrong" commit and revert log messages.


Take backup of your code in to temp folder. Following command will reset same as server.

git reset --hard HEAD
git clean -f
git pull

If you want to keep your changes , and remove recent commits

git reset --soft HEAD^
git pull

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-rebase

rebase in progress. Cannot commit. How to proceed or stop (abort)? git remove merge commit from history Choose Git merge strategy for specific files ("ours", "mine", "theirs") What's the difference between 'git merge' and 'git rebase'? Your branch is ahead of 'origin/master' by 3 commits Rebase feature branch onto another feature branch git rebase merge conflict Remove folder and its contents from git/GitHub's history How to get "their" changes in the middle of conflicting Git rebase? How to rebase local branch onto remote master

Examples related to git-reset

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'? Git, How to reset origin/master to a commit? Can I delete a git commit but keep the changes? What is the meaning of git reset --hard origin/master? How to git reset --hard a subdirectory? git undo all uncommitted or unsaved changes Unstaged changes left after git reset --hard How do I use 'git reset --hard HEAD' to revert to a previous commit?