50 ms·
New in Git: switch and restore
- Thev00d00 5y agoAs a long time git user I absolutely love `git restore`. I hope that the ongoing improvements in porcelain will continue.
- Vinnl 5y agoIt's not really new anymore, but still way underused, so it could certainly do with more attention. Git's UI has become better, but they can't really remove the old UI and tutorials using those, so people keep sticking to that.
- easygenes 5y agoWhat would top recommendations be for tutorials using the newest UI?
- Vinnl 5y agoWe're commenting on one :) But really there's not much to it - `git checkout` is the main wart of Git's UI given how overloaded it is, and with `git switch` and `git restore`, you'll have banned out pretty much all of its regular use. And given how much more straightforward (i.e. they have more guessable arguments) those are, there's not as much need for tutorials anymore.
- pcl 5y agoIt’d be interesting to add a config that disables the cruft for interactive terminals. I wonder if that could be a pathway towards deprecation.
- agumonkey 5y agoand it's a very general problem distributing improvements after a large mass of old habits are spread around is something to be fixed
- Omin 5y agoI can't reasonably start using such functionality until the PC with the oldest software that I still use has updated or I will have to deal with 2 ways of doing things all the time. Currently, that's an ubuntu 18.04 machine at work and that doesn't have `git restore`, yet.
- coder543 5y agoYou could install newer versions of git using a ppa: https://launchpad.net/~git-core/+archive/ubuntu/ppa https://launchpad.net/~git-core/+archive/ubuntu/ppa
- makeitdouble 5y agoThese new commands make a lot more sense, but the weird thing is they don’t bring anything else to the table. They behave exactly like the existing ones, so much that anyone that really cared could have just aliased them. So is there any incentive to switch for the people who went through the trauma of burning the old ones in their soul ? (I often heard that knowing how it works internally makes git commands feel natural. I was lied to)
- Vinnl 5y agoThey don't behave exactly like the old ones, because they do less - which means it's harder to accidentally do the wrong thing, and it's easier to guess what its arguments do. If you know the old ones already and don't make mistakes, it's fine not to use these, really. I consciously got the new commands into my muscle memory in case I'm pair programming or someone less familiar with Git is looking along. And yeah, knowing how Git works makes it a lot easier to understand, but it doesn't make the commands more natural. (Except perhaps knowing when you can use a commit id instead of a branch name.)
- onionisafruit 5y agoI had git-checkout syntax burned into my soul. I switched to switch/restore a year or so ago and am happy to be mostly unburdened of git-checkout. I say mostly because I still use it in scripts so they work with old git versions.
- bicolao 5y agoYou don't need -- as much in the "git restore" example. With git checkout it may be necessary to separate the branch and the paths with "--" but since "git restore" does not take a branch (except with -s), doing this is totally fine git restore test.txt
- 411111111111111 5y agoIt's usually not necessary for checkout either
- yakubin 5y agoNot typing it with "checkout" gets in the way of good tab completion. At work we have at least tens of thousands of branches and if I hit tab after "git checkout file_path_prefix", my shell is going to freeze for a while, while tab completion is going over remote branches.
- Freak_NL 5y agoHow do you end up with that many branches? It sounds like you keep every feature branch around forever. Keeping one around for a few months I get (although personally the sooner they're gone after rebasing or cherry-picking them the better), but this sounds like a full on history of every branch ever.
- yakubin 5y agoIf someone pushed a branch to remote, then there is a chance that they made a CI build for a customer based on that branch. Later you may need that branch to look at the source code, when you get a coredump, or logs or something. (Every non-official build is made from a separate branch.)
- Freak_NL 5y agoDeployed versions that need to be referred to later usually get git tagged, but those are not that much different from branches of course (they'll both show up in autocomplete as a committish object). But even then; tens of thousands of tags/branches? At that point it might be worth considering simply baking in the git commit hash into the build artefact for future reference instead of tagging every CI build with a branch/tag.
- manojlds 5y agoBeen there for about 2 years I believe.
- golemiprague 5y agoI wish git had some tree transversal related commands instead of all the crypto commands that nobody understand what they are doing without long tutorials. Got a feeling that this is a case where going down to the metal is better than all the abstractions.
- talkingtab 5y agogit restore, where have you been all my life?
- lucb1e 5y agoNonexistent for the most part unless you were born recently :)
- Karsteski 5y agoGood article. I've been using git switch since I started using git CLI a few months ago, but restore is new to me!
- rubyn00bie 5y agoI’m surprised no reference to: git switch - For going back to the previous branch you checked out.
- hsn915 5y agoThis is not new. It works the same with the checkout command. git checkout -
- helltone 5y agoWhat is a good resource to learn modern git properly? I have been using git for 10 years and get around with a really small set of old commands.
- nerdponx 5y agoPro Git is excellent: https://git-scm.com/book/en/v2 https://git-scm.com/book/en/v2
- deleted 5y ago[deleted]
- SiebenHeaven 5y agoAh yes, modern git for people using modern c++ If your plain old git works fine for you, there is no need to go looking for modern git IMHO.
- raju 5y agoAgreed with the other recommendation of Pro Git. If you want to learn Git from the inside out, I wrote a two-parter that aims to explain Git from the inside out, focusing on the data-structure Git uses: https://looselytyped.com/blog/2014/08/31/gits-guts-part-i/ https://looselytyped.com/blog/2014/08/31/gits-guts-part-i/ https://looselytyped.com/blog/2014/08/31/gits-guts-part-i/ https://looselytyped.com/blog/2014/08/31/gits-guts-part-i/ Finally, _if_ you have an O'Reilly subscription, I am currently writing Head First Git (first four chapters are in early release). If you are not familiar with the Head First series, its a rather unique format that involves using lot of pictures to explain ideas and traditionally the books move a lot slower than most technical books. Ideas/concepts are cemented using puzzles, quizzes, crosswords. You can see a list of the existing ones here https://www.amazon.com/Head-First-Series-Books/b?ie=UTF8&node=8456760011 https://www.amazon.com/Head-First-Series-Books/b?ie=UTF8&nod... Feel free to email me if you need any more resources—I have spent a lot of time teaching Git.
- quantumsequoia 5y agoNothing has been more intuitive than this interactive online tutorial https://learngitbranching.js.org/ https://learngitbranching.js.org/ You lean in 30 minutes more than you learn in hours of reading documentation and experimenting
- some_developer 5y agoI used to know lot of terminal commands but I'm seriously falling behind due to jetbrains integration which covers 99% of my daily use case. Together with local history, I've never "lost" work in years. Never heard of switch /restore and will probably forget about it the next time I'm on the terminal. Rebase and interactive Rebase is so well integrated for my use cases and I feel much more productive without having to switch context. Reflog and bisect are when I nowadays switch to the terminal, have not discovered an equivalent last time I checked.
- 600frogs 5y agoI currently use a third-party git GUI (GitKraken atm) but I also use jetbrains products, am I missing out by not using the integrated git functionality? If so, is there a tutorial or guide you could recommend, or is it all fairly self-explanatory?
- thecupisblue 5y agoJetbrains Git integration is quite self-explanatory - if you're already using GitKraken, you're not missing out much except being able to do it from your IDE instead of another tool. Maybe conflicts are a tad easier due to same syntax highlighting style and ability to edit on the go.
- AnthonBerg 5y agoThe Jetbrains git interface is really quite good. For me the magic is the general combination of git and local history. The search is good and the diffing and jump-to-source from those. The changelist handling is pretty good too. I think a quick skim of the available features in the docs would probably give a good overview. Then poking around.
- thecupisblue 5y ago> but I'm seriously falling behind due to jetbrains integration This IntelliJ integration is the source of quite a lot of git problems in teams I worked with. I'm quite flabbergasted by this - devs claim to know git on their CV, come in and know what "commit" is and how to use the IntelliJ UI, but don't even understand what its doing. And everyone is acting like it's OK and learning git is a "hard thing ill never need" and we should all use sourcetree or jetbrains. Or people just get so used to it and never understand what exists below it. They lose all sense of what they're doing and just think "the machine knows what I want". Then a vaguely questioned dialog appears - or something similar - and they cause clusterfuck upon their branch - or sometimes even other people's remote branches. How do we allow our culture to be so lazy that people resist using one of the basic tools because "oh its hard I gotta remember 5 commands" and we find it OK? No wonder the plane is burning. It's good that you still know that reflog exists, because a lot of "inteliij is my git client" users don't even know about it. Tho I'm still wondering, isn't it faster/easier to open intelliJ terminal and type a command or two than having to ope a whole new window and click around it? (also sorry if this sounds like an attack on you, it isn't! just really wondering!) Also, re: OP: So basically 2 new commands were added that do what other commands already do, but people dont read the docs so we should add new commands so maybe people will read the docs for them?
- lucb1e 5y agoTL;DR because 'checkout' was found to be confusing, as of git 2.23 the switch command can switch to branches or commits (git switch master, git switch 0c38cf) and the restore command restores files (git restore pufferfish.txt).
- cybersvenn 5y agoInstead of one confusing command we now have three confusing commands.
- lucb1e 5y agoI don't find switch or restore confusing? And also I never found checkout confusing, but that might only have been because I never realized it had multiple functions. I've been using it for both things (switching and restoring) for years, but I remember starting to read the article and thinking: how did I never notice this is the same command for two completely different things?! Maybe I used to know and now it's just not something I think about anymore? Either way, I can see how checkout can be seen as odd, but why switch or restore?
- makeitdouble 5y agoArguably there is some semantic overlap between restore and reset.
- hsn915 5y agoI knew about `git switch` but this is the first time I hear about `git restore`. It also never occurred to me that the checkout command was overloaded in that particular way.
- thecupisblue 5y agoDoesn't seem overloaded, seems quite fitting. Checkout a hash or a file from a hash. Switch kinda seems "underloaded" in this way.
- dilap 5y agoIt's definitely unintuitive for newcomers. And I think arguably modifying working-directory files vs. modifying .git/HEAD are distinct things (though at this point I also find using checkout for everything to be pretty intuitive).
- bobsterman 5y agoswitch being underloaded to checkout is the same as checkout being overloaded to switch. In the checkout model, branches are just named aliases for their current hash. This seems trivial to me. Do we really need to use another vocabulary spot up in our heads for a command that is strictly a more-restricted checkout?
- hsn915 5y agoYea it never bothered me and I never understood why people kept complaining about it online. It seemed very superficial complaint. The way you use git is by first understanding its model. If you understand the git model, everything makes sense. If you come to it expecting it to be a tool that magically does what you want, I guess you will be very disappointed.
- recursive 5y agoI don't think magic is part of it. People like me just want it to be consistent.
- 5y ago
- clon 5y agoAnother difference is that with git checkout you can create and switch to the new branch in one command using the -b flag: git checkout -b new_branch You can do the same with the new one, but the flag is -c: git switch -c new_branch A good example why developers should not try to be designers. Even of talking about "API/CLI design.
- kzrdude 5y agoDevelopers/designers are always torn between the competing priorities of preserving consistency and making a new interface the best it can be.
- yakubin 5y agoApple and GNOME are even better examples of why designers should not try to be designers. Now what options do we have left?
- CRConrad 5y ago> Now what options do we have left? KDE, git, and FreePascal / Lazarus.
- rnestler 5y ago-b in checkout is short for "branch" while -c in switch is short for "create". IMO the UI of git switch is much more intuitive, since the argument is always a branch and the default behavior is to switch to an existing branch. For slightly different behavior (like creating the branch first) there are flags. So I think it's good that the flag for switch is a different one than for checkout, since the interface of git checkout was quite unintuitive IMO.
- ahmedfromtunis 5y agoThe new command makes more sense to me, i.e. `switch` and `-c(reate)` new_branch. With `checkout`, however, what does `-b` even mean? Branch? That said, it'll sure take time before the majority of developers (including myself) get onboard with it. Would a deprecation flag be a good idea for git?
- 5y ago
- sparkling 5y agoFor more convenient git shortcuts, check out https://github.com/frostming/legit https://github.com/frostming/legit which plugs some extra commands into your git
- mcs_ 5y agoFunny! I've been using the alias `gk='git checktout'` for years now. Git has been my first terminal-oriented versioning control system. I've used TFS and other but never focused on the action name, just the icons. Checkout branch/hash/file seems more potent than two different commands.
- cerved 5y agoI feel obliged to note that the version control of TFS (Team Foundation Server) is TFVC (Team Foundation Version Control)
- kzrdude 5y agoIt is unfortunate that: + git switch is documented as "EXPERIMENTAL" + git --help lists git switch but not git checkout as an important command This is a documentation inconsistency. It can't be both the canonical interface to use and experimental at the same time.
- the_biot 5y agoSo send in a patch.
- vanviegen 5y agoYour preferred solution to this inconsistency is for a random bystander to flip a coin?
- asimpletune 5y agoI don’t know why this is getting downvoted. It’s the correct solution and actually a great opportunity to make an easy contribution to open source.
- wpietri 5y agoIt's not the correct solution because it requires knowledge of the intent of the project leaders, which is not easily available to us. A much better response would be, "Thanks for pointing that out! I have filed an issue." One can't simultaneously through shade for not being community-spirited while expecting other people to do all the work.
- asimpletune 5y agoSubmitting a patch would start with filing an issue right? And I assume in that venue the maintainers would clarify their intent. If help were indeed needed then I’m sure the maintainers would be relieved to get some help fixing the documentation but. In any case, the suggestion of submitting a patch when you notice a problem doesn’t deserve downvotes. That’s not throwing shade, at all.
- aryamaan 5y agoI picked up git restore with git's suggestion itself. Whenever I do git status, it tells me which files are changed and if I want to go back to their previous states, I can use got restore.
- rnestler 5y agoFor some time I was pretty annoyed that git was showing the new suggestions, but for some reasons my git autocomplete did not know about them and thus couldn't tab complete. (It was on ArchLinux with zsh using the grml zsh config). After a few month the autocomplete got updated as well and I could actually use the new interface without to much frustration.
- recursive 5y agoI've never seen the auto complete work with anything but file paths. Nor did I know it ever could.
- tkfu 5y agoThis reminds me of the old xkcd [1] about how standards proliferate... Situation: Git has 137 difficult and unintuitive subcommands [2], and new users can't keep straight which ones they should use. "Oh man, that's awful, let's add new subcommands that are clear, and do just one thing well!" Soon: Situation: Git's CLI has 138 difficult and unintuitive subcommands. [1] <https://xkcd.com/927/ https://xkcd.com/927/> [2] Yup, seriously, as of 2.32. I checked.
- 5e92cb50239222b 5y agoMost of those are plumbing. They're only needed if you're building tools on top of git (integration with IDEs or custom GUI, for example), or doing very advanced scripting/broken repository repair.
- ninkendo 5y agoExcept this is actually the opposite… they turned one command that did too many things into two commands which each do fewer things.
- layoutIfNeeded 5y agoNo thanks, I will keep using checkout.
- leephillips 5y agoMe too. The checkout command makes sense and seems consistent to me: check out branches or files. Why do I need two other different commands for this?
- layoutIfNeeded 5y agoExactly. The way I see it is that “checkout” has two arguments: - the commit you want to checkout, which defaults to HEAD - the files you want to checkout, which defaults to everything Simple as.
- Denvercoder9 5y agoThe problem is that this breaks down when you specify both arguments. Doing `git checkout branch file` checks out file from branch to the working tree, but doesn't change HEAD.
- bicolao 5y agoThe two are added with some extra protections. Something that cannot be done with git-checkout without breaking scripts. I believe git-checkout could silently overwrite data in one case (can't remembe the details). And git-swith will stop you from moving the branch when you're in a middle of a rebase or other multi-command operations. It also tries to avoid entering detached HEAD mode by default. It's definitely geared towards newcomers. But even I'm glad it catches me from doing stupid things from time to time.
- philote 5y agoI used to use Git Legit: https://frostming.github.io/legit/ https://frostming.github.io/legit/ But the official addition of git switch conflicted with their git switch (which is better as it auto-stashes changes before switching). Legit also had the issue of not properly handling branches with "/" in them.
- hkopp 5y agoMy git productivity hack is `git diff --color-words`. Instead of showing the line-by-line diff, it shows only the words that changed. Especially useful if you have long sentences where only a comma changed or some other typo. With git diff, the two lines are shown, with --color-words, only the changed symbol is highlighted. The option --color-words also works with git show. I even made aliases for them: git cshow and git cdiff. Other than that, I recommend that people learn to use git properly. In my work, I often have problems with people overwriting their commits and trying to handle merge requests of commits where one commit message is "did some updates" and the other commit is "some fixes". Getting to know git for an hour, may have prevented both issues. But I am biased, since I use git since my bachelor thesis.
- mtekman 5y agoIn a similar manner for Emacs users: magit-toggle-refine-hunk is a lifesaver for word diffs
- andreareina 5y agoKey combo `D t`
- leephillips 5y agoThanks for teaching me about --color-words. I didn’t know about it, and I greatly prefer it over the normal git diff output.
- lisper 5y ago> Other than that, I recommend that people learn to use git properly. Sorry to be harsh here, but that is completely useless advice. It's a tautology. Of course people should learn to use git "properly". What's the alternative, that they should learn to use it improperly? Everyone should learn to use everything properly. It's like telling someone dealing with a crisis that they should "take appropriate action", as if taking inappropriate action was something that someone would actually seriously consider absent this advice. The problem is that no one knows what "properly" means when it comes to git. Git itself provides no clue, and everyone and their second cousin has an opinion. That makes the advice to use git "properly" utterly vacuous. Figuring out what "properly" means is the whole problem with git. [UPDATE] See also my earlier comment here: https://news.ycombinator.com/item?id=27580478 https://news.ycombinator.com/item?id=27580478
- pjmlp 5y agoIt is ironic that Linus hates C++ so much, and then proceeds to create what is for all practical purposes, the C++ of source control systems.
- tonetheman 5y agoThis should be the top comment!
- vnorilo 5y agoComplexity is like body odor: you generally don't mind your own.
- jeltz 5y agoGit has not been Linus's project for many years.
- williamstein 5y agoThe Git commit history of Git is nicely browsable here: https://github.com/git/git/graphs/contributors?from=2005-04-17&to=2021-07-31&type=c https://github.com/git/git/graphs/contributors?from=2005-04-... Indeed, Linus has only one commit in the last 3 years, and is the #28 committer of all time.
- sdesol 5y agoHere is his contribution in the last 5 years https://public-001.gitsense.com/insights/github/repos?p=commits&q=authors%3Atorvalds%40evo.osdl.org%2Ctorvalds%40g5.osdl.org%2Ctorvalds%40linux-foundation.org%2Ctorvalds%40osdl.org%2Ctorvalds%40ppc970.osdl.org%2Ctorvalds%40woody.linux-foundation.org%2Bwindow%3A1825&r=github%3Agit%2Fgit&v=repo https://public-001.gitsense.com/insights/github/repos?p=comm... The changes were mostly pretty small as well, which would make sense given he has been hands off for quite some time now.
- wpietri 5y agoAnalogy still applies, though. If a room is smelly to you, you aren't going to hang around. So the people who spend time there are going to be ones comfortable with the funk.
- marcinzm 5y agoCan someone tell me why they decided to have the new branch argument be `-c` in switch when it's `-b` in checkout?
- Someone 5y agoI would guess it is shorthand for —-create. A quick google confirms that (https://git-scm.com/docs/git-switch https://git-scm.com/docs/git-switch)
- foxpurple 5y agoBecause c for create makes perfect sense while b does not make sense on a command which only works on branches.
- chrisan 5y agomaybe they regret the original `-b` and thought it should be -c for create as well?
- ChrisMarshallNY 5y agoThese make a lot of sense. I have gotten used to the checkout behavior, so it isn't something I'm really pining for, but these are good.
- ztcfegzgf 5y agoboth "switch" and "restore" say in the docs: "THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE." so maybe wait until that note is removed.
- aetherspawn 5y agoA lot of comments along the lines of ‘why do people use <ide> instead of learning the commands’. For me, I used to use the terminal git, and I still do occasionally. But I use Sourcetree now for most things because I make less mistakes seeing the tree visually all the time. My job isn’t to use git, it’s to write specialist software. If I get the software written and the customer is happy, it doesn’t matter whether I use <ide> or not. Imagine having 100 complex things bouncing around your head and having to make that 101 when you forget the order of arguments to merge. The guy who knows every command of git backwards is welcome to apply for a job managing a git repo or something if such a thing exists? But I could harp on the same way about his missing MATLAB or firmware skills.
- benglish11 5y agoFor me I’ve used visual git tools in the past and it ends up doing something unintended/unexpected. So now I only use the terminal commands. Perhaps the old git tools I’ve used have improved significantly though.
- aetherspawn 5y agoI prefer to rebase on the terminal also, but I reference Sourcetree when doing it.
- emsy 5y agoI completely agree. It always baffles me how something so simple (yes, simple!) as version control can end up as the abomination that is git. After controlling for popularity, you don’t see nearly as much posts explaining subversion in detail, and subversion being centralized is neither the only nor the biggest reason for that.
- gallier2 5y agoNo, version control is not simple, never was. git was the first vcs that didn't suck because it was the first tool that understood the nature of the problem. All others were ass backward and instead of solving the problem stood in the way and actively made things worse.
- alexott 5y agoI'm using Git for a long time, but I'm still get back to the Magit for most of the tasks, except very primitive... Good UI helps a lot, especially for things like selective discard or commit
- voidfunc 5y agoThe only git commands you need to know are: clone, checkout, pull, push, fetch, merge, and rm -rf $workdir.
- tkuraku 5y agoI think sublime merge is pretty close to the optimal git GUI, https://www.sublimemerge.com/ https://www.sublimemerge.com/. It uses standard git commands and terminology and allows you to add custom git commands to the GUI. Unlike Sourcetree is cross platform for Windows/Linux/Mac and includes a merge tool. I think command line git and Sublime Merge git translate back and pretty easily.
- elpakal 5y ago+1 on cli git and Sublime Merge. I use cli for everything except viewing diffs and I find Sublime to be great at that.
- rattray 5y agoFor those wondering this is a separate application from Sublime Text, built by the same folks, and with the same free evaluation and $99 lifetime license (not sure how "required" it is). I haven't felt a need for a git gui but I might give this a try anyhow.
- fermentation 5y agoHow do folks like this for reviews? I’m switching to git for a new job and the process is a bit overwhelming compared to mercurial
- solarkraft 5y agoI tried to use it and ended up back with Fork for some reason. I think basic tasks like merging were unnecessarily cumbersome and reliability wasn’t perfect.
- tkuraku 5y agoIve never had a problem, but use whatever works best for you!
- biryani_chicken 5y agoI don't like Sublime Merge that much. Probably because I tried GitKraken first and loved it. It helped me understand stash and rebase better than the command line ever did. At some point they decided to not allow gratis use on private personal repos so I switched to VSCode with the Git Graph extension which gives me most of what I liked from GitKraken.
- zoomablemind 5y agoIt's easy to become a periodic newcomer with service tools like Git, especially after an extensive dive into some complex new contexts. Simply speaking, I may forget the exact wording of Git (or other VCS) commands or switches. However, what really sticks in my memory are the concepts Git implemented. This helps to refresh the operational knowledge rather quickly, well, notes help too. In my view, 'check-out' as a concept is very much central to VCS as such. So, having a dedicated 'checkout' command which works on both commits/branches and files is quite reasonable - it keeps you in the same conceptual context. I don't mind the specialized commands, such as 'switch' and 'restore'. But sure enough, I'll forget these wordings or maybe even mix them up with 'reset' or 'revert', ??'undo', yet the 'checkout' command would likely present itself on my command line as it's directly tied to the concept.
- danjac 5y agoIf you are using the git plugin with oh-my-zsh you can type "gsw" and "grs" for "git switch" and "git restore" respectively.
- aphroz 5y agogit reset HEAD --hard or if it doesn't want to do it, I checkout another branch delete the old one and checkout back then use the local history from Jetbrain's IDE is my way to go. Probably not best practice but fast and efficient.
- uses 5y agoI use restore quite a lot but it kind of terrifies me that it can erase any amount of uncommitted work if I type something wrong. "git restore ." is basically "delete everything that I don't have a backup of".
- rochacon 5y agoAdd `-p` (`--patch`) and then pick from the diff prompt what parts you want to restore. You can also use this flag with commit.
- gigatexal 5y agoI don’t know if it’s the article or me being a curmudgeon but I’m not convinced to switch. I grokk the checkout context aware semantics. I’m not switching.
- Hackbraten 5y ago> These methods are still marked experimental, but for all intents and purposes they are here to stay I’d love to see a source on that. Can experimental features not go away without notice at any moment?
- whiddershins 5y agorestore is a bad name for this action IMO. It is extra pandering to beginners, by targeting what the maintainers maybe believe is the most common use case. Thats’s why checkout is such a great name. Maybe ‘apply’ would be good. My git-fu is often lacking, but I don’t blame git for being hard, I blame myself for not taking the time to be amazing at a tool basically everyone uses all day every day. It’s silly not to know git. It might be the most used development tool in the world.
- nine_k 5y agoOnce upon a time, someone decided to overload `checkout` with a bunch of semi-related actions, apparently for convenience. The day these patches were accepted was a sad day. It's great to see that someone took time to restore sanity. I'll switch to these commands now.
- stepanhruda 5y ago‘switch’ to these commands, hehe
- akx 5y agoThese features were introduced in 2019 (and have been featured in git's help texts since). Serious question - Why is it that they're called new two years later? Is it that no one cares to read git's prompts or release notes?
- jonwinstanley 5y agoThe author did say that had only recently discovered these commands. Unsure why they said they were new, maybe they were kidding?
- CRConrad 5y ago"New" as in new to them, I'd guess.
- erik_seaberg 5y agoIt might take five years for a change like this to show up on every box you log into, depending on how much your org relies on LTS releases to avoid randomly breaking stuff.
- Lordarminius 5y agoAs a helpful aside, in my experience, there are only about a dozen or so Git commands you need to do ninety percent of your work. You don't need to become a git zen master right away. 1. git init: to start a new repository 2. git status: checks your current state 3. git add -A: To begin tracking files 4. git commit -am: Commit all changes in the working directory with a message added on 5. git switch -c [branch name]: Create a branch and switch to it. (git checkout -b will do the same thing) 6. git switch [branch name]: switch between named branches 7. git merge[branch]: merge named branch into current branch 8. git branch [branch name] -D : delete branch if not tracked 9. git log --pretty=oneline: show a graph of commit history. 11. git push 12. git clone [repo]: Copy of a project/file onto your local computer. The comments also contain some additional advice. Here is a good introductory video: https://www.youtube.com/watch?v=2sjqTHE0zok https://www.youtube.com/watch?v=2sjqTHE0zok
- TobTobXX 5y ago> 8. git branch [branchname] -D : delete branch if not tracked Dangerous advice there. Use the lower-case '-d' option. It'll tell you that you maybe don't want to delete the branch when it has unmerged commits. If you really want to delete the branch then, the output of `git branch -d` tells you this option.
- y04nn 5y agoI would add `git rebase -i`, beacause I usually develop on a local branch and rebase it to the updated one. With git things can get messy when you want do something outside of the basic stuff. What I hate the most is resolving 3 way merges.
- dfabulich 5y agoOdd that you put switch on this list but not restore. Undoing changes is pretty important.
- samatman 5y agoI'm mildly amused that your set of commands can't actually commit anything other than a brand-new file! I will admit that I'm a lazy git user, and do most of my commits with `git commit -a`, rather than `-am` since I do try and give a short paragraph explaining the reasoning behind whatever the title message claims is the purpose of the commit. I do run `git diff` first to see what I've changed, and if the diff has unrelated changes in different files I'll usually break it up into separate commits. Decent introductory list, though. It won't surprise you that I think diff should be learned immediately; whether or not you need rebase depends on the conventions of the codebase, and if someone can learn it later they should, it can get tricky.
- bigyikes 5y agoIf you’re a Git CLI user then I can highly recommend using SCM Breeze [1] which makes things a bit prettier and more convenient. It gives you a shorthand for various commands and improves default output formatting. For example, ‘git status’ becomes ‘gs’ and it also gives you a numbered list of files. You can then substitute these numbers in place of file names in subsequent commands. No more manually typing names / paths. Other examples are: * ‘git add <file name>’ -> ‘ga <number>’ * ‘git add -A’ -> ‘gaa’ * ‘git checkout’ -> ‘gco’ [1]: https://github.com/scmbreeze/scm_breeze https://github.com/scmbreeze/scm_breeze
- juped 5y agoThey're several years old, but Duy Nguyen who invented them hasn't been around that much recently, which is why they're still marked "experimental".
- bicolao 5y agoWell, I'm him :) I actually left Git. And it looks like nobody has picked it up since. So it's going to be forever "experimental" [1] until either someone starts doing something, or deletes the whole thing [1] The experimental status is not because it's unstable but rather to allow us (or now, them) to change the UI design based on feedback if we got it wrong (again!).
- juped 5y agoThey are good features! I think they should just be made stable. Most of the help messages direct you to them anyway.
- hrishi 5y agoI have taken git apart and learned it three times now, and it all makes sense to me. The commands however, never clicked. The terminology never felt intuitive, nor predictably applied. So I can explain how git works in great detail, but ask me how to perform an action I haven't in a month, and it's a lot like figuring our a tar command.
- adrianmsmith 5y agoI like how these new commands are basically the same as the Subversion commands "svn switch" and "svn revert" ("git revert" obviously wasn't a possibility for this command as it was already taken). Obviously Git and Subversion are different beasts but, if you only needed the functionality provided by Subversion, I always found the Subversion commands better named and easier to use. I'm glad that Git is slowly re-inventing these commands that Subversion had from the beginning.
- FPGAhacker 5y agoI see command line switches as an api, and it feels wrong to me to keep aggregating new things without a clean break. I feel like these things should be versioned in some fashion. Perhaps come up with a clean slate api and enable it via calling git2 or git3 instead of this terrible mish-mash where new commands are constrained by previous bad decisions. Of course this is not a novel idea, and people attempt this by making their own veneer with a different name… but this has to come from the top for it to be effective. People won’t en masse bother with git2 or git3 if it is not built into the mainline distribution. I guess I should give it a try a submit a patch.
- brundolf 5y agoAs with all successful software that's been around for a decade or two, git is too darn complicated. To adapt the adage about democracy: git is the worst VCS, except for all the other ones.
- niutech 5y agoTry Gitless or Fossil - they are less complicated.
- alkonaut 5y ago> with git checkout you can create and switch to the new branch in one command using the -b flag: git checkout -b new_branch > You can do the same with the new one, but the flag is -c: git switch -c new_branch It's like they had a design meeting where they discussed this and said "so I propose switch -b newbranch to create and switch to a new branch" and the objection was "nah that would make it consistent with checkout, which is against the project policy"
- ReFruity 5y agoWell, maybe it indicates specifically that they had no meeting :)
- eevilspock 5y agoThe purpose of `git switch` is to switch branches, and its normal argument is a branch ref. Thus -b for "branch" would be redundant and confusing. -c is for "create", i.e. create the branch that i want to switch to. The whole point of adding git switch and git restore is to come up with more user friendly porcelain. Otherwise we might as well stick with git checkout. git checkout is maximally consistent with git checkout!
- alkonaut 5y agoHow does -b mean “create” in checkout? I get that it means “branch” but why is “branch” what I use to create a new one? The problem here is obviously the choice made for checkout, not switch. But consistency is more important so it’s better to keep consistency than “correctness” here imo. This problem already happened in the past with -d (delete).
- bendiksolheim 5y agoWhich is better depends on what you want to achieve. In this case, the whole point is to make git easier to use as a tool. Carrying over ideas we already know are confusing would therefore be wrong in my opinion.
- eevilspock 5y ago
- cratermoon 5y agoYea, all of us who have used git and learned to live with its quirks (and probably have Stockholm Syndrome from it) can chuckle a bit, but much of the git cli is a dumpster fire from the "principle of least astonishment" user perspective. The affordances in some cases are terrible. Ever drive a car that had every function ever on one stalk? The one that's the turn signal also has the cruise control, radio, lights, wipers, and blinker fluid. Much of git is like that – context-sensitive. I can't really fault the programmers, they (he) had a certain mental model and translated that concretely to commands. Except that the internal abstractions of how get works are orthogonal to tasks and use cases of users.
- ReFruity 5y agoI love git even with all of its inconsistencies and complexity. May I recommend you an interactive tutorial (not mine)? https://learngitbranching.js.org/ https://learngitbranching.js.org/
- biryani_chicken 5y agoI remember learning `git checkout` checked out files from a specific branch and the default behavior was to checkout all the files of the current branch and it made sense except for one thing. If I check out one file from another branch, HEAD is still pointing to my current branch, if I check out all but one file from another branch it's the same, but if I check out all files, then HEAD points to the other branch and that seems inconsistent. I always thought there should be one command that switched branches and then checkout changed the files.
- CRConrad 5y agoBut in your terms, a branch is the files it contains. Or rather: Changes gathered in a commit can be changes to several files. A branch is just a chain of commits, each based on the previous ones. So what a branch "is", is a bunch of (bunches of) changes to one or more files. Therefore, "checking out a branch" is checking out (a bunch of changes to) one or more files. And now you want to "check out one file from another branch"... Is it really any wonder that doesn't make much sense? I think your doing yourself a disservice by even thinking in terms of "checking out a file" as separate from checking out a branch. The units git deals in are commits and branches, not really individual files as such. If you want to use it, better get used to thinking in the same units it does.
- slaymaker1907 5y agoMy favorite part of switch is "git switch -" which switches back to the previous branch.
- geenat 5y agoStupid since git checkout does the same things. Git user since 2012 here.
- CRConrad 5y agoSorry, you were saying "git checkout is stupid since it does the same things", right? (Only since 2014 myself.)
- stevebmark 5y agoOne of the most common footgun mistakes I see in Git is people checking out a detached head state without knowing it, doing some work, then trying to push and immediately going to hell. I like that switch prevents this behavior by default. Also all git installations should come with a default shell prompt update so that it will show you immediately that you've gone into a detached head instead of a branch name. All git onboarding tutorials should include that instruction right after installing git.
- gkilmain 5y agoI like the git diff --color-words!