Since Git was born in 2005, core commands such as clone, pull, push, merge, checkout, and commit have existed and supported daily development work. With the evolution of version control requirements, Git has been continuously iterated and updated, introducing many enhanced features and new commands. This article will focus on seven recently added Git commands and explore how they further improve work efficiency!

git switch: Safely Switch Branches

The git switch command was introduced in Git version 2.23.0 to address the problem of the git checkout command having too many responsibilities and to make Git commands more intuitive and easier to understand.

Before Git 2.23, git checkout was used both for switching branches and restoring file contents, which was easily confusing. By splitting the functions of git checkout, the Git team created two new, more specialized commands:

  • git switch: Specifically used for switching between branches.
  • git restore: Specifically used for restoring file contents.

Using git switch to switch branches is very simple. Here is the basic usage:

# Switch to an existing branch
$ git switch <branch-name>
# Create and switch to a new branch
$ git switch -c <new-branch-name>
# Create and track a local branch from a remote repository
$ git switch -c <new-branch-name> --track <remote>/<branch-name>
# Return to the previous branch
$ git switch -

Note: If you encounter the error message 'switch' is not a git command, it may be because your Git version is lower than 2.23.0. You can check the Git version by running git --version and upgrade to the latest version to use these new features.

git restore: Safely Undo Changes

The git restore command was also introduced in Git version 2.23.0 and is specifically used to restore the contents of files in the working directory.

git restore is mainly used to undo changes in the working directory. It can be used to discard uncommitted working tree modifications, restore deleted files, or reset files to a previous commit state. Here is the basic usage of git restore:

# Restore a file in the working directory to the state of the last commit
$ git restore <file>
# Restore a file from a specified commit to the working directory
$ git restore --source=<commit> <file>
# Unstage changes (similar to git reset HEAD <file>)
$ git restore --staged <file>
# Restore all files to the state of a specified commit
$ git restore --source=<commit>.
# Restore all deleted files
$ git restore -w -- *
# Discard changes in the staging area and the working directory (i.e., restore to the state of a specified commit)
$ git restore --staged --worktree <file>

When using git restore, you can optionally specify --staged to affect the staging area or --worktree to affect the working directory. If both options are specified, both the staging area and the working directory will be affected.

git restore is a relatively safe operation because it does not change the branch history. It only affects the working directory and/or the staging area.

git worktree: Work on Multiple Branches Simultaneously

The git worktree command was introduced in Git version 2.5. It allows you to create multiple working directories (worktrees) in the same repository, and each working directory can check out a different branch or commit. This provides developers with the ability to handle multiple tasks simultaneously, such as developing and testing on different branches without having to switch branches back and forth.

Here is the basic usage of git worktree:

# Add a new working directory and check out a specified branch
$ git worktree add <path> [<branch>]
# List all working directories
$ git worktree list
# Remove a working directory (you must first ensure that there are no uncommitted changes in the directory)
$ git worktree remove <path>
# Move a working directory to a new location
$ git worktree move <current-path> <new-path>

For example, if you want to add a new working directory to check out a branch named feature-branch, you can do the following:

$ git worktree add../my-feature-worktree feature-branch

This will create a new working directory under the ../my-feature-worktree directory and check out the feature-branch branch.

git sparse-checkout: Efficiently Handle Large Repositories

The git sparse-checkout was introduced in Git version 2.25.0. This function is a significant improvement over the previously existing sparse checkout mechanism. With git sparse-checkout, developers can more efficiently clone large repositories by checking out only some files or directories instead of the entire project.

To enable sparse-checkout, you first need to set the repository to use the sparse checkout mode:

# Enable sparse-checkout mode
$ git sparse-checkout init
# Set the patterns or paths you want to include
$ git sparse-checkout set <pattern>...

For example, if you only want to check out the files in the src directory and its subdirectories, you can do the following:

$ git sparse-checkout set src/

If you want to add multiple patterns or paths, you can list all the paths after the set command or call the command multiple times.

In addition to the set command, you can also use add and list to manage the sparse checkout mode:

# Add additional paths to the sparse checkout mode
$ git sparse-checkout add <pattern>...
# List the existing sparse checkout patterns
$ git sparse-checkout list

If you no longer need the sparse checkout mode, you can disable it and restore the full checkout state by using the following command:

# Disable sparse-checkout mode and restore full checkout
$ git sparse-checkout disable

If you want to keep reading, quickly click on the link and check it out!

https://en.kelen.cc/posts/7-useful-git-commands-you-absolutely-must-know#git sparse-checkout: Efficiently Handle Large Repositories

Author Of article : kelen.cc Read full article