The Git Get By List

Git for me is THE best and most powerful source control system around. I can’t imagine developing without it anymore such is the flexibility and control it gives me over all other source control systems I’ve used. For my own reference purposes and as a quick goto/reminder list, here is a bunch of my most common Git commands that I use on a daily basis. Of course, there are so many git commands that I probably haven’t even used most of them but these are what currently work for me.

git status

Simply shows the current state of affairs of the repository. What’s being tracked, what isn’t, etc.

git remote add origin "<path/to/remote/repo>"

Add a remote location to push to/pull from. This is set automatically when cloning

git log --pretty=oneline

Simple, succinct log

git checkout -b mynewbranch

Creates a new branch “mynewbranch” and checks it out at the same time

git branch -d mynewbranch

Delete “mynewbranch” locally

git push :mynewbranch

If you pushed the branch then this deletes it on the remote

git pull --rebase

Grabs latest changes from origin, rewinds your changes, then plays them back over the top of the pulled changes. I use this over git pull, git merge, and git fetch all the time as I think the history looks better/cleaner in gitk and I seem to get fewer conflicts this way

git add -A

Adds all files to the staging area, both updated and new files, as well as those marked for delete

git rm --cached <file> 

Removes file from staging after the result of a git add command

git diff --cached 

Shows the differences between original and changed files after adding them to the staging area

git commit -m "commit message"

Commits the staged changes

git commit --amend

Adds currently staged changes to the previous commit. Useful for when you should have added them previously but forgot!

git reset --soft HEAD^

Undoes the last commit putting the files back in the staging area. Keep re-issuing to undo one commit at a time. Useful if you forget to add a file or hunk as long as you haven’t already pushed to other users.

git clean

Removes untracked files from working directory. Use the -n (–dryrun) option to see what will be affected and -f to make it happen

git reflog

Shows every action taken on the repository and even contains any commits you may have lost and want to retrieve (as long as you haven’t done a garbage collection). Useful for getting back into a good state to undo the damage you did with a previous command!

git gui

Visual tool to help see what’s staged, what’s not, and an easier way to stage hunks rather than whole files

gitk --all

Visual tool to see all the changes in the repository. The –all flag means include all branches

There’s obviously a whole lot more but these get me by for the most part. I’ll keep adding more as and when they creep into my daily usage but for now this is what keeps me productive.

The Git Get By List

Importing a SVN Dump File

I recently had a need to view some source code that I have stored online in a Subversion host. Trouble is, I haven’t used Subversion for a long time now (Git rules!) and I couldn’t remember how to import the dump file that I’d asked the host to create for me into a local repository. Why not just checkout the repository? Well, at work we don’t use Subversion (TFS, oh joy!) and even after installing it I discovered that it was blocked by the proxy server. I didn’t want to wait around for an admin to open it up for me so a dump file was my next best choice.

Turns out the answer is to use the svnadmin tool (which is included with Visual SVN Server) with the following format:

svnadmin load “path-to-repository” < "path-to-dumpfile"

Once you've got your dump file make sure you uncompress it if necessary, then from the commandline run:

svnadmin load "c:\repositories\myapp" < "c:\myapp.dump"

Just for completeness, in order to backup your SVN repository you can create your own dump file by using the following commmand:

svnadmin dump "c:\repositories\myapp" > "c:\myapp.dump"

and that's it!

Importing a SVN Dump File