git checkout - Switching branches and restoring files (2024)

The "checkout" command can switch the currently active branch - but it can also be used to restore files.

The most common use case for "checkout" is when you want to switch to a different branch, making it the new HEAD branch.

Another use case for "checkout" is when you want to restore a historic version of a specific file. Thereby, you can reset single files to earlier revisions - while keeping the rest of the project untouched.

Important Options

<branch-name>

The name of a local branch that you want to switch to. By specifying the name of a local branch, you will switch to this branch and make it the current "HEAD" branch.

-b <new-branch>

Creates a new local branch and directly switches to it. This can be used as a shortcut instead of the following two commands:
git branch <new-branch-name>
git checkout <new-branch-name>.

-b <new-branch> --track <remote-branch>

Creates a new local branch - and sets up an "upstream" configuration. This way, the new local branch has a tracking relationship with its remote counterpart. This allows you to more easily see when the two aren't in sync (i.e. when unpushed commits in the local branch or unpulled commits in the remote exist).

<file-path> <commit-hash>

Restores a historic revision of a given file. By providing HEAD as the revision, you can restore the last committed version of a file - effectively undoing any local changes that happened since then. If you want to restore a specific earlier revision you can provide that revision's SHA-1 hash.

Usage Examples

In its simplest (and most common) form, only the name of an existing local branch is specified:

$ git checkout other-branch

This will make the given branch the new HEAD branch. If, in one go, you also want to create a new local branch, you can use the "-b" parameter:

$ git checkout -b new-branch

By using the "--track" parameter, you can use a remote branch as the basis for a new local branch; this will also set up a "tracking relationship" between the two:

$ git checkout -b new-branch --track origin/develop

Another use case for "checkout" is when you want to restore an old revision of a file:

$ git checkout 8a7b201 index.html

If you specify "HEAD" as the revision, you will restore the last committed version of the file, effectively undoing any local changes that you current have in that file:

$ git checkout HEAD index.html
Tip

Quick Checkout in Tower

In case you are using the Tower Git client, using checkout becomes easy as pie. Simply double-click a branch in the sidebar to make it the new HEAD branch - or choose a branch from a list.

git checkout - Switching branches and restoring files (1)

git checkout - Switching branches and restoring files (2)

Learn More

git checkout - Switching branches and restoring files (2024)
Top Articles
FAQ: Helpful Resources – Hold Harmless
Add certificates to a keychain using Keychain Access on Mac
The Tribes and Castes of the Central Provinces of India, Volume 3
Couchtuner The Office
Nwi Police Blotter
Seething Storm 5E
Joe Gorga Zodiac Sign
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
What is a basic financial statement?
Richmond Va Craigslist Com
Urban Dictionary Fov
The most iconic acting lineages in cinema history
Top tips for getting around Buenos Aires
Samsung Galaxy S24 Ultra Negru dual-sim, 256 GB, 12 GB RAM - Telefon mobil la pret avantajos - Abonament - In rate | Digi Romania S.A.
D10 Wrestling Facebook
"Une héroïne" : les funérailles de Rebecca Cheptegei, athlète olympique immolée par son compagnon | TF1 INFO
Khiara Keating: Manchester City and England goalkeeper convinced WSL silverware is on the horizon
Convert 2024.33 Usd
Roll Out Gutter Extensions Lowe's
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Craigslist West Valley
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Finalize Teams Yahoo Fantasy Football
Afni Collections
Dell 22 FHD-Computermonitor – E2222H | Dell Deutschland
Ts Modesto
How To Improve Your Pilates C-Curve
Experity Installer
Inmate Search Disclaimer – Sheriff
Bozjan Platinum Coins
Everything You Need to Know About NLE Choppa
Gold Nugget at the Golden Nugget
AI-Powered Free Online Flashcards for Studying | Kahoot!
Merge Dragons Totem Grid
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Uc Santa Cruz Events
National Insider Threat Awareness Month - 2024 DCSA Conference For Insider Threat Virtual Registration Still Available
Flags Half Staff Today Wisconsin
Wilson Tattoo Shops
Firestone Batteries Prices
California Craigslist Cars For Sale By Owner
Saline Inmate Roster
Dr Mayy Deadrick Paradise Valley
The Great Brian Last
Unblocked Games - Gun Mayhem
Mega Millions Lottery - Winning Numbers & Results
Scott Surratt Salary
Craigslist Pets Lewiston Idaho
Fishing Hook Memorial Tattoo
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5679

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.