git switch git restore

Git Constructive Updates: Switch and Restore

by | 08.08.2021 | Changelog

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.

Let’s Start!

Checkout

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

Through 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

Through 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.

New Updates

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.

Switch

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

While with 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.

Restore

The ‘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.’

Conclusion

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.

Further Reads:

Git Restore

Git Switch

CommunityNew

The DevOps Awareness Program

Subscribe to the newsletter

Join 100+ cloud native ethusiasts

#wearep3r

Join the community Slack

Discuss all things Kubernetes, DevOps and Cloud Native

Related articles6

What’s new in Kuma v1.3.0?

What’s new in Kuma v1.3.0?

Kuma recently came with their new version of 1.3.0. It has come up with several bug fixes and new features with this update. In this article, we will see those fixes and new features which will make users have a great experience with the product. Buck up, and let’s...

What’s new in Istio v1.11.3?

What’s new in Istio v1.11.3?

Istio came with its new version recently. It is a minor release, but it contains some significant changes and fixes. In this article, we will have a detailed look at what version 1.11.3 brings to the table. So, without wasting any time. Let's start! What is Istio?...

What’s new in Traefik v2.5.3?

What’s new in Traefik v2.5.3?

Traefik came with a new version of 2.5.3. This version mainly focuses on bug fixing and adding documents. This article will cover all of those entirely. It is not a big update, so this article will be short and crisp. Buckle up for a ride. Let's start! What is...

What’s new in Prometheus v2.30?

What’s new in Prometheus v2.30?

Prometheus v2.30 was released a few days ago, and it is an exciting update. This update is not very inclined on adding new features to the ecosystem, but it brings several enhancements to configurability and resource usage efficiency. It also brings several bug fixes....

What’s new in Python-Tuf v0.18.0?

What’s new in Python-Tuf v0.18.0?

Python-Tuf v0.18.0 recently came, and it is quite a big update with major and minor changes. We will go through all of those changes, additions, fixes and removals in this document. Without further a due, let's start! What is Python-Tuf? The Update Framework (TUF) or...

What’s new in Envoyproxy v1.19.1?

What’s new in Envoyproxy v1.19.1?

Envoyproxy came with its new version a few days ago. Version 1.19.1 comes with very few updates. It provides a few minor behavioural changes and a few bug fixes to make the user experience smoother. In this article, we will cover all of the new changes. Let's start!...