Checkout the previous git branch without needing to remember or type the name.
If you want to checkout the branch you were previously working on, run this command:
git checkout -
It’s magic! You don’t have to remember or type the branch you were just on.
Consider if I’m on a feature branch and I want to check out and update the main branch and merge changes into my feature branch. I can do this:
$ git checkout feature-branch
$ git checkout main
$ git pull origin main
$ git checkout -
# Now we're back on feature-branch, but let's check ...
$ git branch
$ git merge main
(Technically, there’s a more efficient way to do this.) But the point is that I didn’t have to type “feature-branch” again. Instead, I changed back into the
feature-branch branch automatically by running
git checkout -.
Although I'm no expert, I've outlined the things I think you need to know to get started using Git.
See all the options available to you, and also learn how to filter them.
Delete all local git branches except main, master, or your current branch.