ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.freecodecamp.org/news/git-checkout-explained/ |
| Last Crawled | 2026-04-12 15:14:26 (6 hours ago) |
| First Indexed | 2020-02-03 03:48:40 (6 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Git Checkout Explained: How to Checkout, Change, or Switch a Branch in Git |
| Meta Description | The git checkout command switches between branches or restores working tree files. There are a number of different options for this command that won’t be covered here, but you can take a look at all of them in the Git documentation. Checkout a specif... |
| Meta Canonical | null |
| Boilerpipe Text | The
git checkout
command switches between branches or restores working tree files. There are a number of different options for this command that won’t be covered here, but you can take a look at all of them in the
Git documentation
.
Checkout a specific commit
to checkout a specific commit, run the command :
git
checkout specific-commit-id
we can get the specific commit id’s by running:
git
log
Checkout an Existing Branch
To checkout an existing branch, run the command:
git
checkout BRANCH-NAME
Generally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes: 1) trash them, 2)
commit them
, or 3)
stash them
.
Checkout a New Branch
To create and checkout out a new branch with a single command, you can use:
git
checkout
-b
NEW-BRANCH-NAME
This will automatically switch you to the new branch.
Checkout a New Branch or Reset a Branch to a Start Point
The following command is similar to checking out a new branch, but uses the
-B
(note the captital B) flag and an optional
START-POINT
parameter:
git
checkout
-B
BRANCH-NAME START-POINT
If the
BRANCH-NAME
branch doesn’t exist, Git will create it and start it at
START-POINT
. If the
BRANCH-NAME
branch already exists, then Git resets the branch to
START-POINT
. This is equivalent to running
git branch
with
-f
.
Force a Checkout
You can pass the
-f
or
--force
option with the
git checkout
command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from
HEAD
). Basically, it can be used to throw away local changes.
When you run the following command, Git will ignore unmerged entries:
git
checkout
-f
BRANCH-NAME
# Alternative
git
checkout
--force
BRANCH-NAME
Undo Changes in your Working Directory
You can use the
git checkout
command to undo changes you’ve made to a file in your working directory. This will revert the file back to the version in
HEAD
:
git
checkout -- FILE-NAME
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers.
Get started |
| Markdown | [](https://www.freecodecamp.org/news/)
Menu Menu
- [Forum](https://forum.freecodecamp.org/)
- [Curriculum](https://www.freecodecamp.org/learn)
[Donate](https://www.freecodecamp.org/donate/)
[Learn to code — free 3,000-hour curriculum](https://www.freecodecamp.org/)
December 31, 2019
/ [\#Git](https://www.freecodecamp.org/news/tag/git/)
# Git Checkout Explained: How to Checkout, Change, or Switch a Branch in Git

The `git checkout` command switches between branches or restores working tree files. There are a number of different options for this command that won’t be covered here, but you can take a look at all of them in the [Git documentation](https://git-scm.com/docs/git-checkout).
### **Checkout a specific commit**
to checkout a specific commit, run the command :
```
git checkout specific-commit-id
```
we can get the specific commit id’s by running:
```
git log
```
### **Checkout an Existing Branch**
To checkout an existing branch, run the command:
```
git checkout BRANCH-NAME
```
Generally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes: 1) trash them, 2) [commit them](https://guide.freecodecamp.org/git/git-commit/), or 3) [stash them](https://guide.freecodecamp.org/git/git-stash/).
### **Checkout a New Branch**
To create and checkout out a new branch with a single command, you can use:
```
git checkout -b NEW-BRANCH-NAME
```
This will automatically switch you to the new branch.
### **Checkout a New Branch or Reset a Branch to a Start Point**
The following command is similar to checking out a new branch, but uses the `-B` (note the captital B) flag and an optional `START-POINT` parameter:
```
git checkout -B BRANCH-NAME START-POINT
```
If the `BRANCH-NAME` branch doesn’t exist, Git will create it and start it at `START-POINT`. If the `BRANCH-NAME` branch already exists, then Git resets the branch to `START-POINT`. This is equivalent to running `git branch` with `-f`.
### **Force a Checkout**
You can pass the `-f` or `--force` option with the `git checkout` command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from `HEAD`). Basically, it can be used to throw away local changes.
When you run the following command, Git will ignore unmerged entries:
```
git checkout -f BRANCH-NAME
# Alternative
git checkout --force BRANCH-NAME
```
### **Undo Changes in your Working Directory**
You can use the `git checkout` command to undo changes you’ve made to a file in your working directory. This will revert the file back to the version in `HEAD`:
```
git checkout -- FILE-NAME
```
***
If you read this far, thank the author to show them you care. Say Thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn)
ADVERTISEMENT
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
You can [make a tax-deductible donation here](https://www.freecodecamp.org/donate/).
## Trending Books and Handbooks
- [REST APIs](https://www.freecodecamp.org/news/build-consume-and-document-a-rest-api/)
- [Clean Code](https://www.freecodecamp.org/news/how-to-write-clean-code/)
- [TypeScript](https://www.freecodecamp.org/news/learn-typescript-with-react-handbook/)
- [JavaScript](https://www.freecodecamp.org/news/learn-javascript-for-beginners/)
- [AI Chatbots](https://www.freecodecamp.org/news/how-to-build-an-ai-chatbot-with-redis-python-and-gpt/)
- [Command Line](https://www.freecodecamp.org/news/command-line-for-beginners/)
- [GraphQL APIs](https://www.freecodecamp.org/news/building-consuming-and-documenting-a-graphql-api/)
- [CSS Transforms](https://www.freecodecamp.org/news/complete-guide-to-css-transform-functions-and-properties/)
- [Access Control](https://www.freecodecamp.org/news/how-to-build-scalable-access-control-for-your-web-app/)
- [REST API Design](https://www.freecodecamp.org/news/rest-api-design-best-practices-build-a-rest-api/)
- [PHP](https://www.freecodecamp.org/news/the-php-handbook/)
- [Java](https://www.freecodecamp.org/news/the-java-handbook/)
- [Linux](https://www.freecodecamp.org/news/learn-linux-for-beginners-book-basic-to-advanced/)
- [React](https://www.freecodecamp.org/news/react-for-beginners-handbook/)
- [CI/CD](https://www.freecodecamp.org/news/learn-continuous-integration-delivery-and-deployment/)
- [Docker](https://www.freecodecamp.org/news/the-docker-handbook/)
- [Golang](https://www.freecodecamp.org/news/learn-golang-handbook/)
- [Python](https://www.freecodecamp.org/news/the-python-handbook/)
- [Node.js](https://www.freecodecamp.org/news/get-started-with-nodejs/)
- [Todo APIs](https://www.freecodecamp.org/news/build-crud-operations-with-dotnet-core-handbook/)
- [JavaScript Classes](https://www.freecodecamp.org/news/how-to-use-classes-in-javascript-handbook/)
- [Front-End Libraries](https://www.freecodecamp.org/news/front-end-javascript-development-react-angular-vue-compared/)
- [Express and Node.js](https://www.freecodecamp.org/news/the-express-handbook/)
- [Python Code Examples](https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/)
- [Clustering in Python](https://www.freecodecamp.org/news/clustering-in-python-a-machine-learning-handbook/)
- [Software Architecture](https://www.freecodecamp.org/news/an-introduction-to-software-architecture-patterns/)
- [Programming Fundamentals](https://www.freecodecamp.org/news/what-is-programming-tutorial-for-beginners/)
- [Coding Career Preparation](https://www.freecodecamp.org/news/learn-to-code-book/)
- [Full-Stack Developer Guide](https://www.freecodecamp.org/news/become-a-full-stack-developer-and-get-a-job/)
- [Python for JavaScript Devs](https://www.freecodecamp.org/news/learn-python-for-javascript-developers-handbook/)
## Mobile App
- [](https://apps.apple.com/us/app/freecodecamp/id6446908151?itsct=apps_box_link&itscg=30200)
- [](https://play.google.com/store/apps/details?id=org.freecodecamp)
## Our Charity
[Publication powered by Hashnode](https://hashnode.com/) [About](https://www.freecodecamp.org/news/about/) [Alumni Network](https://www.linkedin.com/school/free-code-camp/people/) [Open Source](https://github.com/freeCodeCamp/) [Shop](https://www.freecodecamp.org/news/shop/) [Support](https://www.freecodecamp.org/news/support/) [Sponsors](https://www.freecodecamp.org/news/sponsors/) [Academic Honesty](https://www.freecodecamp.org/news/academic-honesty-policy/) [Code of Conduct](https://www.freecodecamp.org/news/code-of-conduct/) [Privacy Policy](https://www.freecodecamp.org/news/privacy-policy/) [Terms of Service](https://www.freecodecamp.org/news/terms-of-service/) [Copyright Policy](https://www.freecodecamp.org/news/copyright-policy/) |
| Readable Markdown | 
The `git checkout` command switches between branches or restores working tree files. There are a number of different options for this command that won’t be covered here, but you can take a look at all of them in the [Git documentation](https://git-scm.com/docs/git-checkout).
### **Checkout a specific commit**
to checkout a specific commit, run the command :
```
git checkout specific-commit-id
```
we can get the specific commit id’s by running:
```
git log
```
### **Checkout an Existing Branch**
To checkout an existing branch, run the command:
```
git checkout BRANCH-NAME
```
Generally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes: 1) trash them, 2) [commit them](https://guide.freecodecamp.org/git/git-commit/), or 3) [stash them](https://guide.freecodecamp.org/git/git-stash/).
### **Checkout a New Branch**
To create and checkout out a new branch with a single command, you can use:
```
git checkout -b NEW-BRANCH-NAME
```
This will automatically switch you to the new branch.
### **Checkout a New Branch or Reset a Branch to a Start Point**
The following command is similar to checking out a new branch, but uses the `-B` (note the captital B) flag and an optional `START-POINT` parameter:
```
git checkout -B BRANCH-NAME START-POINT
```
If the `BRANCH-NAME` branch doesn’t exist, Git will create it and start it at `START-POINT`. If the `BRANCH-NAME` branch already exists, then Git resets the branch to `START-POINT`. This is equivalent to running `git branch` with `-f`.
### **Force a Checkout**
You can pass the `-f` or `--force` option with the `git checkout` command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from `HEAD`). Basically, it can be used to throw away local changes.
When you run the following command, Git will ignore unmerged entries:
```
git checkout -f BRANCH-NAME
# Alternative
git checkout --force BRANCH-NAME
```
### **Undo Changes in your Working Directory**
You can use the `git checkout` command to undo changes you’ve made to a file in your working directory. This will revert the file back to the version in `HEAD`:
```
git checkout -- FILE-NAME
```
***
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn) |
| Shard | 32 (laksa) |
| Root Hash | 13723046482134587832 |
| Unparsed URL | org,freecodecamp!www,/news/git-checkout-explained/ s443 |