Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 6, 2020 01:41 pm GMT

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 - 

Copy a file from another branch in one command

Sometimes you want to reference code that lives in another branch that hasnt 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]

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]

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

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

Setup 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]

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

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

git config --global alias.br branchgit config --global alias.ci commitgit config --global alias.st status

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

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

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!


Original Link: https://dev.to/milu_franz/git-explained-tips-and-tricks-4299

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To