Git Cheat Sheet

 

Create new repository

Create a new repository on the command line

git init
git commit -m "first commit"
git remote add origin git@github.com:username/repo-name.git
git push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:username/repo-name.git
git push -u origin master

Branch

Basic usage

List branches:

git branch

Create a new branch:

git branch <branch>

Delete and force delete a branch:

git branch -d <branch>
git branch -D <branch>

Rename the current branch to <branch>.

git branch -m <branch>

List all remote branches:

git branch -a

Switch to a new branch:

git checkout <existing-branch>

Create a new branch and switch to it:

git checkout -b <new-branch>

Create new branch based on <existing-branch> instead of current HEAD:

git checkout -b <new-branch> <existing-branch>

Remote Control

Add remote repository <remote-repo> to local repository config:

git remote add <remote-repo> https://github.com/user/repo.git

Pushes the <new-branch> branch to <remote-repo>:

git push <remote-repo> <new-branch>~

Delete branch <new-branch> from remote repository:

git push origin :<new-branch>

Checkout remote branch:

git checkout <remotebranch>