DEV Community

Cover image for Git Explained: Tips and Tricks
Milu
Milu

Posted on • Edited on

Git Explained: Tips and Tricks

As you learn Git and start getting more comfortable with the basics, you also want to learn ways to speed up your workflow. Git is such a powerful tool and there are numerous ways to achieve an intended action, however, some methods are quicker than others. Today we are going to discuss a few tips and tricks that will lead to a more productive way of interacting with version control on your daily development tasks.

Checkout your last branch

We already learned that in order to move in between branches we need to use the command git checkout [branch-name]. You can also quickly switch to the last branch you were in, without having to remember its name, by using the following command:

  git checkout - 
Enter fullscreen mode Exit fullscreen mode

Copy a file from another branch with one command

Sometimes you want to reference code that lives in another branch that hasn’t been merged into master yet. The obvious thing to do with the basic commands you have mastered so far is to stash your changes, checkout the other branch, copy or just look at the file contents you want to reference, and then switch back to your current branch. Let me show you a way to get this same intended action with just one command:

git checkout [branch-name] -- [path/to/file-name]
Enter fullscreen mode Exit fullscreen mode

Restore a deleted file

Have you ever cleaned up a project and then realized you actually wanted to keep one of the files you removed? (Sigh...). Here is a way to restore it in one command:

git checkout [deleting-commit]^ -- [path/to/file-name]
Enter fullscreen mode Exit fullscreen mode

Auto-correct Git Commands

Git can help you by auto-correcting a mistyped command too! By setting up the following configuration, if you type something like ‘chekcout’ instead of ‘checkout’, Git will automatically fix it for you.

git config --global help.autocorrect 1
Enter fullscreen mode Exit fullscreen mode

Undo committed changes to a single file

Every so often, you will commit some changes and then realize that you committed changes to a file by accident (Another sigh...). Here is one command that will allow you to remove the changes made to that file in your last commit:

git reset HEAD^ [file-name] git commit --amend
Enter fullscreen mode Exit fullscreen mode

Create your own shortcuts with Git aliases

Some Git commands can get pretty long and Git does not infer the command you are typing as you type it (silly Git!). The solution for this is to set up aliases for your most used commands using the following git config command:

git config --global alist.[shortcut-name] [command]
Enter fullscreen mode Exit fullscreen mode

The following command will allow you to use the shortcut git co [branch-name] instead of git checkout [branch-name] every time you want to switch in between branches:

git config --global alias.co checkout
Enter fullscreen mode Exit fullscreen mode

Here are some other useful aliases you can use to speed up your workflow:

git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

You can also add an alias for those commands that you wish git offered out of the box!
For instance, If you wish Git had an unstage command, you can create your own using the following:

git config --global alias.unstage ‘reset HEAD --’
Enter fullscreen mode Exit fullscreen mode

Now you only need to use git unstage [fileName] to unstage a specific file.

To list all the alias and configs you have setup so far:

git config --list
Enter fullscreen mode Exit fullscreen mode

I hope you give these shortcuts and configurations a try! I promise they will save you some precious time that can be wisely used to continue solving complex problems.

If you enjoy learning and growing your Git skills and understanding, make sure to check out my other previous Git posts!

Top comments (7)

Collapse
 
zulvkr profile image
zulvkr

These guides will be helpful as reference when I mess up my Git. Concise and cover a lot of cases.

Thank you.

Collapse
 
milu_franz profile image
Milu

Glad you found these tips and tricks helpful! Thank you for reading :)

Collapse
 
vlasales profile image
Vlastimil Pospichal

s/alist/alias/

Collapse
 
dansimiyu profile image
dan-simiyu

Thanks for sharing. Quite helpful

Collapse
 
milu_franz profile image
Milu

Thanks for reading! :)

Collapse
 
mayankjoshi profile image
mayank joshi

Very helpful.

Collapse
 
milu_franz profile image
Milu

Glad you found this post helpful! :)