Convert git snippets to stories (4) (#1943)
This commit is contained in:
@ -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]
|
||||
@ -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 <target-branch>` to switch to the branch into which you want to merge.
|
||||
- Use `git merge --no-ff -m <message> <source-branch>` to merge a branch into the current branch, creating a merge commit with the specified `<message>`.
|
||||
### 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 <target-branch>
|
||||
git merge --no-ff -m <message> <source-branch>
|
||||
# Syntax:
|
||||
# git checkout <target-branch>
|
||||
# git merge <source-branch>
|
||||
|
||||
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 <target-branch>
|
||||
# git merge --no-ff -m <message> <source-branch>
|
||||
|
||||
git checkout master
|
||||
git merge --no-ff -m "Merge patch-1" patch-1
|
||||
# Merges the `patch-1` branch into `master` and creates a commit
|
||||
|
||||
@ -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 <target-branch>` to switch to the branch into which you want to merge.
|
||||
- Use `git merge <source-branch>` to merge a branch into the current branch.
|
||||
|
||||
```shell
|
||||
git checkout <target-branch>
|
||||
git merge <source-branch>
|
||||
```
|
||||
|
||||
```shell
|
||||
git checkout master
|
||||
git merge patch-1 # Merges the `patch-1` branch into `master`
|
||||
```
|
||||
@ -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
|
||||
```
|
||||
@ -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).
|
||||
|
||||
37
snippets/git/s/undo-commit-without-rewriting-history.md
Normal file
37
snippets/git/s/undo-commit-without-rewriting-history.md
Normal file
@ -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 <commit>
|
||||
|
||||
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
|
||||
```
|
||||
@ -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 <commit>` to revert the specified `<commit>`, creating a new commit with the inverse of the commit's changes.
|
||||
|
||||
```shell
|
||||
git revert <commit>
|
||||
```
|
||||
|
||||
```shell
|
||||
git revert 3050fc0d3
|
||||
# Reverts the commit `3050fc0d3`
|
||||
```
|
||||
@ -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
|
||||
```
|
||||
Reference in New Issue
Block a user