Merge some snippets
This commit is contained in:
@ -1,26 +0,0 @@
|
|||||||
---
|
|
||||||
title: Rewind back n commits
|
|
||||||
type: snippet
|
|
||||||
language: git
|
|
||||||
tags: [branch,commit]
|
|
||||||
cover: lake-trees
|
|
||||||
dateModified: 2021-04-13T21:10:59+03:00
|
|
||||||
---
|
|
||||||
|
|
||||||
Rewinds the current branch by a given number of commits.
|
|
||||||
|
|
||||||
- Use `git reset HEAD~<n>` to rewind the current branch `<n>` commits.
|
|
||||||
- This command will uncommit and unstage changes, but leave them in the working directory.
|
|
||||||
- You can use the `--hard` flag to uncommit, unstage and delete changes instead.
|
|
||||||
|
|
||||||
```shell
|
|
||||||
git reset [--hard] HEAD~<n>
|
|
||||||
```
|
|
||||||
|
|
||||||
```shell
|
|
||||||
git reset HEAD~5
|
|
||||||
# Rewinds back 5 commits but keeps changes in the working directory
|
|
||||||
|
|
||||||
git reset --hard HEAD~3
|
|
||||||
# Rewinds back 3 commits and deletes changes
|
|
||||||
```
|
|
||||||
@ -1,26 +1,47 @@
|
|||||||
---
|
---
|
||||||
title: Rewind back to a specific commit
|
title: Rewind back to a specific commit in Git
|
||||||
type: snippet
|
shortTitle: Rewind to commit
|
||||||
|
type: story
|
||||||
language: git
|
language: git
|
||||||
tags: [branch,commit]
|
tags: [branch,commit]
|
||||||
|
author: chalarangelo
|
||||||
cover: walking
|
cover: walking
|
||||||
dateModified: 2021-04-13T21:10:59+03:00
|
excerpt: Did you make a mistake but haven't pushed your changes yet? Learn how to rewind back to a specific commit in Git.
|
||||||
|
dateModified: 2023-05-26T21:10:59+03:00
|
||||||
---
|
---
|
||||||
|
|
||||||
Rewinds the current branch by a given number of commits.
|
One of Git's greatest strengths is its ability to **rewind back to a specific commit**. This is especially useful when you've made a mistake but haven't pushed your changes yet. In that case, you can simply rewind back to a previous commit, fix your mistake and commit again.
|
||||||
|
|
||||||
- Use `git reset <commit>` to rewind the current branch to the specified `<commit>`.
|
### Rewind to a commit
|
||||||
- This command will uncommit and unstage changes, but leave them in the working directory.
|
|
||||||
- You can use the `--hard` flag to uncommit, unstage and delete changes instead.
|
To rewind back to a specific commit, you can use `git reset`. This command will **uncommit and unstage changes**, but leave them in the working directory. You can use the `--hard` flag to **uncommit, unstage and delete** changes instead.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
git reset [--hard] <commit>
|
# Syntax: git reset [--hard] <commit>
|
||||||
|
|
||||||
|
git reset 3050fc0
|
||||||
|
# Rewinds back to `3050fc0` but keeps changes in the working directory
|
||||||
|
|
||||||
|
git reset --hard c0d30f3
|
||||||
|
# Rewinds back to `c0d30f3` and deletes changes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rewind back n commits
|
||||||
|
|
||||||
|
You can also use `git reset` to rewind back a **given number of commits**. To do so, you can use the `HEAD~<n>` syntax, where `<n>` is the number of commits you want to rewind back.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
git reset 3050fc0d3
|
# Syntax: git reset [--hard] HEAD~<n>
|
||||||
# Rewinds back to `3050fc0d3` but keeps changes in the working directory
|
|
||||||
|
|
||||||
git reset --hard c0d30f305
|
git reset HEAD~5
|
||||||
# Rewinds back to `c0d30f305` and deletes changes
|
# Rewinds back 5 commits but keeps changes in the working directory
|
||||||
|
|
||||||
|
git reset --hard HEAD~3
|
||||||
|
# Rewinds back 3 commits and deletes changes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Notes
|
||||||
|
|
||||||
|
The `--hard` flag is considered a destructive action, which means you should be extra careful when using it. If things go wrong, you might be able to recover your changes by [viewing the reference log](/git/s/view-undo-history).
|
||||||
|
|
||||||
|
In case you've already pushed some changes to a remote repository, you might not want to rewrite history, especially if other people have already pulled your changes. In that case, you can use `git revert` to [undo a commit without rewriting history](/git/s/undo-commit-without-rewriting-history).
|
||||||
|
|||||||
Reference in New Issue
Block a user