Kohei Nozaki's blog 

Amend commit and push


Posted on Sunday Mar 09, 2014 at 09:50AM in Technology


Assume this:

kyle-no-MacBook:gitprac5 kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  5  3  9 09:46 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ cat hoge.txt
hoge
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 

Committing with forgotten operation

kyle-no-MacBook:gitprac5 kyle$ echo hogehoge >> hoge.txt
kyle-no-MacBook:gitprac5 kyle$ echo forgotten > fotgotten.txt
kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git commit -m 'second'
[master addf0f0] second
 1 file changed, 1 insertion(+)
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   forgotten.txt
nothing added to commit but untracked files present (use "git add" to track)
kyle-no-MacBook:gitprac5 kyle$ git log
commit addf0f0475267dbb18d89b994412da597098077f
Author: kyle <kyle@example.com>
Date:   Sun Mar 9 09:54:55 2014 +0900

    second

commit 6db0e3d793e9f79621093f6cab302611d7d55013
Author: kyle <kyle@example.com>
Date:   Sun Mar 9 09:54:10 2014 +0900

    initial
kyle-no-MacBook:gitprac5 kyle$ 

Amend

kyle-no-MacBook:gitprac5 kyle$ git add forgotten.txt 
kyle-no-MacBook:gitprac5 kyle$ git commit --amend
  • The editor runs
  • We can modify commit message and overwrite last commit. I add “revised” to bottom of the message.
  • After edit&save and quit the editor, this goes like this:
kyle-no-MacBook:gitprac5 kyle$ git commit --amend
[master 631c895] second revised
 2 files changed, 2 insertions(+)
 create mode 100644 forgotten.txt
kyle-no-MacBook:gitprac5 kyle$ git log
commit 631c8958432a476ebc287c3a2de0566424b483eb
Author: kyle <kyle@example.com>
Date:   Sun Mar 9 09:54:55 2014 +0900

    second revised

commit 6db0e3d793e9f79621093f6cab302611d7d55013
Author: kyle <kyle@example.com>
Date:   Sun Mar 9 09:54:10 2014 +0900

    initial
kyle-no-MacBook:gitprac5 kyle$ 

Push

  • We can push that amend commit to remote branch.
git push -f origin [BRANCH_NAME]
  • This goes like this:
kyle-no-MacBook:jboss-logmanager kyle$ git status
# On branch LOGMGR-104
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   src/main/java/org/jboss/logmanager/handlers/SyslogHandler.java
#   modified:   src/test/java/org/jboss/logmanager/handlers/SyslogHandlerTests.java
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:jboss-logmanager kyle$ git add src/main/java/org/jboss/logmanager/handlers/SyslogHandler.java
kyle-no-MacBook:jboss-logmanager kyle$ git add src/test/java/org/jboss/logmanager/handlers/SyslogHandlerTests.java
kyle-no-MacBook:jboss-logmanager kyle$ git status
# On branch LOGMGR-104
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   src/main/java/org/jboss/logmanager/handlers/SyslogHandler.java
#   modified:   src/test/java/org/jboss/logmanager/handlers/SyslogHandlerTests.java
#
kyle-no-MacBook:jboss-logmanager kyle$ git commit --amend
[LOGMGR-104 262d02f] [LOGMGR-104] SyslogHandler doesn't handle multi-byte characters correctly
 Author: lbtc_xxx <lbtc-xxx@example.com>
 2 files changed, 83 insertions(+), 11 deletions(-)
kyle-no-MacBook:jboss-logmanager kyle$ git push -f origin LOGMGR-104
Counting objects: 30, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (17/17), 2.38 KiB | 0 bytes/s, done.
Total 17 (delta 5), reused 0 (delta 0)
To git@github.com:lbtc-xxx/jboss-logmanager.git
 + ab3033a...262d02f LOGMGR-104 -> LOGMGR-104 (forced update)
kyle-no-MacBook:jboss-logmanager kyle$ 
  • If you have a pull request, it will be updated together.
  • It makes some troubles on some cases such as very active repository so we have to be careful when use this way.



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed