Tip One: Run git status frequently
A common mistake that Git users make is to do a bunch of work and commit while on the wrong branch. Because I am not a Git expert and I'm never sure how to recover from this mistake, I run git status very frequently. I always run it as soon as I've opened the command line and navigated to my repository, so I won't be surprised by what branch I'm on.
Running git status is also a good way to find out what files have been changed in your branch. Sometimes we make a change to a file for debugging purposes and we forget to change it back. If you run git status, it will alert you that a file you didn't want to change has been altered.
Tip Two: Pull from the master branch before you do anything
Before you start changing code in your branch, you'll want to make sure that your branch has the very latest code in it. Otherwise, you may be updating code that doesn't exist any more, or you may be duplicating work that has already been done. By making sure that you have the latest code, you will also avoid creating a merge conflict, where your branch and the master branch have differences that the version control system doesn't know how to handle.
Once you have pulled from the master branch, remember to switch to your own branch! If you are running git status frequently, you'll notice if you've forgotten to make the switch.
Tip Three: Add all of your changed files at once
If you have changed a number of files, you'll find it tedious to type
git add <insert long file name here> over and over again. A quick way to add all of your changed files at once is to use git add -A. Just be sure when using this that you want to add all of your files. I don't know of a command that will let you add all the files except one or two. If you know a command for this, please comment on this post and share it with everyone!
Tip Four: Name your commits something helpful
When you commit your files, adding a commit message is optional, but most companies expect their employees to do it. Make it easier on yourself and everyone else by naming your commits something that will make sense later. "One line code change" is not a very helpful commit message. "Adding test for new contact info endpoint" provides much more detail.
Tip Five: View your git logs in a single line
It can be hard to remember what you've done in Git, because there's no UI to show you. This is where git log is helpful. The log in its full version will show you the last several commits made, who made them, and the date and time they were made. I find it's easier to read the logs when they are condensed to a single line; to do this, type git log --pretty=oneline. To exit the log, type q.
Tip Six: View the diff of your files before you do a pull request
If you are running git status frequently, you probably won't commit and push any files that you didn't mean to push. But it's still possible to accidentally commit code you didn't mean to commit in a file that has the changes you want. So before you do a pull request and ask someone to review your code, view the "diff" of your files, which simply means looking in GitHub and comparing the files in your branch with the files in the master branch to see which lines were changed. Make sure that there are no code changes in the file that you didn't want to commit, such as commented-out code or debugging statements.
If you find that you've accidentally pushed something that you didn't mean to, simply change the file to what you want it to be and add, commit, and push it again.
Hopefully you will find these six tips helpful as you work with Git! In next week's post, I'll talk about how to handle the dreaded merge conflict.