I’m working on a GitLab project that I cloned locally. I made some changes but didn’t commit or push them yet. Now, I want to start working on a new feature but keep the current changes separate. I might discard the new changes later or push both sets of work eventually.
From what I understand, the right step is to use the git create branch approach to isolate my work. Is running git checkout -b NEW_BRANCH_NAME the best way to do this locally? Also, once I have two local branches, how do I switch back and forth between them safely without losing or mixing up changes?
Any clarification on using git create branch and handling local switches would be really helpful!
Been working with Git for a while now, and the basics still come in handy every day. If you’re just starting out, here’s how to use git create branch and switch locally:
git checkout -b feature-branch
This command both creates and switches to a new branch named feature-branch
. It’s a quick way to start fresh from your current branch. If you already have a branch and just want to switch:
git checkout branch-name
That’s your starting point for branching locally. It works, and it’s reliable.
Yep, @Priyadapanicker nailed the basics. Just to add from my experience working on multiple features at once—sometimes you want to git create branch without dragging along the mess of uncommitted changes from your current state. Here’s what I usually do:
git stash
git checkout -b new-feature
That stashes your current changes temporarily and gives your new branch a clean slate. Then you can decide whether to reapply the changes later using:
git stash pop
I’ve found this super helpful when my work-in-progress doesn’t quite fit the new branch I’m starting. Keeps things tidy and focused.