fbpx

10 GitHub Tricks You Should Know

OMOTOLA SHOGUNLE

ARTICLE SUMMARY

In this 5th and final guide from Omotola’s How to Get Good at Git & GitHub - she shares with us 10 GitHub Tricks you should know if you want to be considered a pro.

If you’re aiming to become a certified coder, you should know the following git tricks.

These tricks have been helpful in improving my productivity, and hopefully they’ll help you too.

1. Add and Commit Changes in one Command 

Save keystrokes by adding and committing changes using the `-am` flag like this:

GitHub Tricks

2. Use global config to add Alias for Git commands 

Alias are used to set alternative names for commands etc. In Git, we can use this command to create an alias for any command. For example, to create an alias for checking the status of files, I do this:

GitHub Tricks

To check for all alias set use this command:

GitHub Tricks

To unset an alias use this command:

GitHub Tricks

3. Amend a GitHub Commit Message

Ever make a mistake in your commit message? Instead of resetting your commit to add a new commit message you can just amend the commit message like this:

GitHub Tricks

You can also add more files to an existing commit using the `–amend` flag. If you like to maintain the commit message, use the `–no-edit` flag. For example:

GitHub Tricks

Note: this only works with commits you haven’t pushed yet.

4. Git log

Probably one of my favourite commands, this shows a history of commits made in your branch. It shows the commit hash and commit message as well as other information. I like to use the `–oneline` flag which makes the log easier to read. You can add other flags like `–graph` and `–decorate` together to make it even more interactive and visually appealing:

GitHub Tricks

GitHub Tricks

5. Save changes for Later

Use the `stash` command if you have changes you are not ready to commit yet. It simply removes your changes from your current working directory and saves them for later use without committing it to your repository.

GitHub Tricks

To add the changes back use `git stash pop`:

GitHub Tricks

If you like to reference your stash with a name, do this, so you can reference the particular stash you want to pop. In this case we saved it as `fixforapp`

GitHub Tricks

To see what index your saved stash is, so you can add the changes back use this: 

GitHub Tricks

In this case our stash is saved at index `0` to reapply your changes use `stash` like this:

GitHub Tricks

6. How to stop Git From Tracking your Local Project

If you want to stop tracking individual files add the file path in your .gitignore file. Git automatically stops tracking all files added to the .gitignore file.
However, if you no longer want git to track your local folder all you have to do is delete the .git folder. Using this command

GitHub Tricks

7. Forget your previous branch name?

A quick and easy command for when you forget the name of the last branch you were on is:

GitHub Tricks

8. Sync with remote, overwrite local changes 

If you no longer want your local changes and want to be in sync with your upstream repository you need to take 3 steps. 

First fetch all the remote changes using the `fetch` command, then reset your committed changes in your local repository and finally do a merge.

GitHub Tricks

You might still be left with some untracked files after resetting your local branch to clean these up:

GitHub Tricks

9. Open VsCode in your Web Browser

Like magic, if you go on GitHub and open any of your repositories, click on the period key on your keyboard and VsCode will open with your project in the browser. This is so useful especially if you do not want to clone a repo to your computer before you can play around with it. What a great feature.

10. See author of a piece of code

Curious about who wrote some code you are finding difficult to understand? Use git blame to see the author and timestamp for the file.

GitHub Tricks

This will reveal something like this:

GitHub Tricks

PS: In this case because I am the only one editing this file currently it shows as me, but it would be a heartbreaking situation if you were the author, and you didn’t understand your own code :). Not ideal! From one dev to another,  comment your code for sanity!

Try it out and see 😃

Conclusion

Git is a powerful tool, and is full of neat tricks. Of course there are more tricks you can learn, but my hope is that this series of articles gets the ball rolling for you. You can read the git documentation to find out more.

If you have completed this series, congratulations! You have successfully become better at Git! If at least one person feels more confident with this tool, my job here is done. Are you just joining us here? Feel free to have a look at the other tutorials below. 

Now as promised: the GET GOOD AT GIT CHEATSHEET, 100% FREE. Click here to download it.

Reminders 

Create a bookmark folder in your bookmark bar. Left-Click on the Bookmark Bar -> Click Add Folder -> Name it `Git Series` -> Add this article to the folder

You can always message me on @mycodinghabits on Instagram or email me mycodinghabits@gmail.com if you have questions.

RELATED ARTICLES

SheCanCode have put together some tips and tricks to get you started and to help you reach your career potential in the New Year.
Join us as Sonny Patel, CTFO at Socotra, delves into practical and actionable advice for women aspiring to reach top executive positions like Chief Product...
Amy Jam, a Senior Technical Program Manager with over 20 years of experience in tech and defence, shares her journey as often the only woman...
Join us for an insightful episode as we delve into the world of mentoring at Checkatrade. Our guests, Jess and Rossana, bring a unique perspective...