From 9d1f49511a97afc3e79b7dcca4335daf21eb002d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 27 May 2023 12:53:35 +0300 Subject: [PATCH] Merge some snippets --- .../undo-commit-without-rewriting-history.md | 37 +++++++++++++++++++ snippets/git/s/undo-commit.md | 21 ----------- snippets/git/s/undo-last-commit.md | 21 ----------- 3 files changed, 37 insertions(+), 42 deletions(-) create mode 100644 snippets/git/s/undo-commit-without-rewriting-history.md delete mode 100644 snippets/git/s/undo-commit.md delete mode 100644 snippets/git/s/undo-last-commit.md diff --git a/snippets/git/s/undo-commit-without-rewriting-history.md b/snippets/git/s/undo-commit-without-rewriting-history.md new file mode 100644 index 000000000..8d48f2bf8 --- /dev/null +++ b/snippets/git/s/undo-commit-without-rewriting-history.md @@ -0,0 +1,37 @@ +--- +title: Undo a commit in Git +shortTitle: Undo commit +type: story +language: git +tags: [commit,branch] +author: chalarangelo +cover: night-tram +excerpt: Learn the simple way to undo a commit in Git without rewriting history. +dateModified: 2023-05-27T08:23:17+03:00 +--- + +It's not uncommon to make a mistake when committing changes to a repository. When you realize something went wrong, you might not be able to [rewind the changes](/git/s/rewind-to-commit) you made, especially if you've already pushed them to a remote repository. In that case, you'll want to **undo the commit**, without rewriting history. + +### Revert a commit + +As you might have guessed, `git revert` is the command you're looking for. Using this command, you can **revert a commit**, creating a new commit with the inverse of the commit's changes. + +```shell +# Syntax: git revert + +git revert 3050fc0 +# Reverts the commit `3050fc0` and creates a new commit +# with the inverse of its changes +``` + +### Revert the last commit + +The **latest commit** can be references using the `HEAD` pointer. So, to revert the last commit, you can simply use `git revert HEAD`. + +```shell +# Syntax: git revert HEAD + +git revert HEAD +# Reverts the last commit and creates a new commit +# with the inverse of its changes +``` diff --git a/snippets/git/s/undo-commit.md b/snippets/git/s/undo-commit.md deleted file mode 100644 index d4a3e3246..000000000 --- a/snippets/git/s/undo-commit.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -title: Undo a commit -type: snippet -language: git -tags: [commit,branch] -cover: night-tram -dateModified: 2021-04-13T21:10:59+03:00 ---- - -Undoes a specified commit without rewriting history. - -- Use `git revert ` to revert the specified ``, creating a new commit with the inverse of the commit's changes. - -```shell -git revert -``` - -```shell -git revert 3050fc0d3 -# Reverts the commit `3050fc0d3` -``` diff --git a/snippets/git/s/undo-last-commit.md b/snippets/git/s/undo-last-commit.md deleted file mode 100644 index 7608b033b..000000000 --- a/snippets/git/s/undo-last-commit.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -title: Undo the last commit -type: snippet -language: git -tags: [commit,branch] -cover: racoon -dateModified: 2021-04-13T21:10:59+03:00 ---- - -Undoes the last commit without rewriting history. - -- Use `git revert HEAD` to revert the last commit, creating a new commit with the inverse of the commit's changes. - -```shell -git revert HEAD -``` - -```shell -git revert HEAD -# Reverts the last commit -```