From e61491196b11129d7c9a610109bef6d23d9e527e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 27 May 2023 12:31:09 +0300 Subject: [PATCH 1/3] Merge some snippets --- ...{fast-forward.md => fast-forward-merge.md} | 2 +- snippets/git/s/merge-branch-merge-commit.md | 34 ++++++++++++++----- snippets/git/s/merge-branch.md | 23 ------------- 3 files changed, 27 insertions(+), 32 deletions(-) rename snippets/git/s/{fast-forward.md => fast-forward-merge.md} (98%) delete mode 100644 snippets/git/s/merge-branch.md diff --git a/snippets/git/s/fast-forward.md b/snippets/git/s/fast-forward-merge.md similarity index 98% rename from snippets/git/s/fast-forward.md rename to snippets/git/s/fast-forward-merge.md index bb51fc28a..29a24e60b 100644 --- a/snippets/git/s/fast-forward.md +++ b/snippets/git/s/fast-forward-merge.md @@ -1,6 +1,6 @@ --- title: How does Git's fast-forward mode work? -shortTitle: Git fast-forward +shortTitle: Fast-forward merge type: question language: git tags: [branch] diff --git a/snippets/git/s/merge-branch-merge-commit.md b/snippets/git/s/merge-branch-merge-commit.md index ed8f4ee7d..e890084fd 100644 --- a/snippets/git/s/merge-branch-merge-commit.md +++ b/snippets/git/s/merge-branch-merge-commit.md @@ -1,23 +1,41 @@ --- -title: Merge a branch and create a merge commit -type: snippet +title: Merge a branch in Git +shortTitle: Merge branch +type: story language: git tags: [repository,branch] +author: chalarangelo cover: meteora -dateModified: 2021-04-13T21:10:59+03:00 +excerpt: Learn how to merge a branch in Git with or without creating a merge commit, depending on your team's workflow. +dateModified: 2023-05-26T21:10:59+03:00 --- -Merges a branch into the current branch, creating a merge commit. +Branches are Git's way to organize separate lines of development, allowing a team to work multiple features in parallel. But at some point, you'll want to **merge a branch into another branch**, usually `master` or `main`. Depending on your team's workflow, merging a branch might create a merge commit or not. -- Use `git checkout ` to switch to the branch into which you want to merge. -- Use `git merge --no-ff -m ` to merge a branch into the current branch, creating a merge commit with the specified ``. +### Merging a branch + +In order to merge a branch, you want to **switch to the target branch** first, using `git checkout`. Then, you can use `git merge` to **merge the source branch into the target branch**. ```shell -git checkout -git merge --no-ff -m +# Syntax: +# git checkout +# git merge + +git checkout master +git merge patch-1 # Merges the `patch-1` branch into `master` ``` +By default, Git will use [fast-forward merge](/git/s/fast-forward-merge) to merge the branch. This means that it will create a **linear history**, by placing the commits from the source branch at the tip of the target branch. + +### Creating a merge commit + +If, instead, you want to **create a merge commit**, you can use the `--no-ff` flag when merging. This will create a merge commit at the tip of the target branch, optionally referencing the source branch in the commit message. The rest of the process remains the same. + ```shell +# Syntax: +# git checkout +# git merge --no-ff -m + git checkout master git merge --no-ff -m "Merge patch-1" patch-1 # Merges the `patch-1` branch into `master` and creates a commit diff --git a/snippets/git/s/merge-branch.md b/snippets/git/s/merge-branch.md deleted file mode 100644 index e6c19b75b..000000000 --- a/snippets/git/s/merge-branch.md +++ /dev/null @@ -1,23 +0,0 @@ ---- -title: Merge a branch -type: snippet -language: git -tags: [repository,branch] -cover: sparkles -dateModified: 2021-04-13T21:10:59+03:00 ---- - -Merges a branch into the current branch. - -- Use `git checkout ` to switch to the branch into which you want to merge. -- Use `git merge ` to merge a branch into the current branch. - -```shell -git checkout -git merge -``` - -```shell -git checkout master -git merge patch-1 # Merges the `patch-1` branch into `master` -``` From 9d1f49511a97afc3e79b7dcca4335daf21eb002d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 27 May 2023 12:53:35 +0300 Subject: [PATCH 2/3] 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 -``` From de07ed705b751e971a7ea0aed94fe644fbd77f1f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 27 May 2023 12:53:39 +0300 Subject: [PATCH 3/3] 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).