|
| 1 | +--- |
| 2 | +Title: 'Checkout' |
| 3 | +Description: 'The git checkout command switches, creates and restores branches in the working directory to a specific state.' |
| 4 | +Subjects: |
| 5 | + - 'Bash/Shell' |
| 6 | + - 'Developer Tools' |
| 7 | +Tags: |
| 8 | + - 'Git' |
| 9 | + - 'GitHub' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-git' |
| 12 | + - 'learn-the-command-line' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`git checkout`** command switches, creates and restores branches in the working directory to a specific state. The `git checkout` command also allows switching to a specific commit without changing branches. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +Checkout with branches: |
| 21 | + |
| 22 | +```pseudo |
| 23 | +git checkout [options] <branch-name> |
| 24 | +``` |
| 25 | + |
| 26 | +- `<branch-name>` specifies the name of the branch to switch to or create. |
| 27 | +- `[options]` optional flags that can be used with the checkout command. Here are some of the most commonly used options: |
| 28 | + - `-b` creates a new branch with the specified name and switches to it immediately. |
| 29 | + - `-` returns to the previously checked-out branch. This flag does not need the `<branch-name>`. |
| 30 | + - `-f` (--force) forces the checkout, discarding any local changes in the working directory. |
| 31 | + |
| 32 | +Checkout to a specific commit: |
| 33 | + |
| 34 | +```pseudo |
| 35 | +git checkout <commit-hash> |
| 36 | +``` |
| 37 | + |
| 38 | +## Switch to an existing branch |
| 39 | + |
| 40 | +The following command will switch to an already existing branch, created previously with the [git branch](https://www.codecademy.com/resources/docs/git/branch) command: |
| 41 | + |
| 42 | +```pseudo |
| 43 | +git checkout existing-branch |
| 44 | +``` |
| 45 | + |
| 46 | +> **Note**: From Git 2.23, the new specific `git switch` command has been introduced to switch branches, making it clearer and safer than `git checkout` because it avoids the ambiguity of the latter's multi-purpose nature. |
| 47 | +
|
| 48 | +## Create and switch to a new branch |
| 49 | + |
| 50 | +It is possible to create and switch to a new branch with a single command using the `-b` option: |
| 51 | + |
| 52 | +```pseudo |
| 53 | +git checkout -b new-branch |
| 54 | +``` |
| 55 | + |
| 56 | +## Restore a file from a specific commit |
| 57 | + |
| 58 | +`git checkout` also allows to restore a file from a specific commit using its hash: |
| 59 | + |
| 60 | +```pseudo |
| 61 | +git checkout <commit-hash> -- example.txt |
| 62 | +``` |
| 63 | + |
| 64 | +## Examine a Previous Commit |
| 65 | + |
| 66 | +`git checkout` also allows temporarily moving to a specific commit without changing branches. This state is called **detached `HEAD` state**: |
| 67 | + |
| 68 | +```pseudo |
| 69 | +git checkout <commit-hash> |
| 70 | +``` |
| 71 | + |
| 72 | +The detached `HEAD` state allows to: |
| 73 | + |
| 74 | +- Examine the state of the repository at that specified commit. |
| 75 | +- Create new branches if the developer needs to start from that point. |
| 76 | +- Any code changes made in this state will not be associated with any existing branch unless a new branch is created. |
0 commit comments