News for hackers. Stuff that matters.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @05:05AM (#39356184)
Heya, author here.

I have to admit that I learned a lot of these things fairly recently. The large repository stuff has been added into core piece by piece by Microsoft and GitHub over the last few years, it's hard to actually find one place that describes everything they've done. Hope it's helpful.

I've also had some fun conversations with the Mercurial guys about this. They've recently started writing some Hg internals in Rust and are getting some amazing speed improvements.

I'm also thinking of doing a third edition of Pro Git, so if there are other things like this that you have learned about Git the hard way, or just want to know, let me know so I can try to include it.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by goku12 on Tuesday February 13, 2024 @07:33AM (#39357022)
One thing about git I learned the hard way is the use of diffs and patches (more accurately, 3-way merges) for operations like merging, cherry picking and rebasing. Pro-git (correctly) emphasizes the snapshot storage model of git - it helps a lot in understanding many of its operations and quirks. But the snapshot model can cause confusion in the case of the aforementioned operations - especially rebasing.

For example, I couldn't understand why the deletion/dropping of a commit during a rebase caused changes to all subsequent commits. After all, I only asked for a snapshot to be dropped. I didn't ask for the subsequent snapshots to be modified.

Eventually, I figured out that it was operating on diffs, not snapshots (though storage was still exclusively based on snapshots). The correction on that mental model allowed me to finally understand rebasing. (I did learn later that they were 3-way merges, but that didn't affect the conclusions).

That assumption was eventually corroborated somewhere in Pro-Git or the man pages. But I couldn't find those lines again when I searched it a second time. I feel that these operations can be better understood if the diff/patch nature of those operations are emphasized a bit more. My experience on training people in rebasing also supports this.

PS: Thanks for the book! It's a fantastic example of what software documentation should look like.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by smallpipe on Tuesday February 13, 2024 @08:47AM (#39357547)
> Eventually, I figured out that it was operating on diffs, not snapshots

The snapshot include all the history that led to the current snapshot. So even if you did a squash instead of dropping, you're changing everything that depends on that

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by goku12 on Tuesday February 13, 2024 @11:10AM (#39359048)
> The snapshot include all the history that led to the current snapshot

Git snapshots don't contain any history, other than the commit chain (reference to the parent commit/s) in the commit object. While the storage format is a bit complex, they behave fundamentally like a copy of the working tree at the point of commit.

> So even if you did a squash instead of dropping, you're changing everything that depends on that

Squashes don't change the subsequent commits/snapshots either, other than the commit ID and chain. The tree itself remains untouched. You can verify this.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by elevatedastalt on Tuesday February 13, 2024 @06:24PM (#39364299)
Yes, but since they change the commit id, and since each commit has a pointer to its parent commit, you have to rewrite all the commit objects, even if the tree doesn't change.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by goku12 on Tuesday February 13, 2024 @07:18PM (#39364710)
The context of my first comment is about how a rebase affects the contents of the tree. It's about predicting what happens to subsequent snapshots. Bringing commit ID and parent commit into that context complicates and obscures the point I'm trying to convey.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by seba_dos1 on Tuesday February 13, 2024 @10:38AM (#39358579)
What I'd stress out is that rebasing is nothing else than automated cherry-picking, as it's hard to imagine cherry-picking in any other way than 3-way merge or patch operation.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by phillco on Tuesday February 13, 2024 @02:03PM (#39361375)
Julia Evans wrote a nice blog post about this recently, in that sometimes it's helpful to think commits as snapshots and sometimes as diffs.

https://jvns.ca/blog/2024/01/05/do-we-think-of-git-commits-a...

I guess while it's true the storage layer is snapshot based, as you say, that only gets you so far conceptually, and it's probably best to focus on the _operation_ you're doing, as rebase, cherry-pick, apply-patch, etc are easier to think in terms of diffs.

When I used to use Phabricator, the fact that I could always fall back to handing it a raw patch file to submit changes also made it easier to reason about (regardless of what the server and client were actually doing).

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by infogulch on Wednesday February 14, 2024 @01:01AM (#39366994)
Commits have a dual snapshot-like and diff-like nature, depending on how you observe them.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by ulope on Tuesday February 13, 2024 @06:03AM (#39356582)
I just wanted to say thanks for the entertaining talk you gave at FOSDEM and also that I appreciated the Sneakers reference :)
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @06:04AM (#39356597)
Ha! Yeah, I was wondering if anyone would catch that. I thought I heard a snicker or two in the audience, but I couldn't be sure.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by Towaway69 on Tuesday February 13, 2024 @07:11AM (#39356914)
Great tips and great article :+1:

One question that I have is what is happening to large file support within Git? Has that been merged into the core since Microsoft changes have also made it into core. Obviously there is a difference in supporting very many small files or a few very large files but won't it make sense to roll LFS into core as well?

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @07:24AM (#39356981)
What a great question. If I recall correctly, the LFS project is a Go project, which makes it difficult to integrate with Git core. However, I believe that the Git for Windows binary _does_ include LFS out of the box.

There was a discussion very recently about incorporating Rust into the Git core project that I think had a point about LFS then being viable due for some reason, but I'd have to find the thread.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by Towaway69 on Tuesday February 13, 2024 @07:31AM (#39357013)
Thanks for the insight. I'm surprised to hear that LFS is Go based, I would have thought LFS outdated Go - but learn something new everyday! :)
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @01:46PM (#39361187)
Looks like it was started in 2015, so Go had about a 6 year head start on it:

https://github.com/git-lfs/git-lfs/blob/main/CHANGELOG.md

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by diarrhea on Tuesday February 13, 2024 @07:02AM (#39356866)
I remember watching your FOSDEM talk on YouTube, where you asked whether people have rerere turned on _and_ know what it is, in one question. I have it on, but only the faintest of clues what it is! Just git things, I suppose.

https://youtu.be/aolI_Rz0ZqY?t=910

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bemusedthrow75 on Tuesday February 13, 2024 @06:38AM (#39356752)
Thank you for your writing on Git over the years, particularly Pro Git, which is helpful.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @06:41AM (#39356767)
Thank you for reading. :)
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by SushiHippie on Tuesday February 13, 2024 @05:41AM (#39356416)
Hey little feedback on the terminal images in your posts. I'm viewing this on a phone, and it would be better if the terminal images were just the terminal (some are) and not surrounded by a large blank space which is your wallpaper. This would make it a bit easier to read on small screens, without the need to zoom in!
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @05:45AM (#39356450)
But then would it be as pretty?
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by juliusdavies on Tuesday February 13, 2024 @06:01AM (#39356566)
I wrote this and wish more people followed this specific git advice:

https://mergebase.com/blog/doing-git-pull-wrong/

TLDR: don’t be afraid of rewriting history but ALWAYS do “git pull -r —autosquash “

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by vinc on Tuesday February 13, 2024 @08:30AM (#39357392)
I watched the FOSDEM talk yesterday, and I laughed hard when I heard "Who use git blame -L? Does anybody know what that does?" because it suddenly looked like the beginning of a git wat session. But it was really informative, I learned a lot of new things! Thanks
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by foobarian on Tuesday February 13, 2024 @08:30AM (#39357399)
I chuckled at the title - "So You Think You Know Git" - no, one does not think one knows git :-)
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by pantulis on Tuesday February 13, 2024 @08:34AM (#39357425)
Git thinks it knows you.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by unclebucknasty on Tuesday February 13, 2024 @08:40AM (#39357474)
Never not a good time for this:

https://xkcd.com/1597/

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by rafacm on Tuesday February 13, 2024 @08:36AM (#39357446)
Except Chuck Norris that is!
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by TravelPiglet on Tuesday February 13, 2024 @12:34PM (#39360245)
Chunk Norris
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by nemoniac on Tuesday February 13, 2024 @08:50AM (#39357567)
How about an RSS feed on your blog? If there's one there, it's not obvious.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by tsukurimashou on Tuesday February 13, 2024 @09:19AM (#39357816)
https://blog.gitbutler.com/rss/

most of the time trying the main URL + /rss works

also the tag is there <link rel="alternate" type="application/rss+xml" title="GitButler" href="https://blog.gitbutler.com/rss/">

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by surge on Tuesday February 13, 2024 @08:57AM (#39357635)
Hey Scot,

I met you and we chatted for a bit at a bar after hours at a tech conference years ago, before you dropped you were a GitHub co-founder towards the end. You actually gave me some advice that has worked out well for me. Just wanted to say thanks!

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @09:57AM (#39358163)
In vino veritas. Thanks for the thanks. :)
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @09:30AM (#39357918)
You probably already know these bits & bobs, but I wanted to share:

  [diff]
    external = difft
Use the fantastic difftastic instead of git's diff. https://difftastic.wilfred.me.uk/

  [alias]
    fza = "!git ls-files -m -o --exclude-standard | fzf -m --print0 | xargs -0 git add"
    gone = "!f() { git fetch --all --prune; git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D; }; f"
    root = rev-parse --show-toplevel
Those are the most used aliases in my gitconfig.

"git fza" shows a list of modified/new files in an fzf window, and you can select each file with tab plus arrow keys. When you hit enter, those files are fed into "git add". Needs fzf: https://github.com/junegunn/fzf

"git gone" removes local branches that don't exist on the remote.

"git root" prints out the root of the repo. You can alias it to "cd $(git root)", and zip back to the repo root from a deep directory structure. This one is less useful now for me since I started using zoxide to jump around. https://github.com/ajeetdsouza/zoxide

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by keysersoze33 on Tuesday February 13, 2024 @11:18AM (#39359167)
Thanks for the difftastic & zoxide tips!

However, I've been using this git pager/difftool: https://github.com/dandavison/delta

While it's not structural like difft, it does produce more readable output for me (at least when scrolling fast through git log -p /scanning quickly)

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by pdimitar on Tuesday February 13, 2024 @02:35PM (#39361797)
Yep, I tried at least 3 other differs (difft included) but delta was perfect and still is. Using it all the time.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @09:50AM (#39358100)
This one I'm less sure about. I haven't yet gotten it to the point where I really like using it, but I'm sharing since someone might find it useful as a starting point:

  [alias]
    brancherry = "!f() { git checkout -b $(git rev-parse --abbrev-ref HEAD)-$(git rev-parse --short \"$1\") $1; }; f"
It's intended to be used for creating a cherry-picking branch. You give it an branch name, let's say "node", and it creates a branch with that as its parent, and the short commit hash as a suffix. So running "git brancherry node" creates the branch "node-abc1234" and switches to it.

The intended workflow being you cherry pick into that branch, create a PR, which then gets merged into the parent.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by ulope on Tuesday February 13, 2024 @10:16AM (#39358353)
difftastic - amazing!

I've been wanting something like this for years...

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @10:25AM (#39358441)
Be prepared to hand out the difftastic URL and install instructions a lot :) I get asked "what git setting is that?" when I do diffs while sharing my screen.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @01:35PM (#39361043)
meta-tip: you can also put your aliases that start with '!' into stand-alone shell scripts named `git-fza` (e.g.) and then call it as `git fza` which will search your PATH for `git-fza` and invoke it as if it's built-in.

I do this for some of my more complicated aliases because I generally think it's poor form to embed shell scripts into configuration languages. (Looking at you, yaml.)

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by svieira on Tuesday February 13, 2024 @02:30PM (#39361727)
I have several scripts like that in my PATH, but Git on Mac can never find them. What am I doing wrong?
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @06:00PM (#39364043)
The scripts have to be in your `PATH` and be executable from wherever you're running `git`.

Say you have a script named `git-foo`. At the shell prompt, all of these should work:

  $ which git-foo
  $ git-foo
  $ git foo
If the first or second commands fail, then `git-foo` is not in your PATH or is not executable. If those both work but the third command fails, I have no explanation. Here's the code which runs commands:

https://github.com/git/git/blob/2996f11c1d11ab68823f0939b646...

It basically prefixes your `PATH` (or a suitable default if `PATH` isn't set) with `GIT_EXEC_PATH` (defaulting to a compiled in value if not set) and then uses the normal Unix execvp machinery to run the command.

You can try:

  $ GIT_TRACE=1 git foo
But I'm not sure that will tell you anything helpful.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by svieira on Wednesday February 14, 2024 @02:39PM (#39374184)
Well, this is fun:

    $ which git-foo
    $ type git-foo
    git-foo is /Users/my.user/.local/bin/git-foo

    $ git-foo --help
    Help output from git-foo

    $ GIT_TRACE=1 git foo
    14:26:18.849078 git.c:749               trace: exec: git-foo
    14:26:18.849815 run-command.c:657       trace: run_command: git-foo
    git: 'foo' is not a git command. See 'git --help'.
I can't say I've ever seen `which` and `type` disagree before ...

And today-I-learned that while bash expands `~` in PATH entries other programs do not. The fix was changing my PATH from:

    PATH=~/.local/bin:other:paths
to:

    PATH=$HOME/.local/bin:other:paths
Thanks so much for the help!
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @06:15PM (#39364210)
What version of git do you have? If it came with macOS and wasn't installed via homebrew, it's possible that it's old enough to cause problems.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @10:55PM (#39366272)
git has had this behavior for at least a decade. As well, macOS does not ship with git - it's installed as part of either the Command Line Tools package and/or Xcode and is reasonably up to date.

For many years now, macOS has included what are effectively wrappers in /usr/bin for the various development tools and that use the xcode-select mechanism to run the actual command. If neither Xcode nor the CLT package are installed, you'll get a prompt to install the CLT package.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by bloopernova on Tuesday February 13, 2024 @02:00PM (#39361342)
Oh nice, I didn't know that!

For git fza I override an oh-my-zsh alias "ga" to run it.

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by olvy0 on Tuesday February 13, 2024 @11:01AM (#39358899)
Hi Scott!

First off, I loved your presentation. And your book. As someone who actually bothers to read most of github's "Highlights from Git" blogs, that the, I was somewhat familiar with some of them, but it was still very informative.

Also liked your side-swipe at people who prefer rebase over merge, I'm a merge-only guy myself...

I also took a look at GitButler and it looks like it could potentially solve one of my pain points.

If you're looking for things which are confusing to beginners, for a future version of your book, there are many useful / interesting / sometimes entertaining git discussions/rants here on HN. One of the recent ones is:

https://news.ycombinator.com/item?id=38112951

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @12:46PM (#39360421)
I love the GitHub Git blog posts. They should have a bigger audience. Taylor is a machine.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by wrs on Tuesday February 13, 2024 @12:32PM (#39360206)
In the part about whitespace diffs, you might want to mention ignore-revs-file [0]. We check an ignore-revs file into the repo, and anyone who does a significant reformat adds that SHA to the file to avoid breaking git-blame.

[0] https://git-scm.com/docs/git-blame#Documentation/git-blame.t...

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by schacon on Tuesday February 13, 2024 @12:44PM (#39360399)
I dont think I knew this. Great tip, thanks!
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by js2 on Tuesday February 13, 2024 @01:41PM (#39361119)
Note that git itself doesn't care what the ignore-revs file is called, but GitHub does. It has to be named `.git-blame-ignore-revs`:

https://github.blog/changelog/2022-03-24-ignore-commits-in-t...

One other thing you might want to be mention, which is obvious after thinking about it, is that updating the ignore-revs file has to occur in a commit after the one that you want to ignore, since you don't know what that first commit's ID is till after you make it. :-)

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by wrs on Tuesday February 13, 2024 @01:56PM (#39361302)
Yes! I recommend making a script that does that little dance automatically.
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by keybored on Tuesday February 13, 2024 @02:08PM (#39361433)
Hey, thanks for a great little series.

The part about `--force-with-leash` could include `--force-if-includes` as well. `--force-with-leash` doesn’t do much if you fetch often.

https://stackoverflow.com/questions/65837109/when-should-i-u...

Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by CRConrad on Tuesday February 20, 2024 @11:14AM (#39443067)
"--force-with-leash" sounds like git for the BDSM community...
Re:So You Think You Know Git – Git Tips and Tricks by Scott ... by lasereyes136 on Wednesday February 14, 2024 @10:17AM (#39370734)
The YouTube video of your presentation is great and I recommend to anyone that wants to learn more about Git or uses Git on a regular basis.

Thanks for the info.