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

Happiness is…

…being able to use Git even when you thought you we’re stuck forever with Team Foundation Server at work. All thanks to Git-Tfs, a bridge between, yep you guessed it, Git and TFS! I’d already been using Git locally for code I was working on by myself and pushing my changes to a mapped drive that gets backed up every night but it was pretty awkward in terms of interacting with TFS and well, there isn’t really a solution when it comes to fetching other people’s changes. But now there is and I’m a very happy developer. Having experienced some considerable pain with the source control aspect of TFS recently I began to wish for something a little less problematic. Binning it altogether would be nice but probably unrealistic. Thankfully, Git-Tfs means I no longer have to care so much.

It’s easy to setup. Once downloaded and unzipped, you add the location to your PATH environment variable, open up git-bash and perform a clone of your TFS repository:

git-tfs clone "http://teamfound:8080/tfs/<your tfs collection name>" $/<your project name>

This creates a copy of the latest build with all the project history (which can take a while depending on project size). At that point you’re good to go with your usual local repository git workflow such as branching and merging etc. When it comes time to get the changes back into TFS you can either do a checkin with a message as you would normally:

git-tfs checkin -m "some message"


git-tfs checkin checkintool

which brings up the usual TFS check-in dialog. And the part that fell down for me with my previous method – grabbing other people’s changes – is now easily achieved simply with:

git-tfs pull

That’s just about the gist of it. I can only describe this tool as a god-send so I hope it continues to get updates and improvements. It’s definitely worth looking at if you’re in a similar position where you’d rather touch TFS as little as possible and, well, if you’ve never used Git then maybe this will give you the excuse you need to start.

Happiness is…