Basic Git usage

From MultimediaWiki
Revision as of 09:36, 26 January 2008 by Superdump (talk | contribs) (Initial document (NEEDS MORE WORK))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Initial checkout:

git-clone git:// ffmpeg-git



Make some changes, revert to head on master branch:

git-reset --hard HEAD

Make a branch:

git-branch <name of branch>

Switch to a branch (make it the active branch such that source files reflect the source in that branch):

git-checkout <name of branch>

Delete a branch:

git-branch -D <name of branch>

Working on a series of incremental, dependent patches

Edit some files making functional changes. Commit changes locally - prompted for a commit message using $EDITOR to write it

git-commit -a

Edit some files making cosmetic changes and commit again.

git-commit -a

Prepare patches for e-mail submission (this creates the patches numbered and in order for easy submission):

git-format-patch origin

Generate a diff of branch 2 against branch 1 (i.e. branch 1 + patch = branch 2)

git-diff <name of branch 1> <name of branch 2>

Apply a git-diff style patch in a working Git tree:

git-am foo.patch

Apply a git-diff style patch in any working directory containing the correct source files to be patched:

git-apply foo.patch

(Note: git-am will create a new commit using the extra metadata for the commit message, etc. whereas git-apply just patches the files)

Revert the last n (where n is a positive int) commits from the active branch:

git-reset --hard HEAD~n


When submitting a set of patches, it is common to want to go back and edit one of the intermediate commits depending on feedback for the patches. This process involves something like:

  • Revert to the commit before the one that needs to be edited.
  • Apply the patch without committing, edit the code.
  • Commit the new code, make a new patch.
  • Merge subsequent commits (the patches that followed).

People have suggested using 'quilt' but I wanted to use Git to see if it could handle such a process. it has been suggested that the following approach should work:

HEAD is the state of the code after application of all the patches (i.e. when you ran git-format-patches origin) so we want to revert to the commit before the patch that needs to be edited. You could make a new branch at version HEAD - n:

git-branch <name of branch> HEAD~n

Then switch to that branch:

git-checkout <name of branch>

Reapply the patch without committing (using git-apply, not git-am), edit the code appropriately, then commit again. Then we want to merge in (some of?) the subsequent commits. We can cherry pick the HEAD-n th commit from another branch (not preserving history!) as follows:

git-cherry-pick <name of branch>~n

(Note: cherry pick can only apply to a clean branch, that is where there are no alterations since the HEAD commit)

Maybe there is a better procedure but this seems to make sense, even if it seems a little cumbersome.