This page is just some useful commands for Git that I keep forgetting.

This is an evergreen page, updated every now and then.

Files

Undo changes to a file (discard unstaged changes):

git checkout -- filename.txt

Branches and Tags

Create (checkout) a new branch:

git checkout -b mybranch

Push a branch remotely (to the origin remote repository):

git push -u origin mybranch

Delete a branch both locally and remotely:

git push origin -d mybranch
git branch -d mybranch

Merging

Merging changes from the development branch into master, and squashing the intermediate commits:

git checkout master
git merge --squash develop
git commit

Remote repositories

List all remote repositories:

git remote -v

Forks

Sync a fork to keep it up-to-date with its upstream repository:

cd my-forked-repo
git remote add upstream https://github.com/username/original-repo
git fetch upstream
git checkout master
git merge upstream/master
git push origin master 

Submodules

Add a submodule to an existing project:

git submodule add https://github.com/username/example-repo new-directory-name