What’s new with Git: Switch and restore

“When I see a door with a push sign, I pull it first to avoid conflict.” – Anonymous

For those of you who have been using Git for a while, you will rarely find new features about it. That is, it would be better if you excluded pipe commands that most of us probably don’t know about. To my surprise, I recently found about two additions to the list of advanced commands:

  • git restore
  • git switch

To understand why they are there, let’s visit our old friends firstgit checkout.

The Checkout thing

Git Checkout is one of the many reasons newcomers find Git confusing. That’s because its effect is context-dependent. Most people use it by switching active branches in their local repository. More specifically, switch the branch to which the HEAD points. For example, develop if you are on a branch can switch to the main branch:

git checkout develop
Copy the code

You can also have your HEAD pointer refer to a specific commit instead of a branch (achieving the so-called detached HEAD state) :

git checkout f8c540805b7e16753c65619ca3d7514178353f39
Copy the code

Where things get tricky is that if you provide a file as a parameter instead of a branch or commit, it will discard local changes to that file and revert it to the branch state. For example, if you check out the Develop branch and make some changes to the test.txt file, you can use the following command to restore the file to the latest commit of the branch:

git checkout -- test.txt
Copy the code

Crazy method

If you look at these two behaviors first, you might think that it doesn’t make any sense. Why do you want one command to do two different actions? Well, it’s a little more subtle than that. If we look at the Git documentation, we can see that the command has an extra parameter that is usually omitted:

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

What is
? It can mean a lot of different things, but the most common is to mean a commit hash or branch name. By default, it is treated as the current branch, but it can be any other branch or commit. So, for example, if you are in the Develop branch and want to change the test.txt file to the version in the main branch, you can do this:

git checkout main -- test.txt
Copy the code

With that in mind, maybe things start to make sense. Git checkout changes all of your files to the state in the corresponding revision when you provide only one branch or commit as an argument, but if you also specify a filename, it only changes the file’s state to match the specified revision.

New features

So even if it starts to make sense after reading the first few paragraphs, we have to admit that it’s still confusing, especially for newcomers. That’s why, in Git 2.23, two new commands were introduced to replace the old git checkout command (which is still available, but is a good place for new Git users to start). As you might expect, they basically implement one of the two behaviors described earlier, splitting Git Checkout into two types.

switch

This implements the behavior of Git Checkout when it runs only for branch names. Therefore, you can use it to switch between branches or commits.

git switch develop
Copy the code

While git Checkout allows you to switch to commit and transition to a separate HEAD state, git Switch does not allow this by default. You need to provide the -D logo:

git switch -d f8c540805b7e16753c65619ca3d7514178353f39
Copy the code

Another difference is that with Git checkout, you can create and switch to a new branch in a command using the -b flag: git checkout -b new_branch

You can do the same thing for new ones, but the flag is -c: git switch -c new_branch

restore

This implements git Checkout’s behavior when you run it against a file. As the name suggests, you can restore the file’s state to the specified Git revision (default: current branch)

git restore -- test.txt
Copy the code

conclusion

These methods are still marked as experimental, but for all intents and purposes they will remain, so I encourage everyone to start using them anyway, as they may become more meaningful, and it will also make Git a little less confusing for new users.

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

  • git switch
  • git restore