I always forget certain git commands. Especially when I don’t use them often enough or am involved in a workflow that doesn’t use git to it’s fullest extent. So here’s a list of git commands in some sort of loose categorization (some of which I hope I remember now and others that I would like to be reminded of in case I forget).

Cloning and Configs

Show the remote info. (Note, origin is the default name for remote that the repository was cloned from.)

git remote show origin

Cloning a repository that’s available on GitHub.

git clone https://github.com/username/repository.git

Sometimes you may need to clone with a different user name. This is useful if you have a personal and a work GitHub with different username/password. [link]

git clone https://username@github.com/username/repository.git

You may need to set your user name and email for your local repository (not your global git config). This is useful if you don’t want your personal email associated with commits made to a work repository (and vice versa).

git config user.name <user name>
git config user.email <user email>

Branching and merging

List all the branches in your repository.

git branch

List all remote branches.

git branch -m

Delete a branch called feature.

git branch -d feature

To create a branch called feature and work on that branch.

git branch feature
git checkout feature
# or
git checkout -b feature

To push your new branch feature and track it on GitHub.

git push -u origin feature

To pull and updated version of the branch you’re working on.

git checkout feature
git pull

To pull a newly created branch new-feature (that’s not on your local machine).

git fetch
git checkout new-feature

Merging

To update a branch from another branch (e.g. updating your feature branch with new commits in master).

git checkout feature
git merge master

If you want to rebase then use the rebase option.

git checkout feature
git merge master --rebase

Merging vs rebasing

Some useful links that’ll probably describe the difference better than I ever can.

Diff

Sometimes it’s useful to look at the difference between the original file and the changes you’ve made via the command line. Before adding the file,

git diff foo.txt

and after adding the file

git diff --cached foo.txt

Submodules

So useful, and something I often overlook. There’s a great overview of submodules on the GitHub blog. A git submodule allows you to keep a git repository as a subdirectory of another git repository (link). Submodules are static in that they track a specific commit and won’t be automatically updated.

To create a submodule in the folder foo using branch master living at url/to/remote/repo.

git submodule add -b master url/to/remote/repo foo

To update a submodule.

cd foo
git checkout master
git pull
git cd ..
git add .
git commit -m "updating submodule in foo"

To clone a repository that uses submodules.

git clone --recursive url/to/remote/repo

Hugo’s directions on deploying a statically generated website on GitHub is a good example of using submodules. In this case, the submodule is in the public folder which is connected to the [username.github.io](http://username.github.io) repository. And this folder contains the site files generated by hugo when you build your site.