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

Join the Community

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

More stories from our blog

Linkerd: Looming on Service Meshes

Linkerd: Looming on Service Meshes

Microservices and service meshes have become a staple of the industry as companies realize the full potential of creating an independent architecture that allows for easier scale up, agile development, resilience and streamlined deployment. Many of these applications...

What’s new in Flux v0.17.0?

What’s new in Flux v0.17.0?

Flux2 came with its new update a while ago, and it is sheer exciting for the users because it brought a lot of new features. It also made a lot of new enhancements and updates. We will take a look at the entire catalogue in this article. So, without further a due,...

What’s new in Portainer v2.7.0 BE?

What’s new in Portainer v2.7.0 BE?

A few days ago, Portainer Business Edition came up with their new update. It is quite a massive update with many new features, bug fixes, enhancements and much more. In this article, we will see all of those in a nutshell. Let's start What is Portainer? Portainer is...

DVC (Git For Data): A Complete Intro

DVC (Git For Data): A Complete Intro

As a data scientist or ML engineer, have you ever faced the inconvenience of experimenting with the model? When we train the model, the model file is generated. Now, if you want to experiment with some different parameters or data, generally people rename the existing...

Recap of the Cloud Native Meetup Saar #3

Recap of the Cloud Native Meetup Saar #3

We are looking back on a very successful third edition of our Cloud Native Meetup Saar #3! Togetherer with our co-host anynines, we enjoyed a fun afternoon filled with great speakers, intriguing topics and thoughtful conversations! We welcomed a total of three...

Portainer Ambassador Series ft. Fabian Peter

Portainer Ambassador Series ft. Fabian Peter

Portainer arranged a fun and informative discussion through a one-hour special named “Ambassador Series” on 1st July 2021. It was pretty amazing to see Savannah Peterson as the host and two other guests. One is our very own CEO of p3r.one, Fabian Peter and the other...

What’s new in Longhorn v1.2.0?

What’s new in Longhorn v1.2.0?

Longhorn came with their new update. It is full of surprises. We will peel off one by one to see all the latest updates, features, bug fixes and much more. This one is a much-awaited update, and we will see all of it in a moment. So, without further a due, let's...

Kubernetes Stateful Friend: What’s more to etcd?

Kubernetes Stateful Friend: What’s more to etcd?

The Kubernetes control plane consists of various components, and one of such components is etcd. Anyone starting to learn k8s come across it and memorizes quickly that it’s a key-value pair for Kubernetes with persistence store. But, what’s more to it? Why do we need...

What’s New in Flux 1.24.0?

What’s New in Flux 1.24.0?

Flux 1.24 is out this month with couple of updates and Important notices. Let’s get around what are the updates in the new release. But, first, let’s do a quick intro on Flux. What is Flux? Flux is a tool that checks to see if the status of a cluster matches the git...

Event Driven Architecture Demystified (For Pros)

Event Driven Architecture Demystified (For Pros)

Event-Driven Architecture or EDA is talked about with pride inside any organization. But, through last few months, I have noticed a trend that the definition of EDA is not consistent across people and organizations. It’s vague. EDA is something where you have events...

Interested in what we do? Looking for help? Wanna talk about software strategy?