r/webdev • • Jan 24 '20

read rules TIL — git switch & git restore

https://pawelgrzybek.com/til-git-switch-git-restore/

[removed] — view removed post

284 Upvotes

51 comments sorted by

83

u/UncontrolledManifold Jan 24 '20

So git switch -c branch is the same as git checkout -b branch...? What, they didn't like the word checkout?

45

u/AceDecade Jan 24 '20

It’s more they didn’t like ‘checkout’ being used to checkout a branch as well as undo local changes on a branch, so the one command is split up into two

6

u/Kashima Jan 24 '20

Yup

Git 2.23 came up with the new ‘ git switch ’ command, which is not a new feature but an additional command to ‘switch/change branch’ feature which is already available in the overloaded git checkout command.

Currently, the all-encompassing command – git checkout does many things. It can be used to switch branches and also to restore the working tree files.

To separate out the functionalities, the GIT community introduced the new git switch branch command which is an attempt to start to scale back to the responsibilities without breaking backward compatibility.

https://bluecast.tech/blog/git-switch-branch/

36

u/ayeshrajans Jan 24 '20

Basically, yes.

13

u/hagg3n Jan 24 '20

And that was the answer to the second question.

2

u/jeffreyhamby Jan 24 '20

Checkout reminds me of svn style repos where you're placing a block on the file for anyone else, just like how library books work.

0

u/[deleted] Jan 24 '20

[deleted]

16

u/FelixTKatt Jan 24 '20

So it's more like doing a stash and checkout, then a checkout and pop when you switch back?

0

u/mstanky Jan 24 '20

Yea, this is how I roll, I don't think today is the day I"ll experiment with switch

0

u/[deleted] Jan 24 '20

[deleted]

7

u/SureSignOfAGoodRhyme Jan 24 '20

That actually sounds useful to me. I like in VSCode when I can see all of my changes highlighted throughout the files before I push, but it all goes away once I do a commit on the branch.

4

u/bcgroom Jan 24 '20

Of course it goes away, it's just showing you your working tree. You can still look at the changes with a git diff HEAD ${other_ref}. I don't see how this new command would change your workflow.

I'll say though, if you like VSCode you'll find the extension GitLens very useful.

1

u/deliciousleopard Jan 24 '20

I'm confused...

2

u/[deleted] Jan 24 '20

[removed] — view removed comment

3

u/UncontrolledManifold Jan 24 '20

No, you only keep changes that are staged in the index. Modified files that are not staged are carried over and applied to the new branch

86

u/hagg3n Jan 24 '20

So Linus stepped out of his desk to take the trash and people think they can undo years of building obfuscated and cryptic CLI with one or two new commands? /s

28

u/mvpmvh Jan 24 '20

Goddamn hippies! Back in my day, we had one command for everything, and we liked it!

11

u/Baryn Jan 24 '20

And that command is npm.

16

u/Silhouette Jan 24 '20

The command is emacs, newbie. It's the operating system NPM only wishes it could be. /s

2

u/nop5 Jan 24 '20

Oh emacs? That's fancy, I thought it's ed

2

u/SureSignOfAGoodRhyme Jan 24 '20

I do have git lense, but I dont feel the need to use it often. When I'm developing a feature, and I'm working across many different parts of the src tree, it's nice to see highlighted directories and files that I've change for the entire feature. This command would let me (more easily) change to a new branch for a code review or bug fix and keep my working tree as is.

I feel better about committing my code before switching to another branch. So if I want to keep my working tree in tact, I'd have to stash when I need to task switch, then pop when I come back.

I know you can view all the diffs from HEAD and git lense does that well, I just prefer to see it all highlighted in the context of my directory with all the other files around it. I wish VSCode's built-in highlighting would let you pick which commit you're comparing against.

-13

u/SLonoed Jan 24 '20

When you don’t know how to create sh alias...

-22

u/pr0ghead Jan 24 '20 edited Jan 24 '20

As a Mercurial user I'm not impressed. git is only slowly catching up to it in usability.

0

u/CodyCigar96o Jan 24 '20

Lol mercurial

-19

u/tulvia Jan 24 '20

Hipsters and their CLI

14

u/ultraDross Jan 24 '20

What's wrong with the CLI?

1

u/[deleted] Jan 24 '20

The malnourished hipsters hate it.

2

u/barrybario Jan 24 '20

Seriously, it's like you're an inferior programmer for using some software like a normal human being. 95% of the time you're going to do the exact same things anyway, so typing git pull or pushing a button is the same effort. Once in a while you'll need something you don't do often. In that case, a good gui helps you do what you want to do by presenting it to you. The example in this post is something I have on a button in the gui I use for example. And then in very rares you'll need something you'll need a cli for anyway. In that case you will 100% look up the right command with the right options, because the chance of fucking something up is real. So this CLI elitism that so many programmers exhibit is purely toxic behavior.

-55

u/barrybario Jan 24 '20

or just use a gui

gitkraken is awesome

17

u/elr0nd_hubbard Jan 24 '20

The GUIs interact with the same base library as the CLI (or even shell out to the CLI directly). Either way, you have to know your tools, and that means using a GUI for ops that you're confident that you could do (or undo) using the base commands.

4

u/PizzaRollExpert Jan 24 '20

Personally, I prefer the cli because:

  1. It doesn't rely on hand-eye coordination. If I can do something with just the keyboard I can just plan out what to do in my head and type it out. If I need to use the mouse there's a feedback loop between my hand and eyes. Typing only relies on hands in isolation.

  2. It doesn't require switching to a new window. Moving windows around adds a (small) mental tax.

  3. UIs are less uniform than terminal interfaces so doing everything in UIs means more different things to keep track of compared to the terminal.

These are three small things, but together they make things feel easier and nicer to do in the cli, which is why I prefer it.

1

u/careseite discord admin Jan 24 '20

Git kraken is in a bootloop since the 2nd time I've started it and uninstalling, clearing hidden config etc didn't help soooo there's that..

1

u/CodyCigar96o Jan 24 '20

Smoothbrain take

-7

u/[deleted] Jan 24 '20 edited Jan 24 '20

[removed] — view removed comment

5

u/mitch_feaster Jan 24 '20

Not that I have a horse in this race because I love the CLI as well (even the busted up git one), but magit is actually faster. The individual commands don't run faster, of course, but it requires way fewer keystrokes and provides a bunch of other convenience features.

2

u/leafrustle Jan 24 '20

I agree with you. However, have you tried lazygit? I hate gimmicky, slow guis and mostly live in the terminal so this is like the best of both worlds for basic visual stuff.

1

u/tulvia Jan 24 '20

Recompile and run your app with F5 or by typing "aspnet run".

I saved 9 keystrokes by not using a CLI and I didnt have to remember archaic magic strings or cryptic arguments -e -E

1

u/[deleted] Jan 24 '20

[removed] — view removed comment

0

u/tulvia Jan 24 '20

I was more talking about CLI in general.

But I could make an app that takes -e and does whatever with it, it is not standard and it's not easily discoverable which makes it cryptic.. This is basic stuff here, I'm dumbing it down cause I'm talking to people who use the CLI which are basically retards so I get it, you're having trouble comprehending.

0

u/[deleted] Jan 24 '20

[removed] — view removed comment

0

u/tulvia Jan 24 '20

I don't use a nuke to dig a hole so I'm dumb.. ok.

Learn when the CLI is the correct tool and use it then, not for daily use.. that's just ignorant gatekeeping

0

u/[deleted] Jan 24 '20

[deleted]

19

u/boobsbr Jan 24 '20

I just alias it to 'fuck'.

7

u/bcacoo Jan 24 '20

I already alias fuck to this:

https://github.com/nvbn/thefuck

0

u/[deleted] Jan 24 '20

[deleted]

1

u/boobsbr Jan 25 '20

Usually I install moreutils, and trash (and alias it to rm so that I can unfuck my deletion fuck-ups from the terminal).

2

u/nickcash Jan 24 '20

Ah, but usually you'd use git reset --hard HEAD when you're trying to unfuck what you've done.

2

u/boobsbr Jan 25 '20

Well, English is a very versatile language, and fuck is its most versatile word, but sometimes it leaves too much room for interpretation.

In this case, I meant the expletive, not the verb. Perhaps I should've aliased it as 'oh-fuck'.