As developers, one of the best pieces of advice is to get comfortable using Git. Linus Torvalds, the creator of Linux, also happens to be the guy who made Git. He is much smarter than all of us, and if he knew that if he made it too easy, it would make us weak. Instead of that he wished us the satisfaction of overcoming the sometimes confusing, albeit powerful, commands of Git. This is what I seek out to help with in this article
Ask yourself: "could I be using Git more proficiently?". The answer is, most likely, a resounding YES!
By the end of this article we will have gone through a bunch of different tips and tricks to help you on your way to become a better developer and being one step closer to that 6-digit salary.
Combine ADD and COMMIT
To save a snapshot of your code, you use add followed by a commit message.
git add .
git commit -m "New line added"
But, there's an actually a better way to get the job done. You can go straight to commit by using the -am flag.
git commit -am "An easy way!"
This will automatically add all the files in the current working directory.Important note: "git commit -a" will only automatically add changes to files git is already tracking. If you create a new file, you still need to "git add" it.
Aliases
There's actually a more concise way to get the job done. The command git config provides a way to create aliases which are commonly used to shorten an existing command or create your own new custom commands.
Let's look at an example:
git config --global alias.ac "commit -am"
git ac "noice!"
We made an alias called ac that runs the add and commit command with just two letters. This allows us to run things faster, but sometimes going fast leads to mistakes.
Amend
What if you made a typo in your last commit message: git ac "noice!"?
Instead of resetting and committing a new commit, the --amend flag followed by a new message will simply update the latest commit.
git commit --amend -m "nice!"
Or maybe you forgot to include or stage a couple of files with your last commit. You can also update your last commit with new files by using the --amend flag.
And if you want to keep the same commit message, add the --no-edit flag as well.
git add .
git commit --amend --no-edit
Force Push โ ๏ธ
Keep in mind, the above command only works when you haven't already pushed your code to a remote repository. If you want all your co-workers to hate you, you can do a git push with the --force flag. This will completely disregard all other commits on the current branch and overwrite them with your local commits.
If you don't want your fellow developers to hate you, use --force-with-lease, which will only allow you to push the code if there are no conficting changes with the current remote branch.
git push origin master --force
This will overwrite the remote commit with the state of your local code. However, if there are commits on the remote branch that you don't have, you'll lose them forever.
Revert
But what happens if you push some code to a remote repository and then realize it's a complete garbage and never should've been there in the first place. The git revert command allows you to take one commit and go back to the state that was there previously.
git revert better-days
git log --oneline
We will use the revert command with the hash id of the latest commit.
git revert b4f4098
It's kind of like an undo but doesn't remove the original commit from the history. Instead, it just goes back to the original state.
And, that's much easier ๐
Codespaces
Another case is, you may need to work on a repository but not have access to your local machine. If you are in your Grandma's house without your laptop, you can use any computer that has a web browser. Go to the GitHub and find the repository that you want to want to work on. Then hit the period key on your keyboard.
And like magic, it pulls up a browser-based version of VS Code where you can make edits, submit pull requests and do almost anything else you could do locally, well except for run the terminal. If you do need to run terminal commands, you can setup a GitHub code space in the cloud which will give you the full power of VS Code and is, likely, much faster than your Grandma's computer.
Stash
Let's switch gears to Git stash. Have you ever spent time working on some changes that almost work, but they can't really be committed yet? git stash will remove the changes from your current working directory and save them for later use without committing them to the repo.
The simple way to use it is,
git stash
git stash pop
when you're ready to add those changes back into your code. But if you use the command a lot, you can use git stash save followed by a name to reference it later.
git stash save coolstuff
Then when you are ready to work on it again, use git stash list to find it and then git stash apply with the corresponding index to use it.
git stash list
git stash apply 0
Now if you want to use a "stash" at Grandma's house, you have now a solution. You can use a GitHub codespace in which case, your stashes would be saved in a file. That's pretty cool!
PC Master Branch
Now, I have a public service announcement for developers in the modern era. Historically, the default branch in git is called the "master" branch, which is now referred to as "main".
To change it, use git branch followed by the -M flag to rename it to main, or maybe get creative and invent your own name.
git branch -M mucho
Pretty Logs
Another command, you might be probably familiar with, is git log to view a history of commits. The problem with this command is that the output is harder and harder to read as your project grows in complexity.
To make the output more readable, add the options of --graph, --oneline and --decorate.
git log --graph --oneline --decorate
You can now see a more concise breakdown and how different branches connect together.
But if you're looking at the git log, there's likely a commit in there that's currently breaking your app ;)
Searching Logs ๐
The log command can also be used to search for specific changes in the code. For example, you can search for the text "README file added by Nikolai Waerpen" as follows.
git log -S "README file added by Nikolai Waerpen"
This command returns the commit where the file is added in the Text directory.
Bisect
Git bisect allows you to start from a commit that is known to have a bug, likely the most recent commit. But if you knew that the app was working a few hours ago, you can point bisect to the last working commit.
git bisect start
Then, it performs a binary search to walk you through each commit in between.
If the commit looks good, type bisect good to move on to the next commit.
git bisect bad
git bisect good 5b010ef
git bisect bad
Eventually, you will find the bad one and know exactly which code needs to be fixed.
Autosquash
Another advanced git technique that every developer should know is how to squash their commits.
Imagine, you are working on a feature branch that has three different commits. And you are ready to merge it into the main branch.
But all these commit messages are kind of pointless, and it would be better if it was just one single commit.
We can do that from our feature branch by running git rebase with the --interactive option for the main branch.
git rebase master --interactive
This will pull up a file with a list of commits on this branch.
If we want to use a commit, we just use the pick command. We can also change a message using reword command. Or we can combine or squash everything into the original commit using squash command.
Go ahead, save the file and close it. Git will pull up another file prompting you to update the commit message, which by default will be a combination of all the messages that you just squashed.
And if you don't like all the messages combined, you can use fixup instead of squash when doing the rebase. To be even more productive, you can also use --fixup and --squash flags when making commits on your branch.
git branch --fixup fb2f677
git command --squash fc2f55
On doing this, it tells git in advance that you want to squash them. So when you go to do a rebase with the --autosquash command, it can handle all the squashing automatically.
git rebase -i --autosquash
Hooks ๐ช
Now, if you maintain a repo, one tool that can be very helpful is "Git hooks."
git commit -m "It is fixed"
Whenever you perform an operation with git like a commit for example, it creates an event. And hooks allow you to run some code either before or after that event happens. If you look in the hidden git directory, you will see a directory called "hooks". And inside it, you will find a bunch of different scripts that can be configured to run when different events in git happen.
If you happen to be a JavaScript/TypeScript developer, there's a package called "husky" that makes it much easier to configure git hooks.
For example, You might install it with npm, then create a script that will validate or link your code before each commit. And that can help improve your overall code quality.
Destroy Things ๐ฅ
To wrap things up, let's talk about deleting things.
Let's imagine you have a remote repository on GitHub than a local version on your machine that you have been making changes to. But, things haven't been going too well.
And you just want to go back to the original state in the remote repo.
git fetch origin
git reset --hard origin/master
First, we do git fetch to grab the latest code in the remote repo. Then, use reset with the --hard flag to override your local code with the remote code.
- Be careful, your local changes will be lost forever.
But you might still be left with some random untracked files or build artifacts here and there. Use the git clean -df command to remove those files as well. If you want to get rid of it altogether, maybe you want to try out Apache subversion to change things up a bit. All you have to do is, delete that hidden git folder, and you are on your own again.
Checkout to Last
Oh! There's one other tip that comes in really handy. If you recently switched out of a branch and forgot its name, you can use:
git checkout -
It takes you directly back to the previous branch that you were working on.
Bonus tip
If you don't want to rely on the cloud you can use git init --bare to create local repos without a working directory, you can then sync all your projects between devices using syncthing.
Conclusion
Hopefully, a few of these commands can help you in your Git journey. There are many other amazing Git commands. Explore and Learn!
Source of the article: Video by the ever amazing Fireship