Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Opensource.

com: Git Cheat Sheet BY MATT BROBERG

Git is the dominant version control utility these days. Here’s how to be effective using it.

The Essentials — When working with git on your own or Important Flags — These are my personal favorites for
with others. keeping everything organized.
git status To remind you of where you left off. See a summary of git reset HEAD -- get back to the last known commit
local changes, remote commits, and untracked files. and unstage others
git diff To see the specific local changes to tracked files. git add -u add only the updated, previously-
Use --name-only to see changed filenames. committed files
git add To stage changes to your tracked and untracked gi
t log --graph for a pretty branch history. Create
files. Use -u, -a, and . strategically. --pretty=oneline a shell or git alias for easy access,
--abbrev-commit such as git lg
git commit To create a new commit with changes previously
added. Use -m and add a meaningful commit message.
git push To send changes to your configured remote Working with a Remote Repository — Once you get
repository, most commonly GitLab or GitHub. into the flow, you’ll frequently contribute back to larger
projects, and possibly managing forks of forks. Here are
Basic Flow — Daily usage of git, including flags some tips for doing so.
git init cd to your local project git fetch --all downloads all commits, files, and
git status that you want to start references to branches on all remote
git add --all versioning with git. You repositories so you can then git checkout
git status only have to run git or pull what you want to work on.
gi
t commit -m "meaningful initial init the first time to
gi
t pull --rebase Merge all commits since your last common
commit message" set up the directory for
<remote> <branch> commit from the remote branch without
git show version tracking.
creating a merge commit
git diff And you begin to hack git stash Use this as needed to save uncommitted
gi
t commit -a -m "Another commit on your local files, changes so you can git stash pop them
message. -a performs the add then commit at regular onto a different branch.
step for you" intervals
git stash pop bring it back
git status
gi
t log --graph --pretty=oneline git add [-A or . or -- Be intentional about what files you add
--abbrev-commit <filename>] to your commits, especially if you want
to open a request to merge them into an
gi
t log --graph --pretty=oneline After a while, you have upstream project.
--abbrev-commit 3 commits that would be
git reset --soft HEAD~3 more meaningful as a gi
t commit -m Most projects have a format they prefer for
git diff --cached single commit "commit message" commit messages. Look at CONTRIBUTING.
gi
t commit -a -m "Better commit md files in the project or review previous
message for last 3 commits" commits to get an idea of their format.
git status Lastly, you delete some gi
t push origin Push your current branch to your remote
git diff --cached unneeded files in the <branch> titled “origin” and branch named <branch>
git add -u current directory gi
t checkout -b A shortcut for git branch <branch> &&
gi
t commit -m "Another commit <new_branch> git checkout branch. It’s great for when
message. -u adds updates, you want to experiment with an idea and
including deleted files" have a new branch to try it out on that can
git status later be merged or deleted.
gi
t log --graph --pretty=oneline t checkout master Great to get to the most recent commit for
gi
--abbrev-commit && git pull a project you only infrequently follow.
git push origin master --rebase
gi
t reset --hard For when you inevitably get lost in all the
Basic Branching — Branches represent a series of commits. origin/master git-fu and need to get to a known state.
git branch --all list all local and remote branches WARNING: this erases all changes, even
commits, since the last commit pushed to
git checkout <branch> change to an existing branch the remote origin on branch master.
t checkout -b <branch> make a branch based off of master
gi gi
t push origin For when you inevitably do something
master and check it out master right! Send your changes up to your
gi
t checkout master && merge branch changes onto master remote titled origin on branch master.
git merge <branch>
Helpful Reads
Getting Help • Read this excellent guide to your first git repository
git <cmd> -h great for quick review of command flags • Learn more about git branching
git <cmd> --help to dig into the full man pages of the command • Dig deeper into reset and rebase

opensource.com Twitter @opensourceway | facebook.com/opensourceway | CC BY-SA 4.0

You might also like