Amending a patch in git

From a co-worker:

amend is new to me… will the updated patch be a full patch to the original source or a patch to the previous patch?

Here’s how I explain it.

in git, nothing is ever lost, but you can rewrite history
if you do git commit (no args) it will create a new commit, and that will be HEAD fro whatever branch you are on
if you then edit a file, and do

git add 
git commit --amend

it will modify that last commit with the changes
however, your original commit is still safely tucked away deep in the bowels of git.
To find it, run

 git reflog

and you will see a transaction log of everything you have ever done in that git repo/

Let me show you how this works.

 cd /opt/stack/dogtag/pki/
 #that is my repo
 git fetch;  git rebase origin/master

I am going to make a simple commit, for the README file

diff --git a/README b/README
index 2c2c36b..214c774 100644
--- a/README
+++ b/README
@@ -1,5 +1,5 @@
 # BEGIN COPYRIGHT BLOCK
-# (C) 2008 Red Hat, Inc.
+# (C) 2008-13 Red Hat, Inc.
 # All rights reserved.
 # END COPYRIGHT BLOCK

After I make that change, but before I commit:
git reflog | fpaste gives me http://paste.fedoraproject.org/92947/13970643

9738598 HEAD@{0}: rebase finished: returning to refs/heads/master
9738598 HEAD@{1}: rebase: checkout origin/master
7a02522 HEAD@{2}: clone: from ssh://git.fedorahosted.org/git/pki.git

If I execute

  git add README
  git commit -m "Updated Copyright on README"

git reflog shows

 
2c7b42d HEAD@{0}: commit: Updated Copyright on README
9738598 HEAD@{1}: rebase finished: returning to refs/heads/master
9738598 HEAD@{2}: rebase: checkout origin/master
7a02522 HEAD@{3}: clone: from ssh://git.fedorahosted.org/git/pki.git

Oops. I realize I made the date 2013 not 2014. Fix that.

 
vi README 
git add README
git commit --amend

git reflog | fpaste now shows

cad8fed HEAD@{0}: commit (amend): Updated Copyright on README
2c7b42d HEAD@{1}: commit: Updated Copyright on README
9738598 HEAD@{2}: rebase finished: returning to refs/heads/master
9738598 HEAD@{3}: rebase: checkout origin/master
7a02522 HEAD@{4}: clone: from ssh://git.fedorahosted.org/git/pki.git

Now, for some wonky reason, I want that -13 edit back
but…lets say I don’t want to lose what I was working on

 git checkout -b update-copyright

that will check out a new branch named update-copyright
It happens that I forget to create a branch before doing work.
So now my master also has that commit on it….I’ll fix that first

 git checkout master

shows

Your branch is ahead of ‘origin/master’ by 1 commit.
(use “git push” to publish your local commits)

nah…I am not ready to push….so

 git reset --hard origin/master

now my master matches origin/master
how can I tell? Couple ways…easiest is:

git log shows my top commit is 9738598e37effc5f68e8f2d211a6273b8846a6fc

and

git log origin/master shows the exact same thing. Now, what about reflog?

9738598 HEAD@{0}: reset: moving to origin/master
cad8fed HEAD@{1}: checkout: moving from update-copyright to master
cad8fed HEAD@{2}: checkout: moving from master to update-copyright
cad8fed HEAD@{3}: commit (amend): Updated Copyright on README
2c7b42d HEAD@{4}: commit: Updated Copyright on README
9738598 HEAD@{5}: rebase finished: returning to refs/heads/master
9738598 HEAD@{6}: rebase: checkout origin/master
7a02522 HEAD@{7}: clone: from ssh://git.fedorahosted.org/git/pki.git

It shows every one of those moves. And that original commit, where I put -13?
that is

2c7b42d HEAD@{4}: commit: Updated Copyright on README

if I look at it with git show 2c7b42d

commit 2c7b42dde010848f2b60e0f585701fe2ef76e732
Author: Adam Young 
Date:   Wed Apr 9 13:26:28 2014 -0400

    Updated Copyright on README

diff --git a/README b/README
index 2c2c36b..214c774 100644
--- a/README
+++ b/README
@@ -1,5 +1,5 @@
 # BEGIN COPYRIGHT BLOCK
-# (C) 2008 Red Hat, Inc.
+# (C) 2008-13 Red Hat, Inc.
 # All rights reserved.
 # END COPYRIGHT BLOCK

Leave a Reply

Your email address will not be published. Required fields are marked *