Git Switch and Restore commands came in version 2.23. In this article, we will go through all the new commands that are here to make our life a bit easier. To understand more about the new Switch and Restore, we will look at “Checkout” first.
git checkout is one of the reasons which makes some of the newcomers find git confusing. And it happens because its effect is dependent on the context.
Most developers use it to switch the active branch in their local repository. More precisely, to give a change the branch to which HEAD points. For example, you can switch to the developed branch if you are on the main branch. Checkout consists of the following commands:
git checkout develop
git checkout develop, we can make our HEAD pointer reference a specific commit instead of a branch( which is reaching the so-called detached HEAD state). The git checkout is where things get tricky. It shows that if we provide a file as an argument instead of a branch or commit, it will clearly discard your local changes to that file and restore it to the branch state. For example, if we checked out the develop branch and made some changes to the test.txt file, we can restore the file as it is in the latest commit of our branch with
git checkout --test.txt.
git checkout -- test.txt
git checkout -- text.txt, if we first look at these two behaviours, we might think that it doesn’t make any sense because it has one command to do two different actions. Well, we can see that things are a little more subtle than that. Looking at the git documentation, we see that the command has an extra argument usually omitted through
git checkout <tree-ish> -- <pathspec>.
git checkout <tree-ish> -- <pathspec>
Now, we have to see what is <tree-ish>? It can mean many different things, but it usually means a commit hash or a branch name. By default, we can see that it takes the committed hash to the current branch, but it can also be any other branch or commit. So, if we are in the develop branch but want to change the test.txt file to be that version from the main branch, we can do it by giving the command
git checkout main -- test.txt.
git checkout main -- test.txt
Through the command
git checkout main -- test.txt, when we provide a branch or commit as an argument for git checkout, then we see that it will change all your files to their state in the corresponding revision. But if we also specify a filename, it will only change the form of that file to match the selected revision.
So even if we may start to make sense of things after learning about git checkout, we have to admit that it is still very confusing, especially for new developers. So, in version 2.23 of git, we can see the introduction of two new commands to replace the old git checkout command(
git checkout is also available, but people who are new to git should start with these preferably). As we would expect, they each implement one of the two behaviours described previously, splitting
git checkout in two.
We can see that this one implements the behaviour of
git checkout when running it only against a branch name. So we can use it to switch between branches or commits. We will also take a look at its extended commands:
git switch develop
git checkout, we can switch to a commit and transition into a detached HEAD state. By default, the
git switch does not allow that. We need to provide the
-d flag, with which the command will look like
git switch -d. Another difference is that with
git checkout, we can create and switch to the new branch in one command using the
-b flag, such as
git checkout -b new_branch. We can do the same with the new one, but the flag is
-c. So, the command will be
git switch -c new_branch.
git restore‘ command implements the behaviour of git when running it against a file. As the name suggests, we can restore the state of a file to a specified git revision(the current branch by default). The command with a sample file ‘test.txt’ looks like ‘
git restore -- test.txt.’
So, these are the minor two updates that came along with the new version of git. These methods are still marked experimental, but they are here to stay, so by all means, we encourage everyone to start using them since they will probably make a lot more sense and also, it will make the use of git just a little less confusing to new users. Try it out now by downloading git here.