Some Useful Git Tips and Commands

This post will take 2 minutes to read.

Git can be over­whelm­ing when you’re first start­ing out, but it’s an incred­ibly power­ful tool that should be a part of every web developer­’s work­flow. In this post, I’ll out­line some resources that will help you bet­ter under­stand and use Git.

Getting to know Git

I’m a fan of learn­ing by doing, so I would def­in­itely recom­mend set­ting Git up on your com­puter and cre­at­ing a test repos­it­ory to exper­i­ment with (if you’re not already using it). As some of the com­mands are quite tech­nic­al, it’s also worth check­ing out the tutori­als that have been released by Code School (par­tic­u­larly good) and Codecademy.

Commonly Used Git Commands

As Git com­mands aren’t always that intu­it­ive, I’ve cre­ated a list of the com­mands that I find myself using a reas­on­able amount. That way I can simply ref­er­ence this list instead of hav­ing to search for the cor­rect way to run a command.

View Commands on GitHub

Setting Up Aliases

Aliases are a big time saver for me when using Git. They allow me to quickly enter com­mands without hav­ing to type them fully. They can be imple­men­ted no mat­ter the oper­at­ing sys­tem you’re using, but if your com­mand-line inter­face sup­ports ali­ases then you can cre­ate even more power­ful ali­as func­tions. Here is an art­icle out­lining how to set up ali­ases, and the ali­ases I use can be viewed below.

# Git Aliases

alias get='git '
alias gut='git '
alias got='git '
alias g='git '
alias gi='git init '
alias gs='git status -sb '
alias ga='git add '
alias gc='git commit -m '
alias gac='ga -A && gc '
alias gp='git push '
alias gpo='git push origin '
alias gpom='git push origin master'
alias gr='git rm -r '
alias gb='git checkout -b '
alias gc-='git checkout - '
alias gd='git diff '

function gcl {
  git clone ssh://[email protected]/$1/$2.git;

function gacp {
  git add -A;
  git commit -m "$1";
  git push origin $2;

Note: Use the func­tions by writ­ing the func­tion name fol­lowed by the rel­ev­ant text for $1 and $2 (e.g. gcl joshnh Git-Commands)

View Aliases on GitHub

Some Usage Tips

Finally, here are some usage tips that will help min­im­ise any issues that might arise:

  • Commit your changes often and accur­ately describe what has changed. This ensures that if you’re look­ing for a spe­cif­ic com­mit in the future you’ll be able to eas­ily find it. The way I do this is to ima­gine I’m writ­ing my com­mit as someone else; how much detail would I want them to include so I can eas­ily tell what has changed?
  • Make use of branches when work­ing on spe­cif­ic fea­tures. This is use­ful when work­ing with oth­ers and helps you make changes without impact­ing exist­ing code.
  • Pull reg­u­larly, espe­cially when work­ing on a pro­ject with oth­er people. This will help min­im­ise any merge issues that might arise when you merge a branch you’ve been work­ing on with a mas­ter branch.
  • If you get stuck when using Git via the com­mand line, make use of git [command] -h. Git’s nat­ive help is often enough to save you hav­ing to search online for a solution.
  • If you don’t want all files in a pro­ject to be tracked using ver­sion con­trol then make use of Git’s .gitignore file.
  • When work­ing with oth­ers, use git blame [filename] to see who authored each line (also when it was edited and in which commit).

Tweet this