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)
- Delete a remote branch
- Switch from HTTP to git/ssh
- Set upstream branch
- Abbreviated status
- Useful Git aliases
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.
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
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 git@github.com:tobyweston/playground
This will alter your .git/config
file from
[remote "origin"]
url = https://github.com/tobyweston/playground.git
fetch = +refs/heads/*:refs/remotes/origin/*
to
[remote "origin"]
url = git@github.com:tobyweston/playground.git
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.
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"]
to
[branch "master"]
remote = origin
merge = refs/heads/master
Do the same for any other remote branches.
Abbreviated status
The standard git status
output
|
The abbreviated git status -sb
version
|
Useful Git aliases and config
Set the proxy, your user name and a couple of useful configurations. Feed straight in from the shell.
|
Some useful aliases.
|
Recommended Reading
- Pragmatic Guide to Git (Pragmatic Programmers), Travis Swicegood
- Pragmatic Version Control Using Git: 1 (Pragmatic Starter Kit), Travis Swicegood
- Pro Git (Expert’s Voice in Software Development), Scott Chacon