return to table of content

Git cheat sheet [pdf]

tiffanyh
26 replies
18h28m

Is there a historical reason why git is needlessly complex?

Seeing this cheat sheet really brings it to life.

fragmede
9 replies
18h23m

It's not needlessly complex. The workflow it was created to support is inordinately complex, and the target market for that is highly technical, so its complexity was not the issue. Kernel developers needed to communicate to other kernel developers in a decentralized manner, and needed to work on top of each other. How do you work on a project where there are 15,000 people writing code? Git was created for this purpose.

It's as complex as it needs to be to support the workflows it was designed to be. A dumber tool would be easy to create, but then it would not be able to support more complex workflows like linux kernel development. So it's not needlessly complex, though it is more complex than it needs to be for simple use cases.

For those needing a less complicated frontend, there's a large number of them to make it easier to interact with git. For people that aren't full-time software developers, I don't recommend using git directly. However for those that are, it's worth taking the time to understand the underlying concepts so git becomes something you understand because once you do, it's quite elegant for what it does. I'm not going to gatekeep being a software developer on whether or not you know git, but if figuring out for yourself how such a fundamental system works doesn't give you a release of dopamine, this might not be your kind of thing. Which is totally fine. There are tons of things that are and aren't my thing.

I recommend https://learngitbranching.js.org for learning how to navigate around git.

but of course, https://xkcd.com/1597/ applies as well.

Zambyte
6 replies
18h2m

After learning Jujutsu, I realized this is really wrong. The underlying data structures and operations are great, and they serve their purposes really well (such as supporting 10k+ people collaborating like you mentioned). They are what makes git great. The git command line interface is a needlessly complex abstraction on top of them that tries to encourage certain behaviors, but does a really bad job at it.

vrinsd
2 replies
17h39m

100% agree.

The fact there are 5 different ways to do the same thing is BAD, not good. Have one well defined, documented clear way to solve a problem.

fragmede
1 replies
13h38m

There are 3 different ways I could get to the store, I could walk or drive or bicycle. There are also 3 different paths I can take. I could go down road A, sidestreet B, or alleyway C. I know you'd rather one big button to press that just says "do it" on it, but in order to perform complex actions on complex objects, there are going to be multiple ways to accomplish the same thing.

ladzoppelin
0 replies
11h2m

But what if one of the ways to the store is dangerous once in a while but usually safe and always just as fast as the other 2 ways to the store.. Laziness has nothing to with the posters question. There is nothing wrong with Git , I just don't agree with the comparison.

euroderf
1 replies
2h49m

So where is the totally refactored/revamped CLI that applies Occam's Razor ?

fragmede
0 replies
15h35m

I'll have to dig into https://github.com/martinvonz/jj hadn't heard of it before. Either way, there's an underlying data structure with operations to be performed on, the interface to it is shit, and learning the underlying data structure makes it all make sense.

shadowgovt
0 replies
17h16m

"Needlessly" can be in the eye of the beholder, but given that Linus basically put it together to solve the problem he was trying to solve without consulting a UX team or building a product for the ages, I would be willing to place my probability bar pretty high on the design tilting towards "One person's best idea right now" over "Best design theoretically possible."

That having been said, the team is really good at engines so the software is quite good at doing what it's supposed to do.

eviks
0 replies
11h26m

WCan you give at least a few examples of complex operations that only arise at the scale of thousands of people?

vrinsd
8 replies
17h47m

Yes. Because git was created by Linus Torvalds specifically to enable and advance Linux kernel development.

The fact it has become so widly adopted is a "happy accident" (or misery). Github I think has been widely credited with making git so common place ("free git hosting") but I don't think git is the "ideal" revision control system for many projects.

Because it was made for Linus for Linus/Linux, this is why the terminology and sometimes workflow is backwards from every other "normal" version control system. A "Pull Request" (PR) is named that because for Linux development Linus/maintainers would PULL changes into Linux kernel tree. In almost every other version control system it's called a Merge Request which logically makes MUCH more sense because you are merging a change (where ever it came from) into a tree.

Git was (still?) the only open-source distributed version control system that could handle enormous code bases with good speed which means it gained a lot of inertia at big companies as well.

I vastly prefer and use Mercurial for personal projects but if you have tried to use Hg on huge code bases (I've tried on Android AOSP) it really chokes. Mercurial got the "user interface" and command-set right and has some nifty features (being able to launch a web server on the fly to get a tree view of your changes) but being written in Python, performance and scalability aren't ideal.

wakawaka28
4 replies
15h12m

Mercurial got the "user interface" and command-set right and has some nifty features (being able to launch a web server on the fly to get a tree view of your changes) but being written in Python, performance and scalability aren't ideal.

I used to think Mercurial had a better UI but I changed my mind after taking the time to understand Git. Mercurial does have nifty features but Git's way of working isn't hard or especially counterintuitive. You must learn the terminology to properly understand it, but after that it's smooth sailing. Git actually has fewer moving parts than Mercurial, as there are less extraneous features. Instead of 3 or more categories of "branch-like" graph structures like Mercurial, you get branches as refs in Git. Every named leaf in the commit graph is a branch or a tag, and those have simple properties. There are surely git plugins to add more metadata but you don't really need that.

Git too has a web interface by default: https://git-scm.com/docs/gitweb Of course there are many Github-like solutions specifically catered to hosting repos and entire project workflows with Git too.

wruza
1 replies
8h54m

You must learn the terminology to properly understand it, but after that it's smooth sailing

This promise never held to me. I know the terminology and “how”, but it never got clear in my mind how to express day to day things which aren’t clone/add/commit/push. It’s just bad unmemorable ui that is both cumbersome to use and reason about.

I do have trouble remembering things but not like this with any other tool. For example, with ffmpeg and magick I could make them work without a manual. With git I can only damage things irrepairably.

wakawaka28
0 replies
2h48m

Lol if you can remember how to use ImageMagick and Ffmpeg but not Git, I don't get it. Those programs do have very different argument passing styles than Git, but Git uses the same style as most every other unix/linux program. If you use it and know what you're doing, you should not be able to lose any work.

My advice to get over the hump you're on is to commit more often, check the status more often (`git diff [--staged]` and `git status`), and make sure you're branching or tagging enough to not lose commits. Then work on how to edit commits with `git rebase -i`. You can use rebase and cherry-pick to eliminate any unwanted commits or branches easily (notwithstanding conflicts, which are another critical thing to learn). If you feel like you messed up, you can almost always `git reset --hard` to the last commit you were on. If you forgot which commit you were on, there's the reflog. Generally, to be good at Git you have to take a few hours to learn it. It differs from the other programs you mentioned because there are different problems you can encounter at various points while using Git, whereas those other ones just succeed or fail and don't usually fail in unrecoverable ways (you just run them again until you're satisfied with the output). You can iterate on modifications to your commit history with Git but that is trickier and riskier.

There are many excellent Git tutorials online that explain everything an ordinary user needs, including rebases and the reflog. I think Github has one. Maybe that's what you need to figure it out, in addition to practice of course.

wakawaka28
0 replies
12h17m

I wouldn't say that these features are unfinished. They're just "extra" features that don't really add anything to the experience. People who like Mercurial rationalize and say that this gives you more options, of course. I think you can't really delete named branches in Mercurial, but don't hold me to that. The one that corresponds to Git branches is bookmarks, and of course bookmarks are the last thing you ever learn about when you are picking up Mercurial.

It is kinda nice to not be able to accidentally lose a branch even if you're a low-skill Mercurial user, and that is the main selling point for most people. On the other hand, low-skill Git users lose stuff often. It's actually really hard to lose committed stuff in Git because of the reflog, but the low-skill users never heard of that and you can really have a hard time explaining it to them. The other big complaint is about `git reset` and its various incarnations. People really don't take the time to understand what it does with its various options, and when they delete their changes accidentally they blame the tool.

8n4vidtmkvmk
1 replies
17h19m

There's a C version of hg, and more recently a rust version. I can't say how much faster they are though

wakawaka28
0 replies
15h34m

I don't think those other implementations were ever finished.

guenthert
0 replies
9h28m

It might also help understanding, when talking about the history of git, to keep in mind that it came to be out of the urgent need for a tool to replace (then still proprietary) BitKeeper. I wouldn't say that bk is as hard to grok as git, but it's certainly complex as well. I wonder if some of git's idiosyncrasies are a result of intentionally avoiding a look&feel equivalent to bk lest that would alienate Mr. McVoy.

sam_bristow
2 replies
13h51m

Some of the complexity comes from the fact that in earlier versions it was reasonably expensive to add new subcommands. As a result more functionality got added to existing subcommands like git checkout. This made the UX more confusing.

In newer versions of git the cost of adding new subcommands is reduced and they've started adding more specific commands like git switch.

tambourine_man
0 replies
6h4m

Why was that?

marssaxman
0 replies
1h35m

The addition of `switch` and `restore` has probably reduced my daily cursing-at-git rate by half.

wsc981
0 replies
18h4m

I think most of the time you’d be ok just knowing a couple of git commands (log, add, commit, pull, push, ..?). And of course there’s also GUI tools.

I don’t think git is that complex for most workflows to be honest. But there is a lot of power that is available if the need arises.

tasuki
0 replies
12h30m

Git is not complex. Its UI is!

Git is very simple. It is built on just four concepts: blobs, trees, commits, refs.

jimbob45
0 replies
18h23m

Because Linus had more clout in the SE industry than anyone else and said “SVN bad” and nobody wanted to do enough research to disprove him so here we are.

Merging in Subversion is a complete disaster. The Subversion people kind of acknowledge this and they have a plan and their plan sucks, too. It is incredible how stupid these people are.

So for example, let's go back to one of the things where I think the designers of Subversion were complete morons. Strong opinions. That's me, right? There's a few of them in the room today, I suspect. You're stupid. (Crowd laughs)

Nobody is interest in branching. Branches are completely useless unless you merge them.

https://sandeep.ramgolam.com/blog/linus-torvalds-talks-about...

eviks
0 replies
11h23m

Because good UI design requires a different skill and takes extra effort to achieve, so it's most often just ignored

thecrims0nchin
17 replies
18h25m

I liked the way I learned git. I started with sourcetree, a third party git gui. Super simple to use and understand. Then I moved to git gui, this step could be skipped. Then I got tired of pulling up my UIs and learned the terminal commands. Highly suggest this for anyone new, but just my (n=1) experience.

SoftTalker
8 replies
18h3m

All I know of git is clone, pull, commit, and push. That’s all I’ve ever needed. Git can be that simple. Of course it supports more complex uses, which you can learn if you need them.

lacedeconstruct
3 replies
17h26m

Life is simple until we play with time, once you start altering the past everything falls apart

Zambyte
1 replies
16h15m

It's not possible to alter commits with git. They are immutable. You can only make new commits that are based on existing ones in some way. The distinction is important, because the original commit (such as a commit pre-rebase) still exists after it's been "changed".

jcranmer
0 replies
15h0m

The distinction is important, because the original commit (such as a commit pre-rebase) still exists after it's been "changed".

The original commit will only exist for as long as git deigns not to garbage collect it.

knighthack
0 replies
16h25m

This... seems a bit deeper than a comment about just Git.

joseda-hg
1 replies
3h8m

I'd add Stash, unless I'm working alone

SoftTalker
0 replies
1h53m

Yeah even that has caused me trouble. I usually just copy the problem file to another directory, checkout the file from the repo, pull, and then diff/merge my changes. It may sound like more work, and maybe it is, but I understand what's happening and it's rare enough for me that to do it "the git way" I'd have to look it up every time.

yxre
0 replies
17h10m

The use case for more is managing the history of it in a specific way to make it easier to understand. Most applications don't need this level of management since most applications don't have lives depending on them

bun_terminator
0 replies
13h49m

And that's the only reasonable subset of git there is - I agree. Branches and cherry picks aren't worth the insurmountable headaches they come with

therealdrag0
4 replies
17h58m

Btw I moved from Sourcetree to Fork. It’s very similar but way more performant and smooth UI (unless ST has improved in last few years)

dryanau
1 replies
16h27m

I like GitKraken, except for the fact that it lacks the ability to show first parent only, i.e. `git log --first-parent`. That feature is available in Visual Studio and Visual Studio Code (Git Graph extension), but I don't like these as much as GitKraken, so I end up switching between command line, GitKraken and VS/Code. It's a bit of a mess. Does Fork support `--first-parent`? It's especially hand in merge-heavy workflows with very noodly graphs that you want to simplify.

bpugh
0 replies
15h54m

Not only does Fork support --first-parent, but it even lets you expand/collapse any merge commit just by clicking the node in the graph! Agreed it's the best way to make sense of a merge-heavy history.

I highly recommend Fork.

thecrims0nchin
0 replies
15h0m

Thank you! Had never heard of this. Ill give it a try!

nozzlegear
0 replies
17h39m

I love Fork, one of my favorite macOS apps. I wish I could get it on my iPad as well.

Barrin92
1 replies
16h21m

Then I got tired of pulling up my UIs and learned the terminal commands

I'd never revert to terminal commands. Git is basically begging to be interfaced with through a GUI given how much sense it makes to visualize a git repo and its workflows. The moment I started using Magit git started to make sense for me.

jjayj
0 replies
16h18m

lazygit is a godsend, personally. `git add`ing individual files is a PITA.

toinebeg
0 replies
9h4m

I had kind of the same path but with GitKraken and Sublime Merge but I kept Sublime Merge around as it ease complex interactions too.

I have only praise for Sublime Merge.

Panzer04
14 replies
17h15m

I always find it interesting how knowledge makes it hard to comprehend the difficulties other people have. I used to barely understand git, but after a couple of years working at a company that relies on a rebase workflow, I'd like to think I largely understand how it works. Its nice to recognise pretty much every operation on this cheatsheet.

Is it common that developers still have issues with git after using it professionally for a couple of years? Admittedly, I still have trouble remembering the commands to do what I want (hello, rebase --onto A B C), though :P.

acjohnson55
2 replies
16h39m

The skillset to navigate this is one of the underappreciated challenges of teaching.

vsnf
1 replies
15h22m

Exactly zero of my mathematics professors, and a large portion of my CS professors managed to adequately account for this. The professors of mine who did manage it remain firmly in my memories as great people and great teachers, while the one's who didn't inhabit an area between complete memory erasure, and disdain for how hard they made my life. This is at the very core of what it means to be a teacher, not just knowing the material, but knowing how to teach the material. I consider many of them to be failures at their main job for failing to navigate the curse.

erik_seaberg
0 replies
13h21m

In the middle of delivering a lecture, Hardy arrived at a point in his argument where he said: "It is now obvious that..." Here he stopped, fell silent, and stood motionless with furrowed brow for a few seconds. Then he walked out of the lecture hall.

Twenty minutes later he returned, smiling, and began: "Yes, it is obvious that..."
smusamashah
2 replies
16h44m

I understand the commonly used core concepts of git pretty well, and using that knowledge, I can do exactly what I want using Git GUIs (IntelliJ these days). With CLI I can do pulls and commits at most.

pacoverdi
1 replies
10h39m

IntelliJ has become pretty powerful with Git these days.

Squashing, dropping commits, rebasing. Even interactive rebase can be done in a few clicks.

I sometimes pop up IDEA to do some non-trivial cleanup in a repo. Even if relatively fluent with the command line, it saves me a lot of time.

smusamashah
0 replies
9h1m

What I like most, and isn't available in most UIs, is the ease of selecting individual lines you want to commit. Allows you to make very clean isolated commits most of the times.

sirentdiver
1 replies
17h8m

Admittedly, I still have trouble remembering the commands to do what I want (hello, rebase --onto A B C), though :P.

I work at a company where every developer uses Sourcetree, a GUI for Git. I don't really have a need to remember Git commands (and what arguments they take) anymore after using a GUI. For me, it is sufficient to know what Git commands do without knowing the semantics.

dtgriscom
0 replies
15h13m

I always use SmartGit, another GUI. (I've lauded it to all and sundry, but have yet to inspire a new devotee...)

PhilipRoman
1 replies
11h47m

I can confirm the part about rebase workflow, it really forces you to understand the git model. I sincerely recommend every dev to try it at least once. It also got me into custom power tools for git, like incremental rebase, dual blame, etc.

PhilipRoman
0 replies
8h40m

Too late to edit but I just realized we probably mean very different things by "rebase workflow". I meant making extensive use of Git history editing even on public branches, (with tags serving as backup). I guess the common meaning of "rebase workflow" is just using rebase instead of merge.

xeonmc
0 replies
16h59m

I always find it interesting how knowledge makes it hard to comprehend the difficulties other people have.

Once the mind had bootstrapped itself to a concept, it is very hard to empathize with your past self unless you took cares to document the difficulties while still in the confused state, articulating as much detail and as many fallacies encountered as possible.

I always liken it to Wittgenstein's analogy -- don't throw away the ladder after having climbed up upon it!

jacobrussell
0 replies
17h2m

I can do git pull, branch, add, commit, push, merge, diff, status, stash pretty much without thinking but much beyond that I have to look it up.

CSMastermind
0 replies
16h37m

Is it common that developers still have issues with git after using it professionally for a couple of years?

As someone who has worked on developer tooling, including building a custom git client at a former job - yes.

Most people learn a few specific workflows not conceptually how things work under the hood.

Visual editors like GitKraken, Submlime Merge, Sourcetree, etc, have made this much better but there's still a big gap.

behnamoh
7 replies
18h1m

Are there git alternatives that aren't a PITA to master before one can use them in production?

cjensen
2 replies
17h24m

Subversion. It was made by CVS maintainers and a lot of care and thought was put into how it works and how the commands work and what the names of the commands are. Subversion is limited in being an older model of version control where there is exactly one single source of truth.

In my mind Git solves a different problem that Linus Torvalds was facing: many people working in parallel with only some of the ideas pushed upstream, but many people wanting to make changes and easily being able to move forward to the latest changes from upstream.

My extremely opinionated view is that most people should use Subversion unless they have a very complicated team structure. And that means every solo dev working by themselves should not complicate their own lives by using Git unless they are really comfortable with it already.

tasuki
0 replies
12h24m

I have just the opposite view of yours! Subversion is so so complex. It requires a client and a server. It might have a nice UI, but its internals are a mess. Just the opposite of git!

Why would a solo dev choose to use a client-server version control instead of one that works fully locally?

erik_seaberg
0 replies
16h35m

Git is well worth learning for any kind of collaboration or speculative work. It's fundamentally better at resolving merge conflicts, because it doesn't rely on being informed about which files may have been moved or copied. The Subversion maintainers owe me days of effort salvaging my small team's branches.

vrinsd
0 replies
17h36m

Mercurial if you're not on a huge code base. While the core command-line hg works great, GUIs and third-party programs are kind of lacking.

Atlassian Sourcetree in theory works with Hg (because Atlassian's Bitbucket used Mercurial as their primary version control system before moving to git) but SourceTree is Windows/Mac only, there is no Linux version for some strange reason.

Trying to run SourceTree on Linux via WINE fails because Sourcetree can't be installed with the 'Administrator' user and recent WINE releases have (accidentally?) removed the ability to run a WINE program as a non-Administrator user.

I haven't personally tried (yet) to install Sourcetree on a Windows machine and copy the directory over and try via WINE, but it's on my list to do.

sunshowers
0 replies
16h18m

Jujutsu! Git-compatible and a generational leap in source control that most people are going to love.

As a former member of Meta's source control team, I believe this with pretty high confidence -- many of the workflows that we rolled out within Mercurial/Sapling, and caused it to consistently be the most loved developer tool at Meta for many years based on surveys and interviews, have been adopted and improved by Jujutsu. See my testimonial, the first one at [2].

[1] https://martinvonz.github.io/jj

[2] https://martinvonz.github.io/jj/latest/testimonials

(Disclosure: I'm obviously biased towards the Jujutsu workflows, and its creator and I worked together on Mercurial. But I like to think that this specific belief of mine is pretty data-driven, both via stated and revealed preference.)

majoe
0 replies
11h38m

git is now so ubiquitous in software development, that it's hard to justify learning another version control system. You're probably better off learning the core concepts and using the most common git commands through some GUI.

abenga
0 replies
17h11m

A small, simple subset of git? You generally don't need all the esoteric stuff people complain about day-to-day.

Pretty much what is listed in the OP cheat sheet.

redbell
5 replies
12h48m

"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton [0]

Based on the above statement, I find it extremely weird to see such git command named "blame", then I realized I'm not the only one:

What does 'git blame' do? [1].

Blame someone else for your bad code [2].

Git blame should be called git credit [3].

Does Git Blame sound too negative? [4].

______________________________

0. https://skeptics.stackexchange.com/questions/19836/has-phil-...

1. https://stackoverflow.com/questions/31203001/what-does-git-b...

2. https://news.ycombinator.com/item?id=27963868

3. https://dev.to/damcosset/git-blame-should-be-called-git-cred...

4. https://www.reddit.com/r/ProgrammerHumor/comments/r5lzyo/doe...

leipert
2 replies
12h36m

I think a better name would be “git praise”

petepete
0 replies
12h28m

I'm sure you know this, but for younger readers, in Subversion 'praise' was an alias for 'blame'.

For neutrality, 'annotate’ was another alias.

omoikane
0 replies
2h14m

One advantage that "git blame" has over possible alternatives such as "git credit", "git author", or "git praise", is that it's one character shorter and maybe faster to type.

For the same number of characters, I have heard of "git glory" as a possible alternative.

Wowfunhappy
0 replies
3h50m

I think `blame` is on-brand for a tool called `git`.

TiredOfLife
0 replies
4h13m

and off-by-one errors.

nomilk
5 replies
17h50m

A couple of hidden gems:

git diff --staged (shows the diff between the last commit and what you've got staged, great when what you're staging is finicky, lets you double check that you have precisely the files/hunks you intended)

git log <file> (git log, but for just commits where <file> was touched; saves pilfering through all of git log, especially useful when the file only gets modified infrequently)

Random tip: when using git add --patch, try the 'e' option if you need more nuance than the hunks git provides (it contains instructions for how to ex/include individual lines, so you don't have to remember how to do it).

omginternets
1 replies
17h27m

15 years… 15 years to discover git diff --staged !

neoecos
0 replies
17h20m

I don't consider myself a git savvy person, but the help gives output, and I think this command is used a lot

m0zzie
1 replies
16h37m

Wow, TIL `git diff --staged` exists and is an alias to `git diff --cached`, which I have used for many years. "staged" certainly feels more intuitive, but will I ever be able to undo this muscle memory?!

anbotero
0 replies
3h23m

Same here. Just make it an alias: ds.

I’m using git dc, but it seems ds may be my new one, haha.

Falimonda
0 replies
15h5m

I find aliasing a lot of these saves me a good chunk of time and reduces the barrier to better organize my work.

In case anyone cares to browse or suggest their own: https://gitlab.com/Falimonda/gitrc

pncnmnp
3 replies
14h45m

I have a funny little habit that probably stems from a certain degree of paranoia when working on a decent-sized change. I like to keep a local patch of it, as follows: "git diff > works.patch"

Later on, you can apply it if necessary with "git apply works.patch".

erik_seaberg
1 replies
13h38m

Do you find that better than a stash or a branch because it shows up in ls?

pncnmnp
0 replies
12h23m

Yes, it can be nice as an archival thing. Keep a directory filled with experimental patches you made to certain parts of your code. Maybe branches are better for certain use cases.

I use stash too, but for different purposes. For instance, when I'd like to quickly change branches to fix a bug/issue and then come back to my stuff. Sometimes, I use stash for resolving potentially complicated pre-commit merges as well.

BossingAround
0 replies
4h30m

You could use stash for just that use case (even multiple times to make multiple "patches").

For a more robuts method, you could also just commit (without pushing of course). Then, you can do "git reset --hard HEAD~1", and you have a pre-patch env ready for more work. When you need to re-do your commit, you can check reflog to get the ID.

Either way is, to me, an easier way to achieve the same result as patches.

koito17
3 replies
12h53m

Cool cheat sheet, especially because it covers common use cases. But practically everything covered here I was able to learn independently with Magit. It's also much easier to press one or two keys in the magit-status buffer than to open a terminal and manually type git commands, so I pretty much never use the git CLI directly, not even for a clone.

wodenokoto
1 replies
12h36m

But it requires you to learn emacs. I thought I could just apt-get emacs and then from there do something to the effect of "install magit" and have a easy to use interface to git.

I half a day later I gave up. Never got to install magit.

noman-land
0 replies
1h53m

I had this exact experience trying to install Common Lisp. Installed emacs and wtfed out of there 30 min later having learned nothing and feeling slightly traumatized.

plowjockey
0 replies
2h8m

I am just the opposite. I started using Git not long after it gained traction outside of the kernel project. Of course, it was primarily geared toward working in the terminal then. Some years later I had switched to Emacs and gave Magit a whirl. Even after going through quite a bit of its copious documentation, I just never felt comfortable with it. I guess I don't like being that far removed from the actual goings on with the repository.

I've since switched to Vim and happily use git at the terminal prompt. I doubt that will ever change as the interactive interface of various Git commands has improved.

ruraljuror
2 replies
18h3m

Love this. I would add:

  git commit —-fixup $COMMIT_ID
combined with

  git rebase -i upstream/master —autosquash
These have become staples of my recent workflow. Julia’s example uses HEAD^^^^^ to rebase the previous 5 commits. I have been doing this as HEAD~5, until recently I realized you can just rebase all commits up to the upstream HEAD.

mwpmaybe
0 replies
13h29m

Ahh, thank you:

    git commit —-fixup $COMMIT_ID
I'm adopting this immediately. I always make little "oops <topic>" commits like "oops GET users/me" and then manually move and fixup them on my next interactive rebase. This is much better!

lathiat
0 replies
16h54m

I only recently learnt this one, it's great for working with stacked patches in gerrit.

AnonC
1 replies
11h54m

Unfortunately, this requires creating a Medium account and signing in. Maybe it also requires payment later on.

This is the message I see.

The author made this story available to Medium members only.

If you’re new to Medium, create a new account to read this story on us.
wyldfire
1 replies
16h36m

The biggest eye opener for me is understanding that all those commits are "still in there". Tags and branches merely point to a commit and the commit that branch used to apply to isn't gone.

Many (probably fair to say "nearly all"?) commands will be non destructive in that you can easily refer to older incarnations of your branch.

I'd seen "git reflog" but mostly used it to remind me of "that branch I worked on a while ago".

I gained a semi-healthy fear of operations that could land me in the middle of a conflict resolution that I wasn't interested in handling right now (often because the conflict occurred because I was referring to the wrong commit/branch).

I haven't completely cured that fear but at least I feel pretty comfortable recovering any old work among rebases, etc.

Zambyte
0 replies
16h17m

Jujutsu has 100% cured me of those exact fears. Here are some ways I deal with them:

- I don't know if there will be conflicts when after some operation. I just do the operation and see what happens. If there are conflicts, I either deal with them, undo the operation that caused the conflict, or make a me commit somewhere else, leaving the conflict to be dealt with later.

- I know there is going to be a conflict. In this case I may duplicate the related branches and either try to reconcile them ahead of time, or try to do the conflicting operating and walk through resolving it. If the conflict resolution gets gnarly, I can either undo portions of the resolution and keep trying, put it off and do something else for some time, or abandon the whole tree.

wodenokoto
1 replies
12h39m

Find every commit that added or removed some text > git log -S banana

I literally spend half an hour the other day having an argument with chatGPT about how I could find versions of a file that contained a specific string. Guess I should have asked for commits and not versions of files.

kzrdude
0 replies
12h30m

git log -G is much more often what I want - diff contains some text but doesn't necessarily add or remove it.

mariocesar
1 replies
17h52m

I didn't know about `git add -p`. Really useful!

  -p, --patch
  Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.

Fellshard
0 replies
17h18m

I always custom-select hunks of my current work to assemble into coherent commits, where possible. I find it really helps me to rearrange the work I've done in my head in such a way that if I needed to walk someone through it, I could do so by walking the commit line.

johnwheeler
1 replies
17h18m

I'll say it aloud: People need to drop git and look at JJ

https://martinvonz.github.io/jj/v0.17.1/

I think even Linus thinks git has a shitty UI. JJ has an amazing workflow.

pixelesque
0 replies
16h27m

Going to have to disagree on the lack of an index: I can see workflows where that's simpler, but I'm so used to that now with git (for better or worse) I'm imagining that'd be a pretty big workflow change for me...

Other than playing around with it, I haven't really used it in anger though, so...

urmish
0 replies
15h1m

Read once here, the best cheat sheet is one you create yourself. In my case, with fish shell or bash history search with fzf, I've effectively created a cheat sheet of commands I frequently tend to use with working examples. Seems to be working for me so far.

timvisee
0 replies
11h44m

`git rebase --onto FIRST_ID^ NEW_PARENT`

is also super powerful to put a series of commits on top of a new parent when history has changed.

That happens with PRs and squash merges quite often.

https://stackoverflow.com/a/29916361/1000145

thefz
0 replies
8h13m

Julia Evans' content is always high quality.

shmerl
0 replies
13h15m

For listing branches, git branch -vv gives some more info.

rswail
0 replies
4h15m

Git has its quirks, but the underlying model is good.

There's some naming problems, like I just had to explain lightweight tags vs annotated ones to some newbies.

The lightweight ones should have had a different name, like "bookmarks" or something.

rezonant
0 replies
13h33m

One thing missing that I use all the time, "git cherry". Without arguments it will show you what commits in your local branch are not in your remote tracking branch. Use "git cherry <ref>" to list the commits in the current branch that are not in the given ref.

Most simple use is to know what you are about to push. Second use case is getting a commit list for release notes when using deployment/promotion branches.

Super useful, I thought everyone used it but maybe some don't know!

jmercouris
0 replies
17h18m

If you aren't already, you can also try Magit. It really helps one master and interact with Git.

iamcreasy
0 replies
15h43m

git reset HEAD is nifty way to unstage everything! Really cool!

codenlearn
0 replies
17h13m

the one thing that is handy and missing here is creating and pushing tags.

abalone
0 replies
13h8m

This is great, especially the little diagrams which help illustrate rebasing vs merging, one of the tricker parts of the git learning curve.

Having said that I think it could more clearly state the author's typical preferred practice for updating a branch and integrating it with main when you're done. There's a few techniques listed for diverged branches, but then fast-forward merge is in its own box and it's not super clear how you might use working-branch rebasing with it to eliminate divergences in the first place (even though it's hinted at under "pull changes").

I wonder if it would be clearer to say something like "combining branches: rebase your branch and do a (FF) merge, optionally with squashing," or however they like to do it.

KennyBlanken
0 replies
12h40m

These cheat sheets are a great idea spoiled somewhat by the use of an infuriatingly annoying-to-read font.

If your mission is to make hard topics easier to understand, why are you using a hard-to-read font whose only purpose is to make the cheat sheet look folksy?

Dban1
0 replies
12h20m

git fetch origin <branch_name>:<branch_name>

to update a branch while you're on another branch

ChrisMarshallNY
0 replies
17h37m

She delivers, yet again.

Thanks!

1000100_1000101
0 replies
14h35m

One of faves isn't listed.

  git log -LstartLine,endLine:fileName
It's like an extended git blame, showing how the specified lines evolved over time, and by who. Tracks the change as the line numbers evolve, and even as the file is moved.

Answers the questions of "who did this, when, and why?" / "How long has it been like that?", etc.