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