3 Reasons to Upgrade Git For The First Time Ever

Git moves at a surprisingly fast pace - as of Aug 2021, the current version of Git (git --version) is 2.33.

I don’t normally know or care what my Git version is, but over the past year this changed. The Git team must be doing something right, because I’ve noticed a few things trickle down to my relatively light usage:

init.defaultBranch

The default branch in Git is named master. As of Git 2.28, you can now configure it to anything else. Of course, this is merely symbolic but is an appreciated signal.

git config --global init.defaultBranch main

This setting will work for new repos. For existing repos, and git aliases that work nicely with BOTH master and main, you can see my Cheatsheet for moving from Master to Main.

git sparse-checkout

You probably know about git clone -–depth 1 [remote-url] for repos where you don’t want to download the full history. Git 2.25 introduced an additional command for giant repos, where even the current version is too big. So sparse checkout is like git clone --depth 1 but for files:

git clone --no-checkout https://github.com/my/big-repo
cd big-repo/
# only files at the root
git sparse-checkout init --cone 
# only files in specific directories (recursive)
git sparse-checkout set <dir1> <dir2> ...

# do it
git checkout main

More on the GitHub blog.

git restore

I needed this one today. Basically if you screw up a file and need to bring it back from Git, you can use the new git restore command in v2.23:

git restore path/to/file # discard current changes
git restore --staged path/to/file # unstage staged file
git restore --source [hash] path/to/file  ## restore from specific commit hash

⚠️ Be careful - once you run this command you cannot get back the changes you discard.

Yes, there are confusingly similar commands between checkout, restore, reset, and even switch. This StackOverflow answer clarifies pretty authoritatively:

  • git-revert is about making a new commit that reverts the changes made by other commits.
  • git-restore is about restoring files in the working tree from either the index or another commit. This command does not update your branch. The command can also be used to restore files in the index from another commit.
  • git-reset is about updating your branch, moving the tip in order to add or remove commits from the branch. This operation changes the commit history. git reset can also be used to restore the index, overlapping with git restore.

Tagged in: #git #tips

Leave a reaction if you liked this post! 🧡
Loading comments...
Webmentions
Loading...

Subscribe to the newsletter

Join >10,000 subscribers getting occasional updates on new posts and projects!

I also write an AI newsletter and a DevRel/DevTools newsletter.

Latest Posts

Search and see all content