Linux Kernel Development Using Git

4
11761
Let us git

Let us git

This article looks at setting up Git and using it for Linux kernel development. It is aimed at developers who would like to move their development environment to Git SCM. I assume that readers are well versed with source-control software.

Git is a powerful source-code-control software. Its fundamental design goal was “high performance”. Earlier, Linux kernel development used a free version of a proprietary software called BitKeeper. Later, when BitKeeper stopped being free, Linus Torvalds came up with the idea of Git (Global information tracker). He was the initial author of the software, and later it was passed to Junio Hamano. Linus has a special need for high-performance software, due to the volume of kernel patches he handles, and Git was the answer. The major advantage of Git is that it need not be connected to a server for committing code.

Prerequisites

You need to have Git downloaded and installed on the host machine (I am using Ubuntu Maverick). If you are using Ubuntu, you can simply issue the command sudo apt-get install git-core. Once installed, Git can be used as a regular user, and does not need super-user privileges.

Configuring Git

The git config command is used to set the Git repository options. The following examples are the most common options you need to set before you perform a code commit:

$ git config --global user.name "Surya Prabhakar"
$ git config --global user.email surya_prabhakar@dell.com

The --global option saves these options in the user’s home directory, in the file ~/.gitconfig.

If you need to manipulate a specific repository’s settings, instead of the global options, you need to cd into the repository directory, and issue the above commands without the --global switch. This will save the options in the config in the .git directory within the repository. System-wide changes can also be performed if the --system tag is used. For more options, refer to th man page with man git-config.

The Linux kernel with Git

Let’s concentrate on setting up your Linux kernel repository using Git. The Linux kernel has multiple Git repositories online. In fact, architectures like arm maintain their own Git repository. Here, let’s try syncing up with Linus Torvalds’ linux-2.6 Git repository. You can obtain information about other repositories from kernel.org.

Linus Torvalds’ repo can be cloned to your local machine using the following command:

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

Refer this URL for more linux-2.6 URLs. Depending on your Internet connection speed, it could take quite some time to download the whole kernel repository.

Git uses port 9418 for communication with the Git server. If you are behind a corporate firewall, the above will not work. For this reason, developers have provided an alternate URL to be used over HTTP port 80:

$ git clone http://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
Note: In case you have to go out via a proxy server, do not forget to set and export the proxy server address in the environment variable http_proxy, which Git uses, e.g, export http_proxy=http://your-corporate-proxy.com/:<port>.

The clone command creates a directory named linux-2.6 in your current directory.
To update your repository, use the command git pull. This downloads all new commits since the last git pull. Once this is done, you can work on your local copy without needing an Internet connection. Use git pull as follows:

$ cd linux-2.6
$ git pull

The above command will automatically use the original source URL to pull updates. To view the URL that is being used, run cat linux-2.6/.git/config.

Working with your local copy

Once you have your local copy, you can create your own branches, modify code, and apply your changes back. Let’s explore a few commands to familiarise ourselves with Git usage. To list all branches in the current repository, issue the command given below:

$ git branch

The current branch will have a * before it. The command git status also shows the current branch. To create a new branch, and make it the working branch, use the following command:

$ git checkout -b new_branch

To jump from the current branch to a new branch, use the code given below:

$ git checkout new_branch

To change code and commit it into our local repository, the general steps are as follows:

$ cd linux-2.6
## enter the repository
## do some changes in the files
$ git commit -a

When you commit changes, Git will prompt you to enter a commit edit message (in vim). Once you enter one and save it, the changes are committed.

Some more common commands and their purpose:

  • git log — To view a log of commits. Your most recent commit will be the top-most.
  • git log -p — Shows the difference in the form of patches.
  • git add <file-name> — This is used to add new files to the repository.
  • git rm < file> — This is to remove files. You should not use the regular rm command directly from within the repository.
  • git log master..<current_branch> — Shows the difference between the current branch and the master branch. Instead of current_branch you can also use HEAD.
  • git log master..Head <your-recently-changed-file> — To see the differences in a file that has recently been changed.
  • git diff — Shows all changes; can be redirected to a patch file and sent out. This should be used before a git commit.
  • git gc — Optimises the repository; helps boost performance.

If you have done your changes in a different branch and want to merge them into the master branch, you can try the following command:

$git checkout master ## first enter into the master branch
$ git merge new_branch ## then issue merge

If you have done a lot of changes, but not performed a commit yet, and want to flush them off the branch, try the command that follows:

$git checkout -f

If you have already committed, and want to get rid of the commit, use the following code;

$ git reset HEAD~1 ## last commit is removed

Contributing to Git development

Git’s core development is done with Git itself. It is still in active development, and hence commits are actively pushed. The latest commits can be viewed on the Web at git://git.kernel.org/pub/scm/git/git.git. You can clone the latest copy with the following command:

$ git clone git://git.kernel.org/pub/scm/git/git.git

If you think you can contribute to Git development, clone your copy, and start working on it!

This article covers the most commonly used set of Git commands. For more information and advanced usage, refer to The Git Community Book. It is not an absolute must that you use Git to do kernel development, but like any other tool, it helps you speed up your work, and allows you to concentrate on the actual patch rather than on how to create a patch.

4 COMMENTS

  1. Can some one please tell me how to create clone form downloading source code “linux-3.12.2.tar.xz” file.. i am new to this and my internet ll take days to download d clone by using “git clone” command..

LEAVE A REPLY

Please enter your comment!
Please enter your name here