good robots do what they're told

Useful Git Commands

More as a reminder to myself than anything, here’s a bunch of Git commands I fund useful.

Create a branch on a remote (pushing a branch to a remote)

Having created a new feature branch locally, you can push to a remote, creating a new branch destination using.

git push -u origin feature_branch

The -u sets upstream tracking and is optional.

« Back to the list

Delete a remote branch

Having pushed your feature branch,

git push origin feature_branch

delete locally,

git branch -d feature_branch

then delete remotely,

git push origin --delete feature_branch

which is short hand for git push origin :feature_branch. The colon looks out of place but its really just the everyday syntax of git push <remote> <local branch>:<remote branch> with a empty string representing the local branch. Effectively, it’s saying, take no branch from my local branch and push it to the remote branch.

More on remote branches from git ready

« Back to the list

Switch from HTTPS to git/ssh

If you’re using HTTPS as your fetch url (check your .git/config file), you’ll likely be asked for your username and password on each push. Switch to git/ssh with the following (assuming you’ve setup ssh).

git remote rm origin
git remote add origin

This will alter your .git/config file from

[remote "origin"]
    url =
    fetch = +refs/heads/*:refs/remotes/origin/*


[remote "origin"]
    url =
    fetch = +refs/heads/*:refs/remotes/origin/*

but won’t associate the remote branch with a local one. You’ll need to set an upstream branch for that.

« Back to the list

Set upstream branch

Trying a git pull after switching from HTTPS to git/ssh above will give you an error.

You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

Set the upstream branch with

git branch --set-upstream master origin/master

which should report back,

Branch master set up to track remote branch master from origin.

and change your .git/config file from

[remote "origin"]


[branch "master"]
    remote = origin
    merge = refs/heads/master

Do the same for any other remote branches.

« Back to the list

Abbreviated status

The standard git status output

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
# modified:   LegoWars/BattleShip.h
# modified:   LegoWars/BattleShip.m

The abbreviated git status -sb version

$ git status -sb
## master...origin/master [ahead 1]
 M LegoWars/BattleShip.h
 M LegoWars/BattleShip.m

Useful Git aliases and config

Set the proxy, your user name and a couple of useful configurations. Feed straight in from the shell.

git config --global http.proxy myproxy:8080
git config --global
git config --global me
git config --global color.ui true

Some useful aliases.

git config --global alias.last "log -1 HEAD"
git config --global "status -sb"
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"

Recommended Reading

More in the Git Series

Over to you...