DEV Community

Cover image for Have You Noticed Two New Git Commands from 2.23?
0xkoji
0xkoji

Posted on

Have You Noticed Two New Git Commands from 2.23?

I just noticed them on gatsbyjs/gatsby-ja repo.
There is a guide for contribution to the repo.

git clone https://github.com/<yourname>/gatsby-ja
cd gatsby-ja
git switch -c docs/accessibility-statement(docs/accessibility-statement.mdを翻訳する場合)
Enter fullscreen mode Exit fullscreen mode

Then I checked a couple of articles and documents of switch then noticed that there is another new command, restore from 2.23.

switch

https://git-scm.com/docs/git-switch/2.23.0

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

Optionally a new branch could be created with either -c, -C, automatically from a remote branch of same name (see --guess), or detach the working tree from any branch with --detach, along with switching.

Switching branches does not require a clean index and working tree (i.e. no differences compared to HEAD). The operation is aborted however if the operation leads to loss of local changes, unless told otherwise with --discard-changes or --merge.

$ git checkout -b feature/add-new-func
$ git switch -c feature/add-new-func

$ git checkout master
$ git switch master
Enter fullscreen mode Exit fullscreen mode

To me, the interesting option is -m.

-m, --merge perform a 3-way merge with the new branch
After this three-way merge, the local modifications are not registered in your index file, so git diff would show you what changes you made since the tip of the new branch.

restore

https://git-scm.com/docs/git-restore/2.23.0

Restore specified paths in the working tree with some contents from a restore source. If a path is tracked but does not exist in the restore source, it will be removed to match the source.

The command can also be used to restore the content in the index with --staged, or restore both the working tree and the index with --staged --worktree.

By default, the restore sources for working tree and the index are the index and HEAD respectively. --source could be used to specify a commit as the restore source.

See "Reset, restore and revert" in git[1] for the differences between the three commands.

Maybe, restore seems more intuitive.

# Restore all files
$ git checkout .
$ git restore .
Enter fullscreen mode Exit fullscreen mode

According to the Git pages, both of them are experimental, so their behavior may change.

I didn't check the Git page when it was updated. So, I just thought I would need to check CLI tools docs sometimes.

Top comments (0)