5

Git Worktree: How It Can Help You Enhance Your Git Workflow

 1 year ago
source link: https://hackernoon.com/git-worktree-how-it-can-help-you-enhance-your-git-workflow
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Git Worktree: How It Can Help You Enhance Your Git Workflow

Search icon
see notifications
Notifications
Happy Weekend, enjoy these top stories from this week, Kaiseki, PIRATES, and more 💚
Last Saturday at 6:00 PM
Happy Weekend, enjoy these top stories from this week, A Duel, ESCAPE, and more 💚
03/11/2023
Happy Weekend, enjoy these top stories from this week, XAXA, ARENA, and more 💚
03/04/2023
Happy Weekend, enjoy these top stories from this week, UTGARD, THUVIA, and more 💚
02/25/2023
Good news Hackers! Our stats graph has leveled up - see how much time daily people spend reading your stories!
02/23/2023
Annotate any hackernoon stories to win a free t-shirt!
02/13/2023
New Contest Alert: Win BIG with the #web-development and #ecommerce writing contests!
02/13/2023
🚨 WINNERS ALERT 🚨 #growth-marketing Writing Contest Announces Round 4 Results 🕺 💃
01/30/2023
#respectthefuckinggreen mug, OG Tee, and other Hacker Merch at 15% Discount until 1/31/2023
01/18/2023
It’s Not Over Until It’s Over! Read our 2022 recap! 📗
12/26/2022
ChatGPT is on 🔥! What do you think is next for A.I?
12/15/2022
New Writing Contest Launch! Win Up To 500 USD Per Month on #MobileDebugging Stories!
12/05/2022
HackerNoon is a Multi-language Platform: All Top Stories Now Available in 8 Languages
11/28/2022
Over 100,000 votes have been casted for this year’s Noonies Nominees. Results will be announced on December 1st.
11/28/2022
Start off your week the right way! Here are some must-read top stories from this week, The Trail to the Sea, Culture, The Philosopher's Public Library and more 💚
11/28/2022
Recently laid off from a tech company? Share your story for free on HackerNoon!
11/14/2022
New Week, New Chance to Win from $18,000! Enter #EnterTheMetaverse Writing Contest Now!
11/08/2022
Stable Diffusion AI Image Generation is Now Available in the HackerNoon Text Editor!
11/03/2022
HN Shareholder Newsletter: Green Clock Strikes Noon :-)
10/26/2022
Vote now on HackerNoon weekly polls!
10/26/2022
Highlight any text on a story and HackerNoon will generate beautiful quote images for you to share!
10/26/2022
Don't miss out on the daily top trending stories on HackerNoon! Subscribe to TechBeat to see what people are currently interested about!
09/20/2022
HackerNoon now publishes sci-fi! Read some of our science fiction stories today and submit your own!
09/05/2022
$200k+ in Committed Writer Payouts for HackerNoon Writing Contests. Enter to Win Monthly Prizes!
08/29/2022
see 17 more
Git Worktree: How It Can Help You Enhance Your Git Workflow by@nullskunk

Git Worktree: How It Can Help You Enhance Your Git Workflow

March 21st 2023 New Story
2 min
by @nullskunk

Dylan

@nullskunk

Engineering Manager with 10+ years slinging code. Hipster. Used MooTools...

Open TLDRtldt arrow
Read on Terminal Reader
Read this story in a terminal
🖨️
Print this story

Too Long; Didn't Read

Git worktree is a way to checkout many branches in a git repository. This lets us switch between different branches without losing our non-committed work. I am constantly switching between branches and code, and Git worktree has been a huge help to my workflow.
featured image - Git Worktree: How It Can Help You Enhance Your Git Workflow
Your browser does not support theaudio element.
Read by Dr. One (en-US)
Audio Presented by

@nullskunk

Dylan

Engineering Manager with 10+ years slinging code. Hipster...

About @nullskunk
LEARN MORE ABOUT @NULLSKUNK'S EXPERTISE AND PLACE ON THE INTERNET.

Git Worktree: Enhance Your Git Workflow

Have you ever been in the middle of coding a feature, and out of nowhere you get a notification that production is down? If you’re using Git, you now have a few options: commit your work, stash your work, or discard your work. All these come with their problems depending on the situation.

If you commit half-baked code, you may want to come back later and rebase to clean up your commit history. Stashing can cause problems because what if you need to stash extra code later before switching back to your original feature work?

I’ll throw away my current changes if they’re small, but otherwise, no thanks.

I recently stumbled upon Git worktree, and it has been an amazing boost in my productivity.

What Is Git Worktree?

Git worktree allows us to check out many branches in a git repository. This lets us switch between different branches in a Git repository without losing our non-committed work. Git does this by creating new, separate directories called a “linked worktree” that gets associated with a single “main worktree” (the directory created using git clone or git init).

To checkout a new worktree, we use the worktree add command: git worktree add <path> <branch_name>. Worktrees get created as new directories. We give the add command a path to where we want the new directory created and which git branch we want to checkout.

Like git checkout -b, we can create brand new branches by adding the -b argument (git worktree add -b <branch_name> <path>).

We can see all the worktrees we’ve created using the git worktree list command.

With our different linked worktrees created, we can change code and switch between isolated instances of our codebase. The changes done in one worktree do not affect the other worktrees.

What about if we want to commit and push our changes out to Github? No problem! Git worktree is a way to check out many branches. So, once we’re done making changes in our worktree, we can git add, git commit, and git push origin like if we are working on a different branch.

The changes would show up on Github under the <branch_name> we gave in git worktree add <path> <branch_name>.

When we are done with a worktree, we can call git worktree remove <path_to_worktree> to delete it.

I keep all worktrees in a separate directory ~/.worktree, outside of my general projects directory ~/projects, to reduce clutter and keep “main worktrees” explicit from “linked worktrees”.

I am constantly switching between branches and code, and Git worktree has been a huge help to my workflow!

by Dylan @nullskunk.Engineering Manager with 10+ years slinging code. Hipster. Used MooTools instead of jQuery.
Read my stories

Comments

loading...
Customi|
Hackernoon hq - po box 2206, edwards, colorado 81632, usa

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK