Practical git cheat sheet

Do you want to improve your git skills? Do you ever struggle with merge conflicts, giant changes, or mysterious errors?

I learned how to use git from a few years of working in tech. This is a practical git cheat sheet you can reference for many situations. I go over:

What is git?

Git is a version control system. Developers use version control to track and manage changes to software. Version control is like Google docs. Both let you to:

For example, git log is like Google Docs version history. You can see and restore previous snapshots of your progress. To do so:

Google Docs version history git log
A screenshot of Google Docs version history A screenshot of a git commit log

Basic git terminology

Learn basic git terminology to have a good mental model of what's going on. Here are some common terms you'll see:


git branches and commits A diagram of the git branching workflow. There are 2 features: feature branch A and feature branch B. They split off from the master branch and then merge back into it.


Basic git command workflow A diagram of the basic git command workflow. It shows the commands git add, git commit, git push, git pull, and git reset.

Common git commands

Here are common git commands for day-to-day development. If you've never used git before, I suggest going through a tutorial to set up a repository first.

Command Usage
git log
Show commit history.
git log -- [file]
Show commit history for a file.
git status
View changes to your working directory.
git help [command]
Get more information on a command.
git checkout [branch name]
Switch branches.
git checkout [commit hash]
Restore a previous version of the code.
git checkout -b [branch name]
Create a new branch with name [branch name].
git checkout -
Go back to the previous place you were at.
git branch
List branches. The asterick shows which branch you're on.
git branch -d [branch name]
Delete the branch with name [branch name].
git reset --soft [prev commit]
Go back to [prev commit] and move changes back to the staging area.
git revert [commit]
Undo changes from a commit.
git cherry-pick [commit]
Grab changes from a commit.
git add .
Add all changes from your working directory to the staging area.
git add [file...]
Add specific files from your working directory to the staging area.
git commit
Add a multiline commit message. See Vim 101 for more details.
git commit -m [message]
Add a one line commit message.
git merge [branch name]
Merge [branch name] into your current branch.
git stash
Stash away changes from your working directory.
git stash apply
Apply the most recently stashed changes.
git blame -- [file]
Figure out who wrote each line of a file.

Here are some tips on git commands:

Practical git workflows

Here are some practical git workflows I use as a software engineer:

Workflow Commands
Making a feature branch
git checkout master
git pull
git checkout -b [branch name]
Committing a change
git add [file...]
git commit -m [commit message]
git push
Keeping a branch up-to-date
git checkout master
git pull
git checkout [branch name]
git merge master
Resolving merge conflicts Fix the merge conflict in your text editor.
git add [file...]
git commit -m [commit message]
Cleaning up commit history
git reset --soft [prev commit]
git commit -m [commit message]
git push --force
Applying changes between two commits to a new branch
git reset --soft [prev commit]
git stash
git checkout [new branch name]
git stash apply
Cherry-picking commits to a new branch
git checkout -b [branch name]
git cherry-pick [commit]
Reverting a change
git checkout master
git checkout -b [branch name]
git log
git revert [faulty commit]

Conclusion

When I first started programming, I struggled to install git and to set up a repository. Git can be confusing at first, but you'll get familiar over time.

My biggest piece of advice on git is don't panic and don't blindly copy/paste commands. Typing random commands won't solve the problem and can make it harder to understand the issue. You can review the section on basic git terminology and common git commands to understand what you're doing.

If you're still confused, ask the Internet. If the first result doesn't work, ask for help. I've found that either the first result solves the problem or something else is going on. Like with a lot of software problems, you should ask for help to resolve your issue and to learn and grow.

Appendix

This section elaborates on various git workflows:

Terminal 101

To use git well, you should learn how to use the terminal. To open a terminal on Mac, type command-space and search "terminal".

I use a Mac computer and Sublime Text for a text editor. Useful commands may vary depending on your operating system and preferred text editor.

Here's a short reference of terminal commands:

Command Usage
pwd
Print working directory. Tells you where you are.
cd [file path]
Navigate to a directory.
cd ~
Navigate to the home directory.
ls
List everything in the directory.
control-R Search previously typed commands.
tab autocomplete Type something and hit tab to autocomplete.
open .
Open the directory in Finder.
sublime .
Open the directory in Sublime Text.
Vim 101

Vim is a text editor in your terminal. It looks something like this:

A screenshot of the Vim terminal

My first experience with Vim was wondering "how do I get out of this thing?". It's confusing at first. In the main mode, tapping on keyboard keys moves the cursor and doesn't add text.

Vim is useful for writing git commit messages. Here's a short reference:

What to type Usage
:q! to quit without saving Get out of vim without saving your commit message.
i to insert Type a commit message in vim.
esc to go back to the main mode Once you're done typing your commit message.
:wq! to quit and save Get out of vim and save your commit message.
Fixing a merge conflict

Usually, Git can merge changes without intervention. Merge conflicts happen when Git doesn't know which changes to keep and asks you to help out.

Merge conflicts look scary, but are easy to fix when you get used to it.

Here's what a merge conflict looks like in the terminal:

  Cat:personal-website elainelin$ git merge practical-git-cheat-sheet
  Auto-merging writing.html
  CONFLICT (content): Merge conflict in writing.html
  Automatic merge failed; fix conflicts and then commit the result.
      

If you read the error message, you see there's a problem with the file writing.html.

Open the file in your text editor. Git highlights the merge conflicts with angry carats. Later, to make sure you've fixed all merge conflicts, you can search the repo for all instances of "<<<<" or ">>>>".

  <<<<<<< HEAD
    <table class="table post-table">
  =======
    <table class="table table-borderless post-table">
      <td class="post-date">May 9, 2021</td>
      <td>
        <a href="/writing/software/practical_git_cheat_sheet.html"
          >Practical git cheat sheet</a
        >
      </td>
  >>>>>>> practical-git-cheat-sheet
      

It's up to you to figure out which changes to keep or not. In this case, the resolution might look like:

    <table class="table post-table">
      <td class="post-date">May 9, 2021</td>
      <td>
        <a href="/writing/software/practical_git_cheat_sheet.html"
          >Practical git cheat sheet</a
        >
      </td>
    
Managing a big feature branch

If you're working on a big feature, you can set up a big feature branch. This helps you:

A diagram showing how you manage a big feature branch.

  1. Suppose you're working on a big feature and set up feature branch A.
  2. As you work on the feature, make branches A', A", etc. off branch A.
  3. Merge A', A", etc. into branch A.
  4. When all of the work is done, you can merge branch A into master.

Here are some example commands:

Workflow Commands
Branching off the big feature branch
git checkout [branch A]
git pull
git checkout -b [branch A']
Keeping A' up-to-date with branch A
git checkout [branch A]
git pull
git checkout [branch A']
git merege [branch A]