This page looks best with JavaScript enabled

Basic Git Commands

 ·  ☕ 2 min read  ·  🥓🥓🥓 werkn

This page contains a reference of basic git commands. This list is by no means exhaustive, essentially updated as I find things I need.

Git Basics

command description
git help <command> Get git help with command, start here 😎
git checkout -- <file> Discard all local changes permanently
git reset HEAD <file> Remove file from staged, keep modifications
git stash Discard all local changes, but ‘stash/put away’ for later usage
git diff <file> View changes for an unstaged file
git init <dir> Initialize a git repo in the directory specified
git branch List branches for this repo (current dir), if you don’t see a branch you know exists try git branch -a
git branch checkout <branch_name> Checkout a branch with the name provided. Use the -b flag to also create branch if doesn’t already exist
git push --set-upstream origin <branch_name> Push a local branch to origin (remote).
git push origin <branch_name> Push a local branch to origin (remote) without setting it as upstream.
git branch -d <branch_name> Delete a local branch. I usually move to a stable branch, then delete the branch I want.
git status List status of staged, unstaged and tracked
git commit -m "msg" Commit directly (staged files) with the provided message instead of launching something like vim/nano

Sharing and Updating Remote Repositories

command description
git pull <remote> Grab the remotes copy of the current branch, merge into local
git push <remote> <branch> Push the specified branch to remote, with all changes/commits locally. Creates a new branch on remote if it does not exist

Git Submodule(s)

Notes: Git submodules allow us to place other git repos within a project and install and maintain them using git. When cloning a repo containing a submodule we must run git submodule init ; git submodule update in order to also clone our submodule, cloning only creates the directories.

command description
git submodule add <repo> Initialize a git submodule in your current directory, specify a path to place @ that location

Common Errors / Messages

HEAD detached at 35e2f89

Your no longer on a branch, ie: master. You’ve checked out a single commit, find the branch the commit is from and check that out, git branch --contains 35e2f89 and then git checkout <branch_name>. I’ve had this occur when using a git repo with a submodule inside it with a branch other then master.

References:

Share on

Ryan Radford
WRITTEN BY
werkn
Developer / IT Guy / Tinkerer