How do I delete a commit from my branch history?
Should I use git reset --hard HEAD?
How do I delete a commit from my branch history?
Should I use git reset --hard HEAD?
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.
Source: https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8
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
First reset the branch to the parent of the current commit
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
Here I just post one clear pipeline to do so
Step1: Use git log to get the commit ID.
git log
Step2: Use git reset to go back to the former version:
git reset --hard <your commit id>
If you're an IntelliJ user, the interface is simply awesome. With a single click, you can see the effect immediately at the same time. Shortcut to this place: Cmd + 9 on MacOS.
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
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
git reset --hard HEAD~1
You will be now at previous head. Pull the branch. Push new code. Commit will be removed from git
// 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
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):
git reset --hard 515b5220c50e3dfbb1063f23789d92ae1d3481a2 //it back-up you to that commit (SHA1 ID of test change 4 commit is 515b5220c50e3dfbb1063f23789d92ae1d3481a2) git reset --hard HEAD~1 // it back-up you before one commit.git reset --hard HEAD^ // To remove the last commit from gitafter delete:
With more than 30 ansers here, I'll keep it simple with what works for me
Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:
Checkout
git checkout --orphan latest_branch
Add all the files
git add -A
Commit the changes
git commit -m "commit message"
Delete the branch
git branch -D master
Rename the current branch to master
git branch -m master
Finally, force update your repository
git push -f origin master
PS: this will not keep your old commit history around.
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 delete the commit you are currently on but don't want to delete the data-
git reset --soft HEAD~1
If you want to delete the commit you are currently on and want to delete the related data-
git reset --hard HEAD~1
PS: these commands will only delete the local commits.
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.