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:
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:
git checkout
a previous commit.Google Docs version history | git log |
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
master
or main
branch.master
branch.master
when the feature branch is
good to go.
git checkout b6194c7fcbb11f3c250536425c67b45779458a59
git checkout b6194c7fcbb11f3c25
git checkout b6194c7
Basic git command workflow
git add
to move files to the staging area and then
git commit
to commit changes.
git reset
to move changes from the staging area to the working
directory.
git push
your changes to a remote copy of a repository.git pull
other people's changes from the remote repository.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.
master
branch,
do git checkout master
.
git log
to find relevant commit hashes. Learn how
git log
works in your terminal. On Mac, hit Enter to scroll down and q to
exit.
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:
error: commit 2ecd4566cde6ffb8ce5e103eb79e971483d0eaa2 is a merge but no -m option was
given
, adding -m 1
will probably fix it. This lets git know which side of the
merge should be considered the parent.
error: pathspec 'b61' did not match any file(s) known to git
, double-check
that you copied the commit hash correctly and provided enough characters.
fatal: pathspec 'writin' did not match any files
, double-check that you typed
the file path correctly.
--no-commit
when you do a cherry-pick or merge if you don't want to
commit the changes.
--no-verify
when you make a commit to bypass any pre-commit hooks.Here are some practical git workflows I use as a software engineer:
git merge
instead of git rebase
. That way, you only have to
resolve one set of merge conflicts.
git push --force
is dangerous.
git cherry-pick
each
commit, but it is often easier to grab all changes between two commits.
staging
branch before merging into master
.
master
. If you aren't working with a protected branch, you may not need to
create another branch for the revert.
Workflow | Commands |
Making a feature branch |
git checkout master |
Committing a change |
git add [file...] |
Keeping a branch up-to-date |
git checkout master |
Resolving merge conflicts |
Fix the merge conflict in your text editor.git add [file...] |
Cleaning up commit history |
git reset --soft [prev commit] |
Applying changes between two commits to a new branch |
git reset --soft [prev commit] |
Cherry-picking commits to a new branch |
git checkout -b [branch name] |
Reverting a change |
git checkout master |
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.
This section elaborates on various git workflows:
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. |
sublime
command to work, you need to download Sublime Text and follow
the command line setup instructions. See
Sublime command line setup
instructions.
Vim is a text editor in your terminal. It looks something like this:
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. |
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>
If you're working on a big feature, you can set up a big feature branch. This helps you:
Here are some example commands:
Workflow | Commands |
Branching off the big feature branch |
git checkout [branch A] |
Keeping A' up-to-date with branch A |
git checkout [branch A] |