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:
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:
To check for all alias set use this command:
To unset an alias use this command:
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:
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:
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:
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.
To add the changes back use `git stash pop`:
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`
To see what index your saved stash is, so you can add the changes back use this:
In this case our stash is saved at index `0` to reapply your changes use `stash` like this:
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
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:
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.
You might still be left with some untracked files after resetting your local branch to clean these up:
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.
This will reveal something like this:
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 😃
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.
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