Skip to content

Git Tutorial

erasmux edited this page Jul 28, 2012 · 3 revisions

This page is aimed at git/github newbies or those who wish to familiarize themselves with the basic git commands. If you already know git well, you don’t need this page. Let me know if you find errors or something important that I missed here. Owners of other kernels, may also find the cherry picking item useful (see end of this page).

This tutorial will loosely draw guidelines for forking my kernel and the git steps useful of developing your own kernel tweaks and fixes. The commands themselves are of course general and useful for all git projects.

I assume you have already registered github, including generating the SSH keys and updating them on github.

To fork this kernel simply click on the “Fork” button on the flykernel repository. This will create a copy of the repository in your github which you can pull to your computer and work on. After you have some changes of your own you can push them back to the github servers where others can see them.

I will detail the basic git commands which I find most useful:

  • To pull a repository to your computer use git clone, for example to get the repository you forked:
    git clone github.com:<your_username>/hero-2.6.29-flykernel.git
    After this you’ll have all the files locally and you can work on them regularly.
  • git status
    The most useful command, shows you what files where changed, which files aren’t being tracked, etc. Run it before doing commits and such to understand exactly what your about to commit.
  • git diff
    Shows the diffs on all files that have changed. You can also specify files and revisions:
    git diff arch/arm/configs/hero_defconfig
    git diff d8ddd9d drivers/cpufreq/cpufreq_smartass.c
    (where d8ddd9d is the start of a commit id).
  • git log
    Shows your recent commits.
    Some flags I find useful:
    git log --pretty=oneline --author=eran
  • git commit
    Commits your changes.
    You need to either specify a file list:
    git commit arch/arm/mach-msm/include/mach/board.h arch/arm/mach-msm/include/mach/camera.h include/media/msm_camera.h
    Or, to commit all files with changes, use: (do a “git status” first to see which files you are about to commit)
    git commit -a
    Additionally, if you don’t add a: -m "my commit message"
    your default text editor will open to edit the commit message.
  • git checkout
    Restore the state of a file. For example to undo the (uncommited) changes in a file:
    git checkout arch/arm/configs/hero_defconfig
    You can also specific different versions, for example:
    git checkout d8ddd9d drivers/cpufreq/cpufreq_smartass.c
    will checkout the version after commit d8ddd9d of the file. Note that after you such a checkout, git remembers you are looking at that version of the file. A git diff will now show the diffs against that version. Use git diff HEAD to see the diffs against your head.
  • git reset --hard HEAD
    Reverts all changes, in all tracked files, and restores their state after the last commit (HEAD).
    To revert changes you already commit use git revert – see below.
    Advanced note: You can also use git reset --hard <commit> to completely erase all commits after the given commit . But be very careful with this and NEVER do this to a commit that exists in another place (i.e. something you already pushed to github)! For example git reset --hard HEAD^ will completely erase the last commit. Be very very careful with this, it can not be undone and again NEVER do it to a commit you already pushed.
  • git revert <commit_id>
    Creates a new commit, which reverses the given commit. For example:
    git revert HEAD
    will undo the last commit.
  • git push origin master --tags
    Pushes your latest commits on branch “master” to the remote github name “origin”.
    If “origin” does not work for you, do a “git remote -v” and see what your github remote is named.
    After this you should see your changes in the github web interface as well.
  • Finally to grab upstream changes: (works only from repositories you are either a direct or indirect fork of, for unrelated repositories see cherry picking below)
    First, you should add the upstream github and give it a name: (you only need to do this once)
    git remote add erasmux git://github.com/erasmux/hero-2.6.29-flykernel.git
    Now to fetch and merge upstream changes do a:
    git pull erasmux master
    where “erasmux” is the remote name we gave the repository and “master” is the branch name.
  • Cherry picking
    Cherry picking, is a bit more advanced but very useful if you want to pull a change from a totally unrelated repository (i.e. kernel for a different device). For example, if we want to pull a commit from the cyanogen kernel. First we need to add the repository: (only done once):
    git remote add cm git://github.com/CyanogenMod/cm-kernel.git
    Then fetch it:
    git fetch cm
    The fetch command will only bring his changes to the locally (hidden in the .git dir), but will not make any changes to your files. Now you can address the branches in this repository in git commands. For example:
    git log cm/android-msm-2.6.35-unified --pretty=oneline
    git diff cm/android-msm-2.6.35-unified arch/arm/mach-msm/smd_rpcrouter.c
    Finally, to get only a specific commit, we can use the cherry picking:
    git cherry-pick 55bd292e0
    Note that the commit id is unique, hence it also implicitly specifies the repository and branch to get the commit from.
    If there are conflicts, it will not commit and let you fix the conflicts first. You can add the " —no-commit" flag to prevent commit even if there aren’t conflicts.

There is much more to git, and most “basic” commands have many flags. Try man and google if you have specific problems.

Clone this wiki locally