From de07ed705b751e971a7ea0aed94fe644fbd77f1f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 27 May 2023 12:53:39 +0300 Subject: [PATCH] Merge some snippets --- snippets/git/s/rewind-n-commits.md | 26 ----------------- snippets/git/s/rewind-to-commit.md | 45 ++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 38 deletions(-) delete mode 100644 snippets/git/s/rewind-n-commits.md diff --git a/snippets/git/s/rewind-n-commits.md b/snippets/git/s/rewind-n-commits.md deleted file mode 100644 index 1a74155f2..000000000 --- a/snippets/git/s/rewind-n-commits.md +++ /dev/null @@ -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~` to rewind the current branch `` 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~ -``` - -```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 -``` diff --git a/snippets/git/s/rewind-to-commit.md b/snippets/git/s/rewind-to-commit.md index baebcc2c9..b03593bb3 100644 --- a/snippets/git/s/rewind-to-commit.md +++ b/snippets/git/s/rewind-to-commit.md @@ -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 ` to rewind the current branch to the specified ``. -- 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] +# Syntax: git reset [--hard] + +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~` syntax, where `` 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~ -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).