monday, april 13, 2020
If you’ve been using Git for a while you’re probably used to the ubiquitous
git checkout command, which is somewhat overloaded in what it’s doing.
You can use
checkout to switch branches, create branches, update the working tree to a past commit, wiping working tree changes, and a few more things.
Put simply, this command has too many responsibilities, and should be refactored.
This is what happened in Git
2.23.0: We can now use two other, more specialized commands,
git switch and
git switch is used to managed branches, that is creating a branch or switching to a branch.
It is complementary with
git branch, which lists, deletes, and (also) creates branches.
git switch as follows:
# switches to master branch git switch master # switches to branch feature-123 git switch feature-123 # switches to last branch, here master git switch - # creates a new branch feature-234 git switch -c feature-234
git restore is used to restore working tree files, in the same way you would have used
git checkout to checkout or restore a certain working tree state.
Here are some examples for
echo "modified" >> file.txt # restores all files in the current directory git restore . # working tree clean echo "modified" >> file.txt git add file.txt git restore . # no changes # restores staged changes from the index git restore --staged . # working tree modified git restore . # working tree clean # checks out a single file from a previous commit git restore -s a0b1c2d file.txt
This post has been reposted from my newsletter issue 041.
You’re interested in more productivity and command line tips? Found the post useful? Then you might enjoy my Developer Productivity Masterclass.