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

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


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 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


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

cd my-forked-repo
git remote add upstream
git fetch upstream
git checkout master
git merge upstream/master
git push origin master 


Add a submodule to an existing project:

git submodule add new-directory-name