3 Reasons to Upgrade Git For The First Time Ever
Relatively new Git features that actually have me excited #git #tips
Read time: 2 minutes Published:
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:
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
main, you can see my Cheatsheet for moving from Master to Main.
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
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
reset, and even
switch. This StackOverflow answer clarifies pretty authoritatively:
git-revertis about making a new commit that reverts the changes made by other commits.
git-restoreis 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-resetis 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.
Join 2,000+ developers getting updates ✉️
Too soon! Show me what I'm signing up for!