Git Command Cheatsheet

Git Command Cheatsheet

My Top 7 Git Commands and How I Use Them

Introduction

This week I took a deep dive into Swyx's philosophy of Learn in Public. I first heard about this idea on Episode 125 of The Scrimba Podcast. This episode focused on the topic of developing a personal brand and featured clips from previous episodes. Including Episode 18 which featured an in-depth interview with Swyx and his book, The Coding Career Handbook.

I listened to both episodes and was so inspired that I purchased the book, joined the community, and (re)started my dailyDevLog journal (a personal journal I use to track my daily progress on projects and what I’m learning).

As a former educator, I fully understand how beneficial writing-to-learn can be. As the quote often attributed to Albert Einstein — but more likely came from Lord Rutherford of Nelson — says, “if you can’t explain it simply, then you don’t understand it well enough”.

That’s always been my goal with writing (both online and offline), but I haven’t been good about writing regularly since I completed my first round of the #100DaysOfCode challenge. My blog posts have been sporadic and I know that regular reflection is what helps our brains process and retain information.

I hope that the resources and inspiration I’ve found in these podcast episodes and in Swyx’s book will help me pursue that goal with renewed energy and commitment.

So, without further ado …

I present to you my latest installment toward that goal. Here is a quick list of the most common git commands I use when working on a project. In addition to this blog post, I’ve also made a GitHub repo, aptly titled LearnInPublic, in hopes of building a collection of things I’ve found useful along my programming journey.

git init

git init

What it does:

  • Initializes git repo in from CLI

Useful when:

  • You start your project! Git will track changes within your repo, but first you've got to initialize it.

git clone <URL>

git clone <URL>

What it does:

  • Clones a remote repo (i.e., creates a copy on your local machine)

Useful when:

  • Cloning an open-source repo to your local machine so you can make contributions
  • Cloning your own remote repo to your local machine

git status

git status

What it does:

  • Checks the status of the repo
    • Are there any unstaged files?
    • Are files staged and ready to commit?

Useful when:

  • You can't remember if you're last set of changes were committed or not

git remote add origin <URL>

git remote add origin <URL>

What it does:

  • Adds a remote origin to a local repo

Useful when:

  • You've created a local repo and need to connect it to a remote repo
    • In other words, this tells git where to send the files when you use the git push command

git add <FILENAME>

git add <FILENAME>

What it does:

  • Adds untracked changes in file to staging (i.e., they're ready to commit to the repo)

Useful when:

  • You've made changes to a project and need to stage those changes before you commit the changes to the repo
    • In other words, you're happy with the changes and ready to "store" them

Variant

git add .

What it does:

  • Adds all files with untracked changes to staging

Useful when:

  • You've made mutliple changes to multiple changes. With git add you need to list individual file names after the command so this is useful when you've made lots changes.

CAUTION: Including all files may also include hidden files (e.g., .DS_Store) within the directory where your repo is located. These kinds of files often contain system metadata, however you may not want them in your repo repository, especially if it's a public one.

Further, you may be working on a project that contains access keys. If you add these files and commit them, then push them to a public repo they become available to the public. This could be bad news if these keys give access to a private database.

To solve this, you can either move these files to another directory or add them to a .gitignore file. This file contains a list of files and directories that git will ignore (i.e., it won't track any changes, stage them for commits, or push them to remote repos).

git commit -m "<COMMIT MSG GOES HERE>"

git commit -m "<COMMIT MSG GOES HERE>"

What it does:

  • Commits the file changes to the repo along with a message about the changes made (e.g., "fix broken link")

Useful when:

  • You're satisfied with the changes you made and want to "save" them

Variant

git commit -am "<COMMIT MSG GOES HERE>"

What it does:

  • Adds all untrack files to staging, then commits them (it's a 2-for-1 command)
    • Here the -am tag is for adding the untracked files to staging (the a part) and attaching the message (m) for the commit

Useful when:

  • You've made a lot of changes and don't want to have to type two separate commands for add and commit (i.e., you're feeling lazy you're a productive programmer and want to save time)

git push

git push

What it does:

  • Pushes commits to remote repo

Useful when:

  • You've made your changes and its time to share them with the world!

Variant

git push --all --set-upstream origin

What it does:

  • This sets the upstream of the repo to the remote repo so it will push future commits to the remote repo

Useful when:

  • You've created a repo on your local machine and you want to push the files to a remote repo

Note: Unless you decide to change the upstream, you only need to this once. I usually do it on my first push.

Other commands

  • git branch <NAME>: Creates a new branch with desired name (e.g., "add-feature")
  • git checkout <NAME>: Moves to specified branch (e.g., from main to add-feature)
  • git checkout -b <NAME>: Creates new branch and moves to that branch
  • git branch -d <NAME>: Deletes specified branch
  • git fetch: Fetches new changes from remote repo
  • git merge <NAME>: Merges changes from the named branch into the current branch

Resources

  • Git Documentation - Docs are always a good starting place when you want to learn how to use a tool
  • Learn Git Branching - Fun interactive resource for beginners to learn how git branching works

References

Thanks for reading

Thanks for reading. I hope you found it useful.

What git commands to you use the most? Did I leave any out?

Let me know in the comments. Hearing your thoughts makes this more of a conversation and helps us both learn. 😄