ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 1.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://superuser.com/questions/35267/how-can-i-roll-back-1-commit |
| Last Crawled | 2026-03-04 17:40:18 (1 month ago) |
| First Indexed | 2014-08-23 07:27:15 (11 years ago) |
| HTTP Status Code | 200 |
| Meta Title | git - How can I roll back 1 commit? - Super User |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | This question shows research effort; it is useful and clear
159
Save this question.
Show activity on this post.
I have 2 commits that I did not push:
$ git status
# On branch master
# Your branch is ahead of 'faves/master' by 2 commits.
How can I roll back my first one (the oldest one), but keep the second one?
$ git log
commit 3368e1c5b8a47135a34169c885e8dd5ba01af5bb
...
commit baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
...
From here:
http://friendfeed.com/harijay/742631ff/git-question-how-do-i-rollback-commit-just-want
Do I just need to do:
git reset --hard baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
That is?
asked
Sep 3, 2009 at 21:23
2
This answer is useful
111
Save this answer.
Show activity on this post.
The safest and probably cleanest way to go is to rebase interactively.
git rebase -i HEAD^^
Or,
git rebase -i baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e^
From there you can squash commits, which puts one or more commits together into the previous commit. To completely delete a commit from the history, delete the line from the list.
You can revert a commit with
git revert
but its going to add more commit messages to the history, which may be undesirable. Use the
-n
parameter to tell Git not to commit the revert right away. You can rebase interactively and squash those on up to a previous commmit to keep things clean.
If the two commits you're working with here affect the same file(s), you may see a merge conflict.
Resetting the repository with
git reset --hard
should be done with care, as it cannot be undone.
Rewriting history should be done with care.
answered
Sep 3, 2009 at 21:32
6
This answer is useful
70
Save this answer.
Show activity on this post.
This if from
http://nakkaya.com/2009/09/24/git-delete-last-commit/
and it worked for me
Git Delete Last Commit
Once in a while late at night when I ran out of coffee, I commit stuff
that I shouldn't have. Then I spend the next 10 - 15 minutes googling
how to remove the last commit I made. So after third time I wanted to
make a record of it so I can refer to it later.
If you have committed junk but not pushed,
git reset --hard HEAD~1
HEAD~1 is a shorthand for the commit before head. Alternatively you
can refer to the SHA-1 of the hash you want to reset to. Note that
when using --hard any changes to tracked files in the working tree
since the commit before head are lost.
If you don't want to wipe out the work you have done, you can use
--soft
option that will delete the commit but it will leave all your
changed files "Changes to be committed", as git status would put it.
Now if you already pushed and someone pulled which is usually my case,
you can't use git reset. You can however do a git revert,
git revert HEAD
This will create a new commit that reverses everything introduced by
the accidental commit.
answered
Aug 5, 2011 at 17:52
1
This answer is useful
8
Save this answer.
Show activity on this post.
Nope. git-reset --hard will bring you back in history. What you're looking for is git revert, which will undo any commit.
answered
Sep 3, 2009 at 21:26
1
This answer is useful
6
Save this answer.
Show activity on this post.
I just done this:
git rebase -i HEAD^^
I screwed it up so I did
git rebase --abort
Then did it again. Then I had to push like this:
git push origin master -f
And it destroyed the commits newer than the commit I had rolled back to. Worked great.
answered
Dec 12, 2011 at 4:39
This answer is useful
5
Save this answer.
Show activity on this post.
No,
git reset --hard baf8d5e
will delete the
3368e1c
commit and HEAD will be at
baf8d5e
afterwards.
If you want to keep the
3368e1c
commit and delete the
bad8d5e
commit the easiest solution is to do a "
git rebase -i HEAD~2
" (i.e. interactive rebase of the last two commits). This command will launch your commit message editor and you'll see one line for each of the last two commits. There you just delete the
bad8d5e
commit line and save. git will then rewrite your history and the 2nd commit will be gone.
There are other useful commands you can use in the commit message editor like
squash
,
edit
, etc. Interactive rebase is VERY powerful!
Do not do this if somebody already saw these commits (push or pull from your repository)!
answered
Sep 3, 2009 at 21:46
This answer is useful
1
Save this answer.
Show activity on this post.
git reset --hard {ref}
is the only way to undo a commit if there is only one other commit in the repo (e.g. initial commit and 1 more). The rest of the ways (revert, rebase) refuse to work, at least as of git 1.7.5.1.
If you follow the
git reset
with a
git gc
then git will actually delete the old commit data from the repo completely.
answered
Feb 28, 2012 at 20:17
This answer is useful
1
Save this answer.
Show activity on this post.
git checkout <treeish> -- /path/to/dir
That will bring back the directory from the given “treeish” for the /path/to/dir
slhck
237k
73 gold badges
639 silver badges
611 bronze badges
answered
Oct 12, 2012 at 9:23
This answer is useful
0
Save this answer.
Show activity on this post.
I got this to work by manually editing the hash codes of the last commits from HEAD files inside the repository folder:
"centralRepository\refs\heads\master"
"centralRepository\refs\heads\branch2"
Before that, I was never able to push to origin the UNMERGE operations I did locally.
It kept saying it "failed to push some refs" to Central Repository.
cpast
2,533
2 gold badges
22 silver badges
27 bronze badges
answered
Jun 13, 2014 at 0:08
This answer is useful
0
Save this answer.
Show activity on this post.
Or just fetch the remote branch on pull request:
git fetch origin/<name of branch on pull request> -b <name of branch on pull request (if making changes to same pull request)/name of another branch (e.g.: main)>
answered
Feb 10, 2022 at 22:58
You must
log in
to answer this question.
Start asking to get answers
Find the answer to your question by asking.
Ask question
Explore related questions
See similar questions with these tags. |
| Markdown | # 
By clicking “Sign up”, you agree to our [terms of service](https://superuser.com/legal/terms-of-service/public) and acknowledge you have read our [privacy policy](https://superuser.com/legal/privacy-policy).
# OR
Already have an account? [Log in](https://superuser.com/users/login)
[Skip to main content](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#content)
#### Stack Exchange Network
Stack Exchange network consists of 183 Q\&A communities including [Stack Overflow](https://stackoverflow.com/), the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
[Visit Stack Exchange](https://stackexchange.com/)
1. - [Tour Start here for a quick overview of the site](https://superuser.com/tour)
- [Help Center Detailed answers to any questions you might have](https://superuser.com/help)
- [Meta Discuss the workings and policies of this site](https://meta.superuser.com/)
- [About Us Learn more about Stack Overflow the company, and our products](https://stackoverflow.co/)
2. ### [current community](https://superuser.com/)
- [Super User](https://superuser.com/)
[help](https://superuser.com/help) [chat](https://chat.stackexchange.com/?tab=site&host=superuser.com)
- [Meta Super User](https://meta.superuser.com/)
### your communities
[Sign up](https://superuser.com/users/signup?ssrc=site_switcher&returnurl=https%3A%2F%2Fsuperuser.com%2Fquestions%2F35267%2Fhow-can-i-roll-back-1-commit) or [log in](https://superuser.com/users/login?ssrc=site_switcher&returnurl=https%3A%2F%2Fsuperuser.com%2Fquestions%2F35267%2Fhow-can-i-roll-back-1-commit) to customize your list.
### [more stack exchange communities](https://stackexchange.com/sites)
[company blog](https://stackoverflow.blog/)
3. [Log in](https://superuser.com/users/login?ssrc=head&returnurl=https%3A%2F%2Fsuperuser.com%2Fquestions%2F35267%2Fhow-can-i-roll-back-1-commit)
4. [Sign up](https://superuser.com/users/signup?ssrc=head&returnurl=https%3A%2F%2Fsuperuser.com%2Fquestions%2F35267%2Fhow-can-i-roll-back-1-commit)
[](https://superuser.com/)
1. 1. [Home](https://superuser.com/)
2. [Questions](https://superuser.com/questions)
3. [Unanswered](https://superuser.com/unanswered)
4. [AI Assist](https://stackoverflow.com/ai-assist)
5. [Tags](https://superuser.com/tags)
6. [Chat](https://chat.stackexchange.com/)
7. [Users](https://superuser.com/users)
8. [Companies](https://stackoverflow.com/jobs/companies?so_medium=superuser&so_source=SiteNav)
2. Stack Internal
Stack Overflow for Teams is now called **Stack Internal**. Bring the best of human thought and AI automation together at your work.
[Try for free](https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=superuser-community&utm_campaign=side-bar&utm_content=explore-teams) [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=superuser-community&utm_campaign=side-bar&utm_content=explore-teams)
3. [Stack Internal]()
4. Bring the best of human thought and AI automation together at your work. [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=superuser-community&utm_campaign=side-bar&utm_content=explore-teams-compact)
**Stack Internal**
Knowledge at work
Bring the best of human thought and AI automation together at your work.
[Explore Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=superuser-community&utm_campaign=side-bar&utm_content=explore-teams-compact-popover)
# [How can I roll back 1 commit?](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit)
[Ask Question](https://superuser.com/questions/ask)
Asked
16 years, 5 months ago
Modified [3 years, 11 months ago](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit?lastactivity "2022-02-10 22:58:18Z")
Viewed 392k times
This question shows research effort; it is useful and clear
159
Save this question.
Show activity on this post.
I have 2 commits that I did not push:
```
$ git status
# On branch master
# Your branch is ahead of 'faves/master' by 2 commits.
```
How can I roll back my first one (the oldest one), but keep the second one?
```
$ git log
commit 3368e1c5b8a47135a34169c885e8dd5ba01af5bb
...
commit baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
...
```
From here:
<http://friendfeed.com/harijay/742631ff/git-question-how-do-i-rollback-commit-just-want>
Do I just need to do:
```
git reset --hard baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
```
That is?
- [git](https://superuser.com/questions/tagged/git "show questions tagged 'git'")
[Share](https://superuser.com/q/35267 "Short permalink to this question")
Share a link to this question
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this question](https://superuser.com/posts/35267/edit)
Follow
Follow this question to receive notifications
asked Sep 3, 2009 at 21:23
[](https://superuser.com/users/8922/n179911)
[n179911](https://superuser.com/users/8922/n179911)
3,7531515 gold badges4444 silver badges4141 bronze badges
2
- 4
This question appears to be off-topic because it is about a programming tool. It belongs on Stack Overflow.
Peter Mortensen
– [Peter Mortensen](https://superuser.com/users/517/peter-mortensen "12,361 reputation")
2014-07-12 11:52:48 +00:00
[Commented Jul 12, 2014 at 11:52](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment1017369_35267)
- @PeterMortensen Agreed. This is of scope for SuperUser
Kellen Stuart
– [Kellen Stuart](https://superuser.com/users/503444/kellen-stuart "628 reputation")
2018-08-17 17:00:03 +00:00
[Commented Aug 17, 2018 at 17:00](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment2023602_35267)
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.") \|
## 10 Answers 10
Sorted by:
[Reset to default](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit?answertab=scoredesc#tab-top)
This answer is useful
111
Save this answer.
Show activity on this post.
The safest and probably cleanest way to go is to rebase interactively.
```
git rebase -i HEAD^^
```
Or,
```
git rebase -i baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e^
```
From there you can squash commits, which puts one or more commits together into the previous commit. To completely delete a commit from the history, delete the line from the list.
You can revert a commit with `git revert` but its going to add more commit messages to the history, which may be undesirable. Use the `-n` parameter to tell Git not to commit the revert right away. You can rebase interactively and squash those on up to a previous commmit to keep things clean.
If the two commits you're working with here affect the same file(s), you may see a merge conflict.
Resetting the repository with `git reset --hard` should be done with care, as it cannot be undone.
Rewriting history should be done with care.
[Share](https://superuser.com/a/35272 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this answer](https://superuser.com/posts/35272/edit)
Follow
Follow this answer to receive notifications
[edited Sep 4, 2009 at 5:54](https://superuser.com/posts/35272/revisions "show all edits to this post")
answered Sep 3, 2009 at 21:32
[](https://superuser.com/users/90/jtimberman)
[jtimberman](https://superuser.com/users/90/jtimberman)
21\.9k1212 gold badges7070 silver badges7878 bronze badges
6
- How can I delete a commit in during 'git rebase'? There are only 3 commands: pick, edit, squash. There is no delete AFAIK.
n179911
– [n179911](https://superuser.com/users/8922/n179911 "3,753 reputation")
2009-09-04 04:22:44 +00:00
[Commented Sep 4, 2009 at 4:22](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment35506_35272)
- 8
delete the line from the list entirely, and it deletes that commit
jtimberman
– [jtimberman](https://superuser.com/users/90/jtimberman "21,947 reputation")
2009-09-04 05:10:46 +00:00
[Commented Sep 4, 2009 at 5:10](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment35515_35272)
- when I wanted to revert the change I wanted to leave no trace of it. If you also want to leave no trace of your revert this is the best way to do it.
Phillip Whelan
– [Phillip Whelan](https://superuser.com/users/27775/phillip-whelan "101 reputation")
2011-04-14 01:52:05 +00:00
[Commented Apr 14, 2011 at 1:52](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment281346_35272)
- 1
"safest and probably cleanest way to go is to rebase interactively" Maybe not if you don't use vim. I got lucky and figured my way out of that mess. Still upvoting because I should learn vim :-)
isimmons
– [isimmons](https://superuser.com/users/161632/isimmons "141 reputation")
2013-12-13 03:42:13 +00:00
[Commented Dec 13, 2013 at 3:42](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment872962_35272)
- I would suggest use `rebase` as less as possible. If you work with others then just make `revert`.
magnump0
– [magnump0](https://superuser.com/users/167792/magnump0 "103 reputation")
2013-12-31 12:08:39 +00:00
[Commented Dec 31, 2013 at 12:08](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment884524_35272)
\| [Show **1** more comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Expand to show all comments on this post")
This answer is useful
70
Save this answer.
Show activity on this post.
This if from <http://nakkaya.com/2009/09/24/git-delete-last-commit/> and it worked for me
> Git Delete Last Commit
>
> Once in a while late at night when I ran out of coffee, I commit stuff that I shouldn't have. Then I spend the next 10 - 15 minutes googling how to remove the last commit I made. So after third time I wanted to make a record of it so I can refer to it later.
>
> If you have committed junk but not pushed,
>
> `git reset --hard HEAD~1`
>
> HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. Note that when using --hard any changes to tracked files in the working tree since the commit before head are lost.
>
> If you don't want to wipe out the work you have done, you can use `--soft` option that will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.
>
> Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,
>
> `git revert HEAD`
>
> This will create a new commit that reverses everything introduced by the accidental commit.
[Share](https://superuser.com/a/319587 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://superuser.com/posts/319587/edit)
Follow
Follow this answer to receive notifications
answered Aug 5, 2011 at 17:52
[](https://superuser.com/users/93073/istryker)
[iStryker](https://superuser.com/users/93073/istryker)
80166 silver badges33 bronze badges
1
- 2
With the --soft option, it's perfect for where you rename a file and accidentally "git commit -a" rather than using "git add" first.
Aaron Butacov
– [Aaron Butacov](https://superuser.com/users/40032/aaron-butacov "103 reputation")
2013-07-18 10:45:55 +00:00
[Commented Jul 18, 2013 at 10:45](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment772190_319587)
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
8
Save this answer.
Show activity on this post.
Nope. git-reset --hard will bring you back in history. What you're looking for is git revert, which will undo any commit.
[Share](https://superuser.com/a/35269 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this answer](https://superuser.com/posts/35269/edit)
Follow
Follow this answer to receive notifications
answered Sep 3, 2009 at 21:26
[](https://superuser.com/users/8746/peltier)
[Peltier](https://superuser.com/users/8746/peltier)
6,52488 gold badges3939 silver badges6464 bronze badges
1
- 6
Well, this works but it also pollutes you commit log. If these commits were not pushed/pulled already it's better to clean them up with an interactive rebase. The ability to change history is one of the main advantages of git over other version control systems.
knweiss
– [knweiss](https://superuser.com/users/498/knweiss "1,724 reputation")
2009-09-03 21:49:10 +00:00
[Commented Sep 3, 2009 at 21:49](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit#comment35391_35269)
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
6
Save this answer.
Show activity on this post.
I just done this:
```
git rebase -i HEAD^^
```
I screwed it up so I did
```
git rebase --abort
```
Then did it again. Then I had to push like this:
```
git push origin master -f
```
And it destroyed the commits newer than the commit I had rolled back to. Worked great.
[Share](https://superuser.com/a/367056 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://superuser.com/posts/367056/edit)
Follow
Follow this answer to receive notifications
answered Dec 12, 2011 at 4:39
[](https://superuser.com/users/36375/hamstar)
[hamstar](https://superuser.com/users/36375/hamstar)
52988 silver badges1111 bronze badges
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
5
Save this answer.
Show activity on this post.
No, `git reset --hard baf8d5e` will delete the `3368e1c` commit and HEAD will be at `baf8d5e` afterwards.
If you want to keep the `3368e1c` commit and delete the `bad8d5e` commit the easiest solution is to do a "`git rebase -i HEAD~2`" (i.e. interactive rebase of the last two commits). This command will launch your commit message editor and you'll see one line for each of the last two commits. There you just delete the `bad8d5e` commit line and save. git will then rewrite your history and the 2nd commit will be gone.
There are other useful commands you can use in the commit message editor like `squash`, `edit`, etc. Interactive rebase is VERY powerful\!
Do not do this if somebody already saw these commits (push or pull from your repository)\!
[Share](https://superuser.com/a/35279 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this answer](https://superuser.com/posts/35279/edit)
Follow
Follow this answer to receive notifications
answered Sep 3, 2009 at 21:46
[](https://superuser.com/users/498/knweiss)
[knweiss](https://superuser.com/users/498/knweiss)
1,72411 gold badge1313 silver badges1212 bronze badges
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
4
Save this answer.
Show activity on this post.
In reference to jtimberman's comment about `git reset --hard` being undoable, that's not entirely true. See here: <https://stackoverflow.com/questions/5473/undoing-a-git-reset-hard-head1>
[Share](https://superuser.com/a/101546 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/ "The current license for this post: CC BY-SA 2.5")
[Improve this answer](https://superuser.com/posts/101546/edit)
Follow
Follow this answer to receive notifications
[edited May 23, 2017 at 12:41](https://superuser.com/posts/101546/revisions "show all edits to this post")
[](https://superuser.com/users/-1/community)
[Community](https://superuser.com/users/-1/community)Bot
1
answered Jan 28, 2010 at 3:48
[](https://superuser.com/users/26397/mattd)
[mattd](https://superuser.com/users/26397/mattd)
4111 bronze badge
0
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
1
Save this answer.
Show activity on this post.
```
git reset --hard {ref}
```
is the only way to undo a commit if there is only one other commit in the repo (e.g. initial commit and 1 more). The rest of the ways (revert, rebase) refuse to work, at least as of git 1.7.5.1.
If you follow the `git reset` with a `git gc` then git will actually delete the old commit data from the repo completely.
[Share](https://superuser.com/a/395080 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://superuser.com/posts/395080/edit)
Follow
Follow this answer to receive notifications
answered Feb 28, 2012 at 20:17
[](https://superuser.com/users/120553/squeegee)
[squeegee](https://superuser.com/users/120553/squeegee)
11122 bronze badges
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
1
Save this answer.
Show activity on this post.
```
git checkout <treeish> -- /path/to/dir
```
That will bring back the directory from the given “treeish” for the /path/to/dir
[Share](https://superuser.com/a/486764 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://superuser.com/posts/486764/edit)
Follow
Follow this answer to receive notifications
[edited Oct 12, 2012 at 9:30](https://superuser.com/posts/486764/revisions "show all edits to this post")
[](https://superuser.com/users/48078/slhck)
[slhck](https://superuser.com/users/48078/slhck)
237k7373 gold badges639639 silver badges611611 bronze badges
answered Oct 12, 2012 at 9:23
[](https://superuser.com/users/164756/lokeshpahal)
[lokeshpahal](https://superuser.com/users/164756/lokeshpahal)
11933 bronze badges
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
0
Save this answer.
Show activity on this post.
I got this to work by manually editing the hash codes of the last commits from HEAD files inside the repository folder:
```
"centralRepository\refs\heads\master"
"centralRepository\refs\heads\branch2"
```
Before that, I was never able to push to origin the UNMERGE operations I did locally. It kept saying it "failed to push some refs" to Central Repository.
[Share](https://superuser.com/a/768178 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0")
[Improve this answer](https://superuser.com/posts/768178/edit)
Follow
Follow this answer to receive notifications
[edited Jun 13, 2014 at 1:35](https://superuser.com/posts/768178/revisions "show all edits to this post")
[](https://superuser.com/users/183377/cpast)
[cpast](https://superuser.com/users/183377/cpast)
2,53322 gold badges2222 silver badges2727 bronze badges
answered Jun 13, 2014 at 0:08
[](https://superuser.com/users/332930/leandro-m)
[Leandro M](https://superuser.com/users/332930/leandro-m)
1
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
This answer is useful
0
Save this answer.
Show activity on this post.
Or just fetch the remote branch on pull request:
```
git fetch origin/<name of branch on pull request> -b <name of branch on pull request (if making changes to same pull request)/name of another branch (e.g.: main)>
```
[Share](https://superuser.com/a/1704326 "Short permalink to this answer")
Share a link to this answer
Copy link
[CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0")
[Improve this answer](https://superuser.com/posts/1704326/edit)
Follow
Follow this answer to receive notifications
answered Feb 10, 2022 at 22:58
[](https://superuser.com/users/1666937/marvatron)
[marvatron](https://superuser.com/users/1666937/marvatron)
101
[Add a comment](https://superuser.com/questions/35267/how-can-i-roll-back-1-commit "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \|
## You must [log in](https://superuser.com/users/login?ssrc=question_page&returnurl=https%3A%2F%2Fsuperuser.com%2Fquestions%2F35267) to answer this question.
Start asking to get answers
Find the answer to your question by asking.
[Ask question](https://superuser.com/questions/ask)
Explore related questions
- [git](https://superuser.com/questions/tagged/git "show questions tagged 'git'")
See similar questions with these tags.
- The Overflow Blog
- [What’s new at Stack Overflow: February 2026](https://stackoverflow.blog/2026/02/02/what-s-new-at-stack-overflow-february-2026/?cb=1)
- [Generating text with diffusion (and ROI with LLMs)](https://stackoverflow.blog/2026/02/03/generating-text-with-diffusion-and-roi-with-llms/?cb=1)
- Featured on Meta
- [Results of the January 2026 Community Asks Sprint: Community Badges](https://meta.stackexchange.com/questions/417043/results-of-the-january-2026-community-asks-sprint-community-badges?cb=1)
- [All users on Stack Exchange can now participate in chat](https://meta.stackexchange.com/questions/417109/all-users-on-stack-exchange-can-now-participate-in-chat?cb=1)
#### Related
[6](https://superuser.com/questions/310010/go-back-in-time-and-create-branch?rq=1 "Question score (upvotes - downvotes)")
[Go back in time and create branch?](https://superuser.com/questions/310010/go-back-in-time-and-create-branch?rq=1)
[1](https://superuser.com/questions/451120/roll-back-git-master-repository-and-branch-commits-from-rollback-point?rq=1 "Question score (upvotes - downvotes)")
[Roll back git master repository and branch commits from rollback point](https://superuser.com/questions/451120/roll-back-git-master-repository-and-branch-commits-from-rollback-point?rq=1)
[23](https://superuser.com/questions/523963/how-can-i-revert-back-to-a-git-commit?rq=1 "Question score (upvotes - downvotes)")
[How can I revert back to a Git commit?](https://superuser.com/questions/523963/how-can-i-revert-back-to-a-git-commit?rq=1)
[0](https://superuser.com/questions/956402/extract-a-specific-revision-of-the-git-repository?rq=1 "Question score (upvotes - downvotes)")
[extract a specific revision of the git repository](https://superuser.com/questions/956402/extract-a-specific-revision-of-the-git-repository?rq=1)
[6](https://superuser.com/questions/1240754/use-output-of-command-as-exit-code?rq=1 "Question score (upvotes - downvotes)")
[Use output of command as exit code](https://superuser.com/questions/1240754/use-output-of-command-as-exit-code?rq=1)
[1](https://superuser.com/questions/1244415/replace-master-with-branch?rq=1 "Question score (upvotes - downvotes)")
[Replace master with branch](https://superuser.com/questions/1244415/replace-master-with-branch?rq=1)
#### [Hot Network Questions](https://stackexchange.com/questions?tab=hot)
- [Number of ways to arrange numbers 1–9 in 9 circles such that shaded circles are greater than their neighbors](https://math.stackexchange.com/questions/5122827/number-of-ways-to-arrange-numbers-1-9-in-9-circles-such-that-shaded-circles-are)
- [Where was Aemon Targaryen (Egg's brother) during the events of A Knight of the Seven Kingdoms?](https://scifi.stackexchange.com/questions/303114/where-was-aemon-targaryen-eggs-brother-during-the-events-of-a-knight-of-the-s)
- [Delta v in gravity assistance with impact parameter](https://astronomy.stackexchange.com/questions/62171/delta-v-in-gravity-assistance-with-impact-parameter)
- [Are two algebraic extensions with same polynomial root existence sentences isomorphic?](https://mathoverflow.net/questions/507741/are-two-algebraic-extensions-with-same-polynomial-root-existence-sentences-isomo)
- [DA 42 electric distribution question](https://aviation.stackexchange.com/questions/114921/da-42-electric-distribution-question)
- [Why does pulsar have a very powerful magnetic field (magnetosphere)?](https://physics.stackexchange.com/questions/868682/why-does-pulsar-have-a-very-powerful-magnetic-field-magnetosphere)
- [Possible typo in Bach Flute Partita in A minor](https://music.stackexchange.com/questions/143167/possible-typo-in-bach-flute-partita-in-a-minor)
- [What does this grace note/trill notation mean?](https://music.stackexchange.com/questions/143170/what-does-this-grace-note-trill-notation-mean)
- [Carcassone Castle Crossing pieces](https://boardgames.stackexchange.com/questions/64388/carcassone-castle-crossing-pieces)
- [How did Luther's Mariology evolve over time?](https://christianity.stackexchange.com/questions/112990/how-did-luthers-mariology-evolve-over-time)
- [App won't install on one of two "identical" tablets](https://android.stackexchange.com/questions/264944/app-wont-install-on-one-of-two-identical-tablets)
- [Decoding Overpass Ultra encoded query strings](https://gis.stackexchange.com/questions/499897/decoding-overpass-ultra-encoded-query-strings)
- [Where are My Chassidim?](https://judaism.stackexchange.com/questions/154999/where-are-my-chassidim)
- [e-scooter and fire risk](https://mechanics.stackexchange.com/questions/101907/e-scooter-and-fire-risk)
- [If the natural numbers are bijective onto themselves, how can they also be bijective onto the integers?](https://math.stackexchange.com/questions/5122505/if-the-natural-numbers-are-bijective-onto-themselves-how-can-they-also-be-bijec)
- [What does "Allow custom filters requiring trust" mean in uBlock Origin](https://superuser.com/questions/1934505/what-does-allow-custom-filters-requiring-trust-mean-in-ublock-origin)
- [Scoping the value of a l3keys option to the place it is used (part 2)](https://tex.stackexchange.com/questions/759145/scoping-the-value-of-a-l3keys-option-to-the-place-it-is-used-part-2)
- [How do atoms and molecules form structure without quantum measurements on position?](https://physics.stackexchange.com/questions/868654/how-do-atoms-and-molecules-form-structure-without-quantum-measurements-on-positi)
- [Does this search algorithm have a name](https://cs.stackexchange.com/questions/176156/does-this-search-algorithm-have-a-name)
- [procmail not matching email header if it is broken to multiple lines](https://superuser.com/questions/1934492/procmail-not-matching-email-header-if-it-is-broken-to-multiple-lines)
- [B-equivariant implies G-equivariant](https://mathoverflow.net/questions/507681/b-equivariant-implies-g-equivariant)
- [How to add fermata symbol to metre package?](https://tex.stackexchange.com/questions/759162/how-to-add-fermata-symbol-to-metre-package)
- [NIntegrate error estimates become wrong with Exclusions](https://mathematica.stackexchange.com/questions/318631/nintegrate-error-estimates-become-wrong-with-exclusions)
- [pgfplotstable: hide 'leading zeros' and values '1.0000'](https://tex.stackexchange.com/questions/759143/pgfplotstable-hide-leading-zeros-and-values-1-0000)
[Question feed](https://superuser.com/feeds/question/35267 "Feed of this question and its answers")
# Subscribe to RSS
Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

# Why are you flagging this comment?
It contains harassment, bigotry or abuse.
This comment attacks a person or group. Learn more in our [Abusive behavior policy](https://superuser.com/conduct/abusive-behavior).
It's unfriendly or unkind.
This comment is rude or condescending. Learn more in our [Code of Conduct](https://superuser.com/conduct/abusive-behavior).
Not needed.
This comment is not relevant to the post.
```
```
Enter at least 6 characters
Something else.
A problem not listed above. Try to be as specific as possible.
```
```
Enter at least 6 characters
Flag comment
Cancel
You have 0 flags left today
# 
# Hang on, you can't upvote just yet.
You'll need to complete a few actions and gain 15 reputation points before being able to upvote. **Upvoting** indicates when questions and answers are useful. [What's reputation and how do I get it?](https://stackoverflow.com/help/whats-reputation)
Instead, you can save this post to reference later.
Save this post for later
Not now
##### [Super User](https://superuser.com/)
- [Tour](https://superuser.com/tour)
- [Help](https://superuser.com/help)
- [Chat](https://chat.stackexchange.com/?tab=site&host=superuser.com)
- [Contact](https://superuser.com/contact)
- [Feedback](https://meta.superuser.com/)
##### [Company](https://stackoverflow.co/)
- [Stack Overflow](https://stackoverflow.com/)
- [Stack Internal](https://stackoverflow.co/internal/)
- [Stack Data Licensing](https://stackoverflow.co/data-licensing/)
- [Stack Ads](https://stackoverflow.co/advertising/)
- [About](https://stackoverflow.co/)
- [Press](https://stackoverflow.co/company/press/)
- [Legal](https://stackoverflow.com/legal)
- [Privacy Policy](https://stackoverflow.com/legal/privacy-policy)
- [Terms of Service](https://stackoverflow.com/legal/terms-of-service/public)
- Cookie Settings
- [Cookie Policy](https://policies.stackoverflow.co/stack-overflow/cookie-policy)
##### [Stack Exchange Network](https://stackexchange.com/)
- [Technology](https://stackexchange.com/sites#technology)
- [Culture & recreation](https://stackexchange.com/sites#culturerecreation)
- [Life & arts](https://stackexchange.com/sites#lifearts)
- [Science](https://stackexchange.com/sites#science)
- [Professional](https://stackexchange.com/sites#professional)
- [Business](https://stackexchange.com/sites#business)
- [API](https://api.stackexchange.com/)
- [Data](https://data.stackexchange.com/)
- [Blog](https://stackoverflow.blog/?blb=1)
- [Facebook](https://www.facebook.com/officialstackoverflow/)
- [Twitter](https://twitter.com/stackoverflow)
- [LinkedIn](https://linkedin.com/company/stack-overflow)
- [Instagram](https://www.instagram.com/thestackoverflow)
Site design / logo © 2026 Stack Exchange Inc; user contributions licensed under [CC BY-SA](https://stackoverflow.com/help/licensing) . rev 2026.2.2.39324 |
| Readable Markdown | This question shows research effort; it is useful and clear
159
Save this question.
Show activity on this post.
I have 2 commits that I did not push:
```
$ git status
# On branch master
# Your branch is ahead of 'faves/master' by 2 commits.
```
How can I roll back my first one (the oldest one), but keep the second one?
```
$ git log
commit 3368e1c5b8a47135a34169c885e8dd5ba01af5bb
...
commit baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
...
```
From here:
<http://friendfeed.com/harijay/742631ff/git-question-how-do-i-rollback-commit-just-want>
Do I just need to do:
```
git reset --hard baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
```
That is?
asked Sep 3, 2009 at 21:23
[](https://superuser.com/users/8922/n179911)
2
This answer is useful
111
Save this answer.
Show activity on this post.
The safest and probably cleanest way to go is to rebase interactively.
```
git rebase -i HEAD^^
```
Or,
```
git rebase -i baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e^
```
From there you can squash commits, which puts one or more commits together into the previous commit. To completely delete a commit from the history, delete the line from the list.
You can revert a commit with `git revert` but its going to add more commit messages to the history, which may be undesirable. Use the `-n` parameter to tell Git not to commit the revert right away. You can rebase interactively and squash those on up to a previous commmit to keep things clean.
If the two commits you're working with here affect the same file(s), you may see a merge conflict.
Resetting the repository with `git reset --hard` should be done with care, as it cannot be undone.
Rewriting history should be done with care.
answered Sep 3, 2009 at 21:32
[](https://superuser.com/users/90/jtimberman)
6
This answer is useful
70
Save this answer.
Show activity on this post.
This if from <http://nakkaya.com/2009/09/24/git-delete-last-commit/> and it worked for me
> Git Delete Last Commit
>
> Once in a while late at night when I ran out of coffee, I commit stuff that I shouldn't have. Then I spend the next 10 - 15 minutes googling how to remove the last commit I made. So after third time I wanted to make a record of it so I can refer to it later.
>
> If you have committed junk but not pushed,
>
> `git reset --hard HEAD~1`
>
> HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. Note that when using --hard any changes to tracked files in the working tree since the commit before head are lost.
>
> If you don't want to wipe out the work you have done, you can use `--soft` option that will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.
>
> Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,
>
> `git revert HEAD`
>
> This will create a new commit that reverses everything introduced by the accidental commit.
answered Aug 5, 2011 at 17:52
[](https://superuser.com/users/93073/istryker)
1
This answer is useful
8
Save this answer.
Show activity on this post.
Nope. git-reset --hard will bring you back in history. What you're looking for is git revert, which will undo any commit.
answered Sep 3, 2009 at 21:26
[](https://superuser.com/users/8746/peltier)
1
This answer is useful
6
Save this answer.
Show activity on this post.
I just done this:
```
git rebase -i HEAD^^
```
I screwed it up so I did
```
git rebase --abort
```
Then did it again. Then I had to push like this:
```
git push origin master -f
```
And it destroyed the commits newer than the commit I had rolled back to. Worked great.
answered Dec 12, 2011 at 4:39
[](https://superuser.com/users/36375/hamstar)
This answer is useful
5
Save this answer.
Show activity on this post.
No, `git reset --hard baf8d5e` will delete the `3368e1c` commit and HEAD will be at `baf8d5e` afterwards.
If you want to keep the `3368e1c` commit and delete the `bad8d5e` commit the easiest solution is to do a "`git rebase -i HEAD~2`" (i.e. interactive rebase of the last two commits). This command will launch your commit message editor and you'll see one line for each of the last two commits. There you just delete the `bad8d5e` commit line and save. git will then rewrite your history and the 2nd commit will be gone.
There are other useful commands you can use in the commit message editor like `squash`, `edit`, etc. Interactive rebase is VERY powerful\!
Do not do this if somebody already saw these commits (push or pull from your repository)\!
answered Sep 3, 2009 at 21:46
[](https://superuser.com/users/498/knweiss)
This answer is useful
1
Save this answer.
Show activity on this post.
```
git reset --hard {ref}
```
is the only way to undo a commit if there is only one other commit in the repo (e.g. initial commit and 1 more). The rest of the ways (revert, rebase) refuse to work, at least as of git 1.7.5.1.
If you follow the `git reset` with a `git gc` then git will actually delete the old commit data from the repo completely.
answered Feb 28, 2012 at 20:17
[](https://superuser.com/users/120553/squeegee)
This answer is useful
1
Save this answer.
Show activity on this post.
```
git checkout <treeish> -- /path/to/dir
```
That will bring back the directory from the given “treeish” for the /path/to/dir
[](https://superuser.com/users/48078/slhck)
[slhck](https://superuser.com/users/48078/slhck)
237k73 gold badges639 silver badges611 bronze badges
answered Oct 12, 2012 at 9:23
[](https://superuser.com/users/164756/lokeshpahal)
This answer is useful
0
Save this answer.
Show activity on this post.
I got this to work by manually editing the hash codes of the last commits from HEAD files inside the repository folder:
```
"centralRepository\refs\heads\master"
"centralRepository\refs\heads\branch2"
```
Before that, I was never able to push to origin the UNMERGE operations I did locally. It kept saying it "failed to push some refs" to Central Repository.
[](https://superuser.com/users/183377/cpast)
[cpast](https://superuser.com/users/183377/cpast)
2,5332 gold badges22 silver badges27 bronze badges
answered Jun 13, 2014 at 0:08
[](https://superuser.com/users/332930/leandro-m)
This answer is useful
0
Save this answer.
Show activity on this post.
Or just fetch the remote branch on pull request:
```
git fetch origin/<name of branch on pull request> -b <name of branch on pull request (if making changes to same pull request)/name of another branch (e.g.: main)>
```
answered Feb 10, 2022 at 22:58
[](https://superuser.com/users/1666937/marvatron)
## You must [log in](https://superuser.com/users/login?ssrc=question_page&returnurl=https%3A%2F%2Fsuperuser.com%2Fquestions%2F35267) to answer this question.
Start asking to get answers
Find the answer to your question by asking.
[Ask question](https://superuser.com/questions/ask)
Explore related questions
See similar questions with these tags. |
| Shard | 122 (laksa) |
| Root Hash | 3131140398176769522 |
| Unparsed URL | com,superuser!/questions/35267/how-can-i-roll-back-1-commit s443 |