November 2011
2 posts
Changing The URL Of An Upstream Submodule...
Just a short one today. Say you have a repository that contains a submodule. But, for whatever reason, you want to change the URL that the submodule points to. This could be because that repo moved, or changed it’s name. Or maybe you’ve forked that repository and you want to use your fork as a submodule. All you need to do is to change the URL in the “.gitmodules” file...
Nov 11th
5 tags
Set Up a Submodule Where Only You Have Write...
Say you want to add a submodule to your repository, and this submodule is a one to which you have push access. It is frequently useful to be able to publish a repository with the submodule URL as pull-only, while keeping the push-pull URL in your own repository. This way, you can make changes to the submodule repository from within the enclosing repository. This is a simple thing to do in Git once...
Nov 9th
27 notes
April 2011
1 post
6 tags
Git merge --squash
There is a flag to the git merge command: --squash which at first looks as if it does the same thing as a rebase, but this isn’t exactly so. This post attempts to explain the difference. Consider the following repository structure: There is a master branch and two feature branches feat1 and feat2. The feat2 branch will introduce a merge conflict, which is why it is highlighted in...
Apr 5th
14 notes
March 2011
1 post
4 tags
Git submodule foreach – updating many submodules...
If you have a repository with many submodules, it’s easy to update them all at once. For example: I have my vim dotfiles in a Git repository. Not only does this make it easier for me to keep my vim setup the same across multiple machines, but also lets me add other bundles as submodules which makes it easy to update the bundles and propagate changes. Once in a while I go into the...
Mar 5th
19 notes
February 2011
2 posts
8 tags
Getting a diff between the working tree and other...
I’ve previously written about how to get the diffs between your working tree, the index and the last commit. But you frequently need to see the difference between the code you’re working on and previous commits. Here’s a diagram that summarises the commands: If you’ve read the previous article then this should make sense I’ve used HEAD~ to represent the parent...
Feb 23rd
14 notes
4 tags
Writing Git commit messages
Unsurprisingly, there is a convention for writing Git commit messages. This comes from the submitting patches guidelines for Git itself. In summary: The first line of the commit message should be a short description (50 characters is the soft limit), and should skip the full stop The body should provide a meaningful commit message, which: uses the imperative, present tense:...
Feb 15th
January 2011
2 posts
7 tags
Adding a GPG Public Key to a repository
After my post about signed tags yesterday Kevin Ballard left a comment about a neat trick that lets you put the gpg public key used to sign tags into the repository itself. This explains how to do just that. As a practical example I’ll use this Hello World repository on GitHub. What this technique does is to add the gpg public key as a blob to the repository, but not as part of any...
Jan 18th
22 notes
5 tags
Signing a Git Tag
I’ve already written about the Git Tag Object and made a passing reference to signed tags. Git doesn’t make any checks about whether the name and email you enter into a git config file really is you, so it’s trivial make a check-in that looks like it’s been made by someone else. But, if you have some form of GPG installed, you can cryptographically sign a tag object (not...
Jan 17th
18 notes
April 2010
17 posts
6 tags
Quick commit editing with vim
On Mac OS a and Unix systems Git defaults to using vim as the editor for commits.(I don’t know what the default is on Windows) You can change this, if you like, but for now I’ll just give a couple of tips for using vim — just enough to get by. When Git needs a message for an action it will bring up vim in view mode. You can move around using the h(move left), l(move right), k(line...
Apr 17th
3 notes
8 tags
The “Hole Hawg” of version control
I use Git. I use Mercurial, sometimes, but I use Git. I used to find it hard going (actually, I still find it hard going, but it’s a lot easier now). I used to find it frustrating that you pull from origin/master, but push to origin master; and how the documentation talks of things like refspecs, HEAD, and ORIGIN_HEAD. Really; you don’t want to wade through stuff like that when you’re trying to...
Apr 16th
4 notes
4 tags
Git manpages
Depending on how you installed Git, there may or may not be man pages on your system. Using manpages Manpages are the standard way to get help with command line programs. To see if they are installed, type man git into the terminal and you should get the man page for Git. The space bar will move the page forwards and q will get you back to the terminal. If you want to know more about man,...
Apr 15th
4 notes
5 tags
Forcing yourself on Git
Consider this scenario: You’re working on a branch and you don’t like where it’s going. You want to switch to another branch and delete the branch you were on. However, Git won’t let you change branches, because you have untracked changes to files (i.e. changes that are not in the index), and by switching branches the changes to the files will be overwritten and, because they are not in the index,...
Apr 14th
3 notes
7 tags
Three ways of excluding files.
Almost every git user knows about adding a .gitignore file to root of the project to control the visibility of files and folders. Because this file is in the repository; this configuration also applies to remote repositories of the project. But it’s not the only way. I’m going to tell you how to get Git to ignore files on a per computer and per repository basis. These could be better choices in...
Apr 13th
35 notes
8 tags
Checking the code in the index.
I was at Barcamp Bournemouth over the weekend where I gave a talk on Git. I was asked a question and the Barcamp combination of sleep deprivation and overnight indulgence led me to give an incorrect answer. This is an attempt to clear things up (sorry Adrian)! Say you have changes staged in the index and untracked changes in the working directory. Before checking this code in, you want to run...
Apr 12th
3 notes
6 tags
Adding an empty directory to a commit.
I believe there are cases where a directory needs to exist in a repository but it’s empty. Either because it will be written into at a later stage, or because the contents have been ignored. If you remember, Git tracks content, not files. Because there is no content, the directory is not tracked, even if you add it. This might cause problems. The work-around is simple. Just create an empty file...
Apr 11th
5 notes
5 tags
Getting files from another branch
Say you are on your working branch, let’s call it feature. You’ve made some changes to your files and you decide that you didn’t really want to change (say) the main.c file. You don’t need to check out the master branch, copy the file change back to feature and paste the changes. What you can do is get the state of the file from the master branch directly. git checkout master main.c This will...
Apr 10th
3 notes
6 tags
Deleting branches on a remote.
There comes a time when you need to delete a branch on a remote. Say you have a repository set up only for your own use which you use to keep half-baked development branches in sync between machines. Once you’ve done with the branch, you don’t need to have it exposed, not least because the branch is unlikely to exist on your local repository anyway. Say the remote branch is named backup and...
Apr 9th
4 notes
5 tags
Simple aliases
All this typing, all these commands. It doesn’t have to be this hard. You can set up aliases in Git’s configuration that let you concatenate these commands and flags or even run scripts without as much keyboard action. But sometimes for the simpler commands I find it easier to use shell aliases. Another advantage of this is that you don’t have to type gitin front of the aliases. Here’s the way...
Apr 8th
3 notes
4 tags
Fast forward merge
Say we have a repository with a master branch and a feature branch. Since the feature branch has come off master, there have been no other changes added. Also, the master branch is the currently checked out branch and we can see that because HEAD points to master. So, we want to bring in the changes from the feature branch to the master branch so we issue a simple git merge feature. Even if...
Apr 7th
2 notes
6 tags
What’s in the commit?
I’m as reluctant as the next person to read a slab of text when I’m just looking for information. So just a short one today. Say you want to see what exactly you are adding to a repository with a commit. Here’s a handy command: git commit -v The important thing here is the -v (shorthand for the --verbose) flag. When the editor opens up with the commit message, the diff between the current...
Apr 6th
2 notes
7 tags
Git objects: the tag
The final object left to cover is the tag. Strangely (or not, depending on your view of Git) there are two types of tags. There are lightweight tags which are almost like branches, but here I’ll introduce the tag object. Tagging A tag is a shorthand way of naming an object, usually a commit, so it can be referred to easily. A tag object differs from a lightweight tag in that it is a...
Apr 5th
3 notes
8 tags
Git objects: the commit
The third Git object is the commit, and this is what holds everything together. The top level object in the repository is the commit object. It references the top level tree and also keeps other information such as the committer, the date and time of the commit, references to the previous commit (or commits in the case of a merge) and the commit message. It can also have an author and can be...
Apr 4th
7 tags
Git objects: the tree
Yesterday I wrote about the blob, the basic building block of the git repository. Today, I’ll talk about the tree. The tree is a nice recursive object that holds blobs and other trees to make up a graph. It’s the tree object that contains the names of the files that the content in blobs expands into. It also holds the names for the tree objects that it contains. Just like other git objects, the...
Apr 3rd
4 notes
6 tags
Git objects: the blob
I’m going to step back from tips over the next few days and cover some fundamentals over the Easter weekend. There are 4 kinds of objects that Git works with: The blob The tree The commit The tag Although I’ll cover the blob today there are common characteristics for each of these objects These objects just store contents. For a file, it’s just the contents of the file, not the name or...
Apr 2nd
5 tags
Fixing merge conflicts
Branching is great. Being able to work on code that you don’t have to check in, that doesn’t risk polluting the main branch until it’s finished is great; but you have to be able to bring the changes back in at some stage. One way is to use git merge. Here’s a nice contrived example of a simple repository containing two files. Greetings.txt and Partings.txt. There are three branches; master,...
Apr 1st
March 2010
8 posts
7 tags
Updating all remotes at once.
Just a quick one. Say you have a repository with many remotes. Don’t roll your eyes; it’s very likely if you have many collaborators, or if you’re tracking a popular open source project. To pull down all available changes at once try this: git fetch --all No surprise about what it does. Of course, this is a fetch, not a pull, which is a combination of git fetch and git merge, so after this...
Mar 31st
2 notes
6 tags
Branching out
Branching in git is fast and easy. Coupled with fast merging, and rebasing, it is very convenient to always work on a separate branch. Typically, you create a branch, make changes to your project on that branch, merge this working branch with one of the permanent branches and delete the temporary branch. Let’s take an example. Assume you have a repository with a single branch; master with a few...
Mar 30th
3 notes
6 tags
Simple history
There are times when you just want to have a quick look at the history of what you’ve been working on. You could use git log but that shows you the full history with the details for each commit. Not exactly helpful if you just want a summary or only details for the last few commits. Try: git log --oneline This will just show you the short sha for each commit and the subject line of the...
Mar 29th
3 notes
4 tags
Cleaning up your files
The clocks have gone forward in the UK today, so here’s a spring cleaning tip for the files in your project. This is not tidying up the repository itself, that’s different, but the files that Git doesn’t track. The command that I use: git clean -dxf The man file for git-clean is quite small, so have a look at it. -d means directories will be removed, -x removes all untracked and ignored...
Mar 28th
5 tags
Break from your origins.
When you clone a repository, the remote is given the name origin by default. But you don’t have to accept this. Git is a distributed version control system, so in most cases there isn’t a canonical repository. This might be confusing, but with a little thought it makes sense, and I’ll cover it some other day. I consider my repositories to be versioned backups as well as a source control...
Mar 27th
2 notes
6 tags
What’s the difference? Part 1
This is one that used to catch me out when I first started using Git. I’d make some changes, add things to the index and then try and get a diff by calling git diff and nothing would come up. Of course, git wants to know what I want a diff between. If you remember yesterday’s post about the buckets there are three types of diff that you can get. If you’re new to git you may be asking...
Mar 26th
8 tags
The four buckets — how Git considers content.
A slightly longer post today but it’s important that the basics of git are covered. My first suggestion is to think of git as tracking content, not files. In the coming days I’ll cover the way that Git stores information and this should become a lot clearer. But it helps to realise that the contents of a file and the name of that file are not kept in the same place. Secondly, Git only tracks...
Mar 25th
4 tags
Amending your last commit
A nice simple one to start with, and protects against those idiot moments when you check something in that you didn’t want to. Say you’ve just made a commit to your repository and you think “oops! I didn’t want to do that”. Well, you can change your last commit quite simply. Make whatever changes to your code that need to be made, add the changes to your index and then simply type this...
Mar 24th