For regular git users, there are few opportunities to discover anything new about Git. I recently noticed two new features added to the advanced command list:

  • git restore
  • git switch

To understand why these two new commands are added, let’s review the much-used Git checkout command.

Git checkout

Some newcomers to Git Checkout may feel confused when they first start using it. This is because the results it produces depend on the parameters passed in.

Let’s look at a few usage scenarios for Git Checkout

  • Switch the local branch, or more accurately, the branch to which the HEAD pointer points, for example, you can switch from the Develop branch to the main branch:

      git checkout main
    Copy the code
  • Toggle the HEAD pointer to a specific Commit

      git checkout f8c540805b7e16753c65619ca3d7514178353f39
    Copy the code
  • Restores the file to its last committed state

    If you enter a filename instead of a branch name or commit, it will discard your changes to the file and reset to the state of the last COMMIT version.

      git checkout test.text
    Copy the code

Looking at these behaviors, you might think that it doesn’t make any sense. Why do two different actions with one command? If we look at the Git documentation, we can see that the command has an extra parameter, which is usually ignored:

  git checkout <tree-ish> -- <pathspec>
Copy the code

What is? It can represent many different things, but the most common are commit values or branch names. By default, it is considered the current branch, but it can be any other branch or commit. For example, if you are in the Bland branch and want to change the test.txt file to the version from the main branch, you can do this:

 git checkout main -- test.txt
Copy the code

Now, maybe things start to make sense. When you provide a branch or commit parameter to Git Checkout, it changes all of your files to the state in the corresponding version, but if you also specify a filename, it only changes the state of the file to match the specified version.

New commands

Even now that we know the many uses of Git Checkout, we have to admit that it can still be confusing for novices. That’s why, in Git version 2.23, two new commands were introduced to replace the old Git checkout(it’s still available, but new git users are better off starting with these commands). They each implement one of git’s many behaviors.

Switch

You can use this command to switch between branches or commit

  git switch develop
Copy the code

Git checkout/commit/git checkout/git checkout/Git checkout/Git checkout/Git checkout/Git Checkout/Git Checkout/Git Checkout/Git Checkout/Git Checkout/Git Checkout/Git Checkout/Git Checkout/Git Checkout/Git Checkout

  git switch -d f8c540805b7e16753c65619ca3d7514178353f39
Copy the code

The -c flag is required to switch over and add a local branch

  git checkout -b new_branch

  git switch -c new_branch
Copy the code

Restore

You can restore the file’s state to the specified Git version (default: current branch)

  git restore -- test.txt
Copy the code

conclusion

Compared to Git Checkout, these two commands are much cleaner.

More details about these two commands can be found in the Git documentation:

  • git switch

  • git restore

More front-end and back-end content