How to delete last commit git
How to delete last commit git
How can I undo pushed commits using git?
I have a project in a remote repository, synchronized with a local repository (development) and the server one (prod). I’ve been making some commited changes already pushed to remote and pulled from the server. Now, I want to undo those changes. So I could just git checkout to the commit before the changes and commit the new changes, but I’m guessing that there will be problems to push them again to remote. Any suggestion on how should I proceed?
14 Answers 14
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You can revert individual commits with:
This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:
Look at the git-revert man page for more information about the git revert command. Also, look at this answer for more information about reverting commits.
A solution that keeps no traces of the «undo».
NOTE: don’t do this if someone already pulled your change (I would use this only on my personal repo.)
this will re-checkout all the updates locally (so git status will list all updated files)
then you «do your work» and re-commit your changes (Note: this step is optional)
At this moment your local tree differs from the remote
will force the remote branch to take this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).
What I do in these cases is:
In the server, move the cursor back to the last known good commit:
Locally, do the same:
See a full example on a branch my_new_branch that I created for this purpose:
This is the recent history after adding some stuff to myfile.py :
I want to get rid of the last commit, which was already pushed, so I run:
Nice! Now I see the file that was changed on that commit ( myfile.py ) shows in «not staged for commit»:
Since I don’t want these changes, I just move the cursor back locally as well:
So now HEAD is in the previous commit, both in local and remote:
This will remove your pushed commits
You can REVERT (or you can also call it DELETE) the Git Commit BOTH Locally and Remotely if you follow the steps as given below via git command line.
Run the following command to see the commit id that you want to revert
You will get like a following screenshot
If you also check remote (via Web Interface) then you can see that this would be same as shown below
As per screenshot currently you are on commit id e110322 however you want to revert back to 030bbf6 BOTH LOCALLY and REMOTELY.
Perform the following steps to DELETE/REVERT Commits Locally+Remotely
First Locally Reverting to commit id 030bbf6
These two commands clean force reset to commit stage 030bbf6 as shown below in snapshot
now if you run git status then you’ll see that you are TWO Commits BEHIND from the remote branch as shown below
Run following to update your indexes (if there are any updates). It is recommended that you ask all developers not to accept any pull requests on main remote branch.
Once you are done with it then you are required to Push this commit forcefully by using + symbol in-front of branch as shown below. I have used here as master branch, you can replace it with any
now if you see the web interface of remote then commit there should be reverted as well.
Can I delete a git commit but keep the changes?
In one of my development branches, I made some changes to my codebase. Before I was able to complete the features I was working on, I had to switch my current branch to master to demo some features. But just using a «git checkout master» preserved the changes I also made in my development branch, thus breaking some of the functionality in master. So what I did was commit the changes on my development branch with a commit message «temporary commit» and then checkout master for the demo.
Now that I’m done with the demo and back to work on my development branch, I would like to remove the «temporary commit» that I made while still preserving the changes I made. Is that possible?
12 Answers 12
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
It’s as simple as this:
Note: some shells treat ^ as a special character (for example some Windows shells or ZSH with globbing enabled), so you may have to quote «HEAD^» or use HEAD
1 in those cases.
Note that another option is to carry on as normal, and then at the next commit point instead run:
which will instead edit the most recent commit, having the same effect as above.
Note that this (as with nearly every git answer) can cause problems if you’ve already pushed the bad commit to a place where someone else may have pulled it from. Try to avoid that
There are two ways of handling this. Which is easier depends on your situation
Reset
If the commit you want to get rid of was the last commit, and you have not done any additional work you can simply use git-reset
Then you can just used git uncommit in the future to back up one commit.
Squashing
Squashing a commit means combining two or more commits into one. I do this quite often. In your case you have a half done feature commited, and then you would finish it off and commit again with the proper, permanent commit message.
Note that the commits will be re-committed in the order they show up on the list when you exit the editor. So if you made a temporary commit, then did other work on the same branch, and completed the feature in a later commit, then using rebase would allow you to re-sort the commits and squash them.
WARNING:
Stashing
In the future, to avoid this problem consider using git stash to temporarily store uncommitted work.
This will store your current changes off to the side in your stash list. Above is the most explicit version of the stash command, allowing for a comment to describe what you are stashing. You can also simply run git stash and nothing else, but no message will be stored.
You can browse your stash list with.
This will show you all your stashes, what branches they were done on, and the message and at the beginning of each line, and identifier for that stash which looks like this stash@ <#>where # is its position in the array of stashes.
To restore a stash (which can be done on any branch, regardless of where the stash was originally created) you simply run.
In your case you moved around branches a bit more, but the same idea still applies.
On undoing, fixing, or removing commits in git
This document is an attempt to be a fairly comprehensive guide to recovering from what you did not mean to do when using git. It isn’t that git is so complicated that you need a large document to take care of your particular problem, it is more that the set of things that you might have done is so large that different techniques are needed depending on exactly what you have done and what you want to have happen.
If you have problems after clicking through this document, please copy-paste the «Path» you took (what links you clicked on, automatically presented to you if javascript is available) when asking for further help, since doing so will explain very precisely what you were trying to do, and that you at least tried to help yourself.
First step
Answer the questions posed by clicking the link for that section. A section with no links is a terminal node and you should have solved your problem by completing the suggestions posed by that node (if not, then report the chain of answers you made on #git or some other git resource and explain further why the proposed answer doesn’t help). This is not a document to read linearly.
Are you trying to find that which is lost or fix a change that was made?
Due to previous activities (thrashing about), you may have lost some work which you would like to find and restore. Alternately, you may have made some changes which you would like to fix. Fixing includes updating, rewording, and deleting or discarding.
Have you committed?
If you have not yet committed that which you do not want, git does not know anything about what you have done yet, so it is pretty easy to undo what you have done.
Discard everything or just some things?
So you have not yet committed, the question is now whether you want to undo everything which you have done since the last commit or just some things, or just save what you have done?
How to save uncommitted changes
There are five ways you can save your uncommitted change. I also suggest you read Pro Git as these are pretty basic git operations.
How to undo all uncommitted changes
How to undo some uncommitted changes
So you have not yet committed and you want to undo some things, well git status will tell you exactly what you need to do. For example:
Do you have uncommitted stuff in your working directory?
So you have committed. However, before we go about fixing or removing whatever is wrong, you should first ensure that any uncommitted changes are safe, by either committing them ( git commit ) or by stashing them ( git stash save «message» ) or getting rid of them.
git status will help you understand whether your working directory is clean or not. It should report nothing for perfect safety («Untracked files» only are sometimes safe.)
Have you pushed?
So you have committed, the question is now whether you have made your changes (or at least the changes you are interesting in «fixing») publicly available or not. Publishing history is a seminal event.
If you are dealing with commits someone else made, then this question covers whether they have pushed, and since you have their commits, the answer is almost certainly «yes».
Also note that these commands will fix up the referenced commits in your repository. There will be reflog’d and dangling commits holding the state you just corrected. This is normally a good thing and it will eventually go away by itself, but if for some reason you want to cut your seat belts, you can expire the reflog now and garbage collect with immediate pruning.
Do you want to discard all unpushed changes on this branch?
There is a shortcut in case you want to discard all changes made on this branch since you have last pushed or in any event, to make your local branch identical to «upstream». Upstream, for local tracking branches, is the place you get history from when you git pull : typically for master it might be origin/master. There is a variant of this option which lets you make your local branch identical to some other branch or ref.
Discarding all local commits on this branch
Replacing all branch history/contents
If instead of discarding all local commits, you can make your branch identical to some other branch, tag, ref, or SHA that exists on your system.
Once you have found the correct state of your branch, you can get to that state by running:
Obviously replace «REF» with the reference or SHA you want to get back to.
Is the commit you want to fix the most recent?
While the techniques mentioned to deal with deeper commits will work on the most recent, there are some convenient shortcuts you can take with the most recent commit.
Do you want to remove or change the commit message/contents of the last commit?
Removing the last commit
2 to remove the last two commits. You can increase the number to remove even more commits.
If you want to «uncommit» the commits, but keep the changes around for reworking, remove the «—hard»: git reset HEAD^ which will evict the commits from the branch and from the index, but leave the working tree around.
Reworking the last commit
WARNING: These techniques should only be used for non-merge commits. If you have a merge commit, you are better off deleting the merge and recreating it.
You can do this with multiple (non-merge) commits in a row (using «HEAD^^» or similar techniques), but then of course you lose the separation between the commits and are left with an undifferentiated working directory. If you are trying to squash all of the commits together, or rework which bits are in which commits, this may be what you want.
If you want to reorder commits, split them, merge them together, or otherwise perfect the commits, you should explore Post Production Editing Using Git.
Moving a commit from one branch to another
However, if everything looks good, we can move the actual destination branch pointer to where nonce is:
Updating the last commit’s contents or commit message
To update the last commit’s contents, author, or commit message for a commit which you have not pushed or otherwise published, first you need to get the index into the correct state you wish the commit to reflect. If you are changing the commit message only, you need do nothing. If you are changing the file contents, typically you would modify the working directory and use git add as normal.
If you want to do something more sophisticated that what «—amend» allows, please investigate reworking the last commit.
Do you want to remove an entire commit?
Removing an entire commit
» shortcuts you may use those.
Obviously replace «SHA» with the reference you want to get rid of. The «^» in that command is literal.
Do you want to remove/change/rename a particular file/directory from all commits during all of git’s history
Changing all commits during all of git’s history
You have not pushed but still somehow want to change all commits in all of git’s history? Strange.
Use The BFG to remove unwanted data, like big files or passwords, from Git repository history.
Disclaimer, the author of this document has not qualified this tool (including for safety or usability for any purpose), but recognizes the need which this tool fills for large repositories.
Remove all blobs bigger than 1 megabyte (to make your repo take up less space):
Replace all passwords listed in a file with ***REMOVED*** wherever they occur in your repository :
Arbitrarily changing all commits during all of git’s history
BTW, this is the one command I referred to earlier which will update all tags and branches, at least if you use the best practice arguments.
Is a merge commit involved?
If the commit you are trying to change is a merge commit, or if there is a merge commit between the commit you are trying to change and the tip of the branch you are on, then you need to do some special handling of the situation.
Changing a single commit involving only simple commits
» shortcuts you may use those.
Obviously replace «SHA» with the reference you want to get rid of. The «^» in that command is literal.
You will be dumped in an editor with a bunch of lines starting with pick. The oldest commit, the one you are probably interested in changing, is first. You will want to change the «pick» to «reword» or «edit», or perhaps even «squash» depending on what your goal is. Please read the manual page for more information. A document on Post-Production Editing using Git goes through much of the major uses of git rebase is some detail. The use case is a little different, but the fundamental techniques are not.
Changing a single commit involving a merge
Note, that this only applies if you have a merge commit. If a fast-forward (ff) merge occurred you only have simple commits, so should use other instructions.
Oh dear. This is going to get a little complicated. It should all work out, though. You will need to use a nonce branch as a placeholder. I will call the nonce branch «nonce» in the following example. However, you may use any branch name that is not currently in use. You can delete it immediately after you are done.
Identify the SHA of the commit you wish to modify.
» shortcuts you may use those.
Remember the name of the branch you are currently on
The line with a star on it in the git branch output is the branch you are currently on. I will use «$master» in this example, but substitute your branch name for «$master» in the following commands.
Create and checkout a nonce branch pointing at that commit.
Obviously replace «SHA» with the reference you want to modify.
Modify the commit
You need to get the index into the correct state you wish the commit to reflect. If you are changing the commit message only, you need do nothing. If you are changing the file contents, typically you would modify the working directory and use git add as normal.
If the commit you are updating is a merge commit, ensure that the log message reflects that.
Put the remaining commits after the new one you just created
Validate that the topology is still good
Delete the nonce branch
Can you make a positive commit to fix the problem and what is the fix class?
Rewriting public history is a bad idea. It requires everyone else to do special things and you must publicly announce your failure. Ideally you will create either a commit to just fix the problem, or a new git revert commit to create a new commit which undoes what the commit target of the revert did.
Making a new commit to fix an old commit
If the problem in the old commit is just something was done incorrectly, go ahead and make a normal commit to fix the problem. Feel free to reference the old commit SHA in the commit message, and if you are into the blame-based development methodology, make fun of the person who made the mistake (or someone who recently left if you made the mistake).
Making a new commit to restore a file deleted earlier
The file may have been deleted or every change to that file in that commit (and all commits since then) should be destroyed. If so, you can simply checkout a version of the file which you know is good.
» shortcuts you may use those.
Obviously replace «SHA» with the reference that is good. You can then add and commit as normal to fix the problem.
Reverting an old simple pushed commit
» shortcuts you may use those.
Obviously replace «SHA» with the reference you want to revert. If you want to revert multiple SHA, you may specify a range or a list of SHA.
Reverting a merge commit
Note, that this only applies if you have a merge commit. If a fast-forward (ff) merge occurred you only have simple commits, so should use another method.
Oh dear. This is going to get complicated.
» shortcuts you may use those.
One option is actually to do this reverse merge immediately, annihilating any changes before the bad merge, and then to «revert the revert» to restore them. This leaves the changes removed from the branch you mistakenly merged to, but present on their original branch, and allows merges in either direction without loss. This is the simplest option, and in many cases, can be the best.
Another option is to abandon the branch you merged from, recreate it from the previous merge-base with the commits since then rebased or cherry-picked over, and use the recreated branch from now on. Then the new branch is unrelated and will merge properly. Of course, if you have pushed the donor branch you cannot use the same name (that would be rewriting public history and is bad) so everyone needs to remember to use the new branch. Hopefully you have something like gitolite where you can close the old branch name.
This approach has the advantage that the recreated donor branch will have cleaner history, but especially if there have been many commits (and especially merges) to the branch, it can be a lot of work. At this time, I will not walk you through the process of recreating the donor branch. Given sufficient demand I can try to add that. However, if you look at howto/revert-a-faulty-merge.txt (also shipped as part of the git distribution) it will provide more words than you can shake a stick at.
Rewriting an old branch with a new branch with a new commit
If the state of a branch is contaminated beyond repair and you have pushed that branch or otherwise do not want to rewrite the existing history, then you can make a new commit which overwrites the original branch with the new one and pretends this was due to a merge. The command is a bit complicated, and will get rid of all ignored or untracked files in your working directory, so please be sure you have properly backed up everything.
You actually are being provided with two methods. The first set is more portable but generates two commits. The second knows about the current internal files git uses to do the necessary work in one commit. Only one command is different and a second command runs at a different time.
How can I undo a `git commit` locally and on a remote after `git push`
7 Answers 7
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Generally, make an «inverse» commit, using:
then send it to the remote as usual:
This won’t delete the commit: it makes an additional commit that undoes whatever the first commit did. Anything else, not really safe, especially when the changes have already been propagated.
First of all, Relax.
«Nothing is under our control. Our control is mere illusion.», «To err is human»
1. At first, get the SHA-1 value of the commit you are trying to return, e.g. commit to master branch. run this:
you’ll see bunch of ‘f650a9e398ad9ca606b25513bd4af9fe. ‘ like strings along with each of the commits. copy that number from the commit that you want to return back.
2. Now, type in below command:
you should see message like «HEAD is now at «. you are on clear. What it just have done is to reflect that change locally.
3. Now, type in below command:
you should see like
Now, you are all clear. Check the master with «git log» again, your fixed_destination_commit should be on top of the list.
You are welcome (in advance ;))
Now, the changes you had made before all these began, are now gone. If you want to bring those hard-works back again, it’s possible. Thanks to git reflog, and git cherry-pick commands.
For that, i would suggest to please follow this blog or this post.
Git FAQ
Frequently asked questions around Git and Version Control.
How can I undo the last commit?
First, before we bring the big guns in, let’s make sure you really need them. Because in case you just want to edit your last commit, you can simply use Git’s amend feature. It allows you to correct the last commit’s message as well as add more changes to it. If that’s what you want to do, read more about amend.
The Git Cheat Sheet
Undoing the Last Commit
In these cases, the «reset» command is your best friend:
In case you’re using the Tower Git client, you can simply hit CMD+Z to undo the last commit:
You can use same, simple CMD+Z keyboard shortcut to undo many other actions, from a failed merge to a deleted branch!
Undoing Multiple Commits
The same technique allows you to return to any previous revision:
Always keep in mind, however, that using the reset command undoes all commits that came after the one you returned to:
Learn More
Get our popular Git Cheat Sheet for free!
You’ll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That’s why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.
Источники информации:
- http://stackoverflow.com/questions/15772134/can-i-delete-a-git-commit-but-keep-the-changes
- http://sethrobertson.github.io/GitFixUm/fixup.html
- http://stackoverflow.com/questions/6459080/how-can-i-undo-a-git-commit-locally-and-on-a-remote-after-git-push
- http://www.git-tower.com/learn/git/faq/undo-last-commit