Two Git commands you probably missed in your daily routine

Two Git commands you probably missed in your daily routine

Featured on Hashnode

Let's be frank, Git is/was not very intuitive. I gave up following the updates to the project because at least for me it was very boring, nothing was added to help the daily routine. But I'm glad I was wrong and recently(2.27 and beyond) two shortcuts were added to Git which are a good step in the right path:

git-switch

Ever asked yourself why we use git-checkout to create new branches AND move to different branches AND restore files? Now you have a semantic option to switch branches:

# switch to a existing branch
git switch mybranch

# switch to a *new* branch
git switch -c mynewbranch

# switch back to previous branch
git switch -

# by default, the command will error if you have uncommitted local changes, 
# you can do a three-way merge to the new branch in this case
git switch mybranch
error: You have local changes to 'main'; not switching branches.

git switch -m mybranch
Auto-merging main

git-restore

When you need to restore a file to N revisions back, you can use git-checkout, but since this is the same command that switches branches a newcomer might get confused. Now we have git-restore!

# Restore a file from 3 revisions back
git restore --source main~3 package.json

# Maybe you deleted all .png files in a folder, restore them
git restore '*.png'

# Restore all files in the current directory
git restore .

I will be paying more attention to Git news in case something cool like these commands is added to the workflow, stay tuned!