Today I Learned - Rocky Kev

TIL git switch

POSTED ON:

TAGS:

These two commands replaces two common uses of git checkout.

git switch to switch to a new branch after creating it if necessary

git switch my-branch              # same as git checkout my-branch
git switch -c my-branch           # same as git checkout -b my-branch
git switch -c my-branch HEAD~3    # branch off HEAD~3
git switch --detach HEAD~3        # checkout commit without branching

git restore to restore changes from a given commit.

git restore --source HEAD~3 main.c  # --worktree is implied

rm -f hello.c
git restore hello.c

git restore '*.c'

git restore --staged hello.c    # same as using git reset

git restore --source=HEAD --staged --worktree hello.c   # same as git checkout

Reasoning
reference


Related TILs

Tagged:

TIL the core-js spam trolling

This entire repo is a hilarious rabbit-hole when you combine this with everyone complaining about the console spam that the dev added to it asking if anyone can find him a job. One of my favorite PRs

TIL How many Fucks there are in Linux

As of today, in the Linux Repo, there are comments that contains the following mentions: 1,651 for hacks, 2,863 for workarounds, and 4,102 for fixme

TIL reverting git

Two solutions. One is re-write history and hide your mistakes. The other is showing the history, to remind you of your mistakes.