Merge some snippets

This commit is contained in:
Angelos Chalaris
2023-05-27 12:53:39 +03:00
parent 9d1f49511a
commit de07ed705b
2 changed files with 33 additions and 38 deletions

View File

@ -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
```

View File

@ -1,26 +1,47 @@
---
title: Rewind back to a specific commit
type: snippet
title: Rewind back to a specific commit in Git
shortTitle: Rewind to commit
type: story
language: git
tags: [branch,commit]
author: chalarangelo
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>`.
- 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.
### Rewind to a commit
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
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
git reset 3050fc0d3
# Rewinds back to `3050fc0d3` but keeps changes in the working directory
# Syntax: git reset [--hard] HEAD~<n>
git reset --hard c0d30f305
# Rewinds back to `c0d30f305` and deletes changes
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
```
### 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).