Git tip: switch to previous branch

You can use git switch - to switch to the previous branch. In the following example, we see switching back and forth between branches main and my_dev_branch:

C:\git\my-repo [my_dev_branch]> git switch -
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
C:\git\my-repo [main ≡]> git switch -
Switched to branch 'my_dev_branch'
C:\git\my-repo [my_dev_branch]>

Edit: Old habits die hard. Updated to use switch instead of checkout since switch has a clearer responsibility. Obviously they work exactly the same for this scenario.

57 points · 27 comments · view on lemmy.world

27 Comments

vampatori@feddit.uk · 16 pts · 3y (7 replies)

Which follows the similar functionality used by the cd - command to switch to the previous directory you were in. Very handy!

invicticide@programming.dev · 9 pts · 3y (6 replies)

You can do what 👀

vampatori@feddit.uk · 8 pts · 3y (4 replies)

There's more! Well, it's more a bash thing than a cd thing.. in bash the variable $_ refers to the last argument to the previous command. So you can do the following:

> mkdir -p my/nested/dir
> cd $_
> pwd
/home/user/my/nested/dir

It's handy for a whole host of things, like piping/touching then opening a file, chown then chmod, etc.

zygo_histo_morpheus@programming.dev · 4 pts · 3y (1 reply)

On many terminal emulators you can also use Alt-. to search through your history of previous arguments, so mkdir foo followed by cd [Alt-.] will populate your command line with cd foo for example. If you have some other command in between you can just hit Alt-. repeatedly

rewire@programming.dev · 2 pts · 3y

Or ESC followed by "." Repeating it works too.

invicticide@programming.dev · 2 pts · 3y

This is amazing ♥️

static_motion@programming.dev · 2 pts · 3y

That's incredible, I never knew that. Thank you!

zygo_histo_morpheus@programming.dev · 5 pts · 3y

There's also pushd and popd so that you can pushd into one directory, move around as much as you want and then go back to before the pushd with popd

JackbyDev@programming.dev · 12 pts · 3y (10 replies)

Use switch, boomer!

canpolat@programming.dev · 5 pts · 3y (2 replies)

Old habits die hard. Thanks for pointing this out. I updated the post.

JackbyDev@programming.dev · 2 pts · 3y (1 reply)

Oh lol, I was just trying to poke fun, sorry if it came across as accusatory. 😎👍

canpolat@programming.dev · 1 pts · 3y

Not at all. It was a funny comment (I upvoted it) 👍 But you are also right. It makes more sense to refer to switch and restore whenever possible.

Hexarei@programming.dev · 3 pts · 3y (4 replies)

What's the difference? Genuine question

JackbyDev@programming.dev · 4 pts · 3y

Checkout was one of those commands that I joking would call Turing complete because of how much you can do with it (I haven't actually tried to see if it is, but am fully prepared for someone to be nerd sniped and tell me it actually is). I think they're mostly the same, but switch and restore were added as more straightforward versions of checkout and reset.

mvirts@lemmy.world · 3 pts · 3y (2 replies)

Well one starts with an s, the other with a c... :P

They changed the command to clarify what it does, checkout was / is used for switching branches as well as branch creation but has connotations of doing some locking in the repo from older vcs software.... I think. the new commands are switch and branch. check the docs

Idk what the deal is with switch, I thought it wasn't supposed to be creating branches but right in the docs there's a flag for it???

Im the kind of user that just deletes .git and starts over when I f up the repo, so take my git advice with a tablespoon of salt.

ray_gay@programming.dev · 3 pts · 3y (1 reply)

I switch to using switch since git switch auto-creates the local branch from the remote branch, if the branch doesn't exist yet, and a remote branch with the corresponding name exists.
Also git switch -c for auto-creating a new branch, even if there is no remote branch for it

jnareb@programming.dev · 2 pts · 3y

If I remember it correctly, git checkout also automatically creates the local branch from the remote branch (of the same name), and sets up tracking.

mvirts@lemmy.world · 1 pts · 3y

Came here to say this

Nibbler@lemmy.ml · 1 pts · 3y

NEVER!!

erlingur@programming.dev · 6 pts · 3y

Great tip!! Thanks!

firelizzard@programming.dev · 3 pts · 3y

But evidently git rev-parse - will not print out the previous branch 😔 that would have been useful for scripts

crystelium@programming.dev · 3 pts · 3y

This led me down a real rabbit hole of looking at what you can do with git commands, very neat.

There's a few more things you can do which I found, like switching to the Nth branch you last had checked out: https://www.w3docs.com/snippets/git/how-to-checkout-the-previous-branch.html

mertssmnoglu@programming.dev · 2 pts · 3y

That's so cool, this can be my favorite command so on. Switching between two branches is easy with that.

jmondi@programming.dev · 2 pts · 3y

I always forget to use this one, thanks for the reminder

jnareb@programming.dev · 1 pts · 3y

Note that git checkout - / git switch - examine reflog to find previous branch. Which means if you renamed the branch, at least current version of Git would be unable to run git switch -.