Git Cheatsheet

Creating a Repo
git clone [url] Download the Git repo at url into a new directory.
git clone [url] [dir] Download the Git repo at url into a new directory called dir.
git init Initialize your current directory as a new Git repo.
Working with Remotes
git remote Show your current remote.
git remote -v List all your remotes.
git remote add [name] [url] Add a new remote called name pointing to url.
git remote set-url [name] [newurl] Update the remote called name to point to newurl.
git remote rename [oldname] [newname] Rename the remote called oldname to newname.
git remote remove [name] Remove the remote called name.
Working with Branches
git branch Show your current branch (and all your other branches).
git branch [name] Create a new branch called name off of your current branch.
git checkout [name] Switch your current branch to name.
git checkout -b [name] Create and switch to a new branch called name.
git branch -d [name] Delete the branch called name.
Making Changes
git status Show a summary of your local changes.
git diff Show the details of your local changes.
git diff [path] Show the details of your local changes to path.
git restore [path] Discard your local changes to path.
git add [path] Stage your local changes to path so they're ready to commit.
git diff --staged Show the details of your staged changes.
git restore --staged [path] Unstage your changes to path.
git commit -m "[message]" Commit your staged changes with a descriptive message.
Syncing your Changes
git pull Pull any new commits to your current branch from your current remote.
git pull [remote] [branch] Merge commits from branch on remote into your current branch.
git push Push your local commits to your current branch on your current remote.
git fetch Pull down a list of branches from your current remote.
git fetch [remote] Pull down a list of branches from remote.
git fetch --all Pull down a list of branches from all remotes.
Miscellaneous Commands
git help Show the Git manual.
git help [command] Show the Git manual for command.
git stash Temporarily hide your uncommitted local changes.
git stash pop Replay your stashed changes on top of your current files.
git log Show the Git commit log.
git log --oneline --graph Just try it. It's beautiful.
A Typical Workflow
Fork a repo on GitHub. This gives you a place to keep your changes.
git clone [url] Clone the repo to your local machine.
cd [myrepo] Change directory into the repo you just cloned.
git remote add upstream [url] Add a remote for the repo you forked.
git checkout master Make sure you're starting from the master branch.
git checkout -b mybranch Create and switch to a new branch for your feature.
Edit stuff locally. Since you're on a branch, you won't mess anyone else up.
git status See what files you changed. Make sure they're all things you want to commit.
git diff [path] Check your individual changes to make sure they're all good.
git add [path1] [path2] [...] Stage all the files you want to commit.
git status Make sure you've staged all the files you want and none of the files you don't.
git commit -m "[message]" Commit your staged files. Use a message so people can tell what you did.
git push Push your changes to your remote.
Repeat. Commit early and often. Repeat 7-13 until you're happy with your feature.
Make a PR on GitHub. This tells your upstream that you'd like to get your feature merged in.
Deal with feedback. They'll want changes. Repeat 7-13 until they're happy with your feature.
Your PR gets merged! Your code is now part of the upstream project!
git checkout master Go back to your master branch.
git pull upstream master Update your master branch with the upstream changes.
git push Push these new changes to your remote.
git branch -d mybranch No need to keep your feature branch around any more.
Repeat! Time for a new feature! Go to step 5 and start again.