Applying patches

Now we know how to create patches from commits. It is time to learn to apply them.

Getting ready

We'll use the repository from the previous examples along with the generated patches as follows:

$ cd offline-sharing 
$ git checkout master
$ ls -1a
.
..
.git
Makefile
README.md
another_pi.c
latest-commit
math.c
not-on-master

How to do it...

First, we'll checkout the develop branch and apply the patch generated from the master branch (0001-Calculate-pi-with-more-digits.patch) in the first example. We use the Git command am to apply the patches; am is short for apply from mailbox:

$ git checkout develop
Your branch is up-to-date with 'origin/develop'.
$ git am latest-commit/0001-Calculate-pi-with-more-digits.patch
Applying: Calculate pi with more digits

We can also apply the master branch to the series of patches that was generated from the develop branch as follows:

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git am not-on-master/*
Applying: Move print functionality of is_prime
Applying: Adds Makefile for easy building
Applying: Adds functionality to prime-test a range of numbers

How it works…

The git am command takes the mailbox file specified in the input and applies the patch in the file to the files needed. Then, a commit is recorded using the commit message and author information from the patch. The committer identity of the commit will be the identity of the person performing the git am command. We can see the author and committer information with git log, but we need to pass the --pretty=fuller option to also view the committer information:

$ git log -1 --pretty=fuller
commit 5af5ee2746b67893b0550d8a63110c48fd1b667c
Author:     Aske Olsson <[email protected]>
AuthorDate: Wed Apr 9 21:50:18 2014 +0200
Commit:     Aske Olsson <[email protected]>
CommitDate: Fri Jun 13 22:50:45 2014 +0200

    Adds functionality to prime-test a range of numbers

There's more…

The git am command applies the patches in the files specified and records the commits in the repository. However, if you only want to apply the patch to the working tree or the staging area and not record a commit, you can use the git apply command.

We can try to apply the patch from the master branch to the develop branch once again; we just need to reset the develop branch first:

$ git checkout develop
Switched to branch 'develop'
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)
$ git reset --hard origin/develop
HEAD is now at c131c8b Adds functionality to prime-test a range of numbers
$ git apply latest-commit/0001-Calculate-pi-with-more-digits.patch

Now, we can check the state of the repository with the status command:

$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  another_pi.c
  latest-commit/
  not-on-master/

nothing added to commit but untracked files present (use "git add" to track)

We successfully applied the patch to the working tree. We can also apply it to the staging area and the working tree using the --index option, or only to the staging area using the --cached option.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset