Kohei Nozaki's blog 

Using rebase


Posted on Sunday Mar 09, 2014 at 08:17PM in Technology


Make multi commits to one commit with rebase

Commit

kyle-no-MacBook:hello kyle$ echo commit1 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'commit1'
[b4 2447554] commit1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ echo commit2 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'commit2'
[b4 50e8dee] commit2
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git log -2
commit 50e8deed471c20abc9b86eb1cb7d6b6af4c9fcfd
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:23 2014 +0900

    commit2

commit 2447554c8495ff5f407b4dffb84278840d7fdba6
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:15 2014 +0900

    commit1

Rebase

  • Exec this (number 2 means how many commits to be one commit):
git rebase -i HEAD~2
  • Editor launches with this text:
pick 2447554 commit1
pick 50e8dee commit2

# Rebase 9ccae64..50e8dee onto 9ccae64
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  • Edit to:
pick 2447554 commit1
squash 50e8dee commit2
  • Another editor will launch so that we can edit commit message:
# This is a combination of 2 commits.
# The first commit's message is:

commit1

# This is the 2nd commit message:

commit2

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached at 2447554
# You are currently editing a commit while rebasing branch 'b4' on '9ccae64'.
#
# Changes to be committed:
#   (use "git reset HEAD^1 <file>..." to unstage)
#
#       modified:   README.md
#
  • Gone like this:
kyle-no-MacBook:hello kyle$ git rebase -i HEAD~2
[detached HEAD ada1147] commit1
 1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/b4.
kyle-no-MacBook:hello kyle$ 
  • Log:
kyle-no-MacBook:hello kyle$ git log -2
commit ada114798c5727e8461afc36a10ad0b36e5e214a
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:15 2014 +0900

    commit1

    commit2

commit 9ccae643e8ceee10c5f7bcb4103857bc1a38cdd4
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 19:09:56 2014 +0900

    cancel
kyle-no-MacBook:hello kyle$ 
  • README.md
kyle-no-MacBook:hello kyle$ cat README.md 
b4-local1
CANCEL
commit1
commit2
kyle-no-MacBook:hello kyle$ 


Cancelling commit and push


Posted on Sunday Mar 09, 2014 at 05:17PM in Technology


Cancel a commit

kyle-no-MacBook:hello kyle$ echo CANCEL >> README.md 
kyle-no-MacBook:hello kyle$ git commit -am 'cancel'
[b4 8c2027b] cancel
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ 
  • Cancel:
kyle-no-MacBook:hello kyle$ git reset HEAD^
Unstaged changes after reset:
M   README.md
kyle-no-MacBook:hello kyle$ git status
# On branch b4
# 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:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:hello kyle$ git log
commit 75dc62abde6e533a5aef8ecab248ed4f3bbfe43a
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 17:40:57 2014 +0900

    local1-b4
  • If you want to commit again, just do this:
git commit -am 'cancel'

Cancel a push

kyle-no-MacBook:hello kyle$ git push
warning: push.default is unset; its implicit value is changing in
...
Counting objects: 10, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 498 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   bdbe00c..c107ca9  b1 -> b1
   75dc62a..9ccae64  b4 -> b4
   059de38..30254fe  master -> master
kyle-no-MacBook:hello kyle$ 
  • Now b4 is:

  • Cancelling:
kyle-no-MacBook:hello kyle$ git push -f origin HEAD^:b4
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
 + 9ccae64...75dc62a HEAD^ -> b4 (forced update)
kyle-no-MacBook:hello kyle$ 
  • Roll-backed:

References

  1. git reset についてもまとめてみる - murankの日記


Using remote branches: Push and Pull


Posted on Sunday Mar 09, 2014 at 04:52PM in Technology


Preparation: make 2 clones at local

kyle-no-MacBook:remotebranchprac kyle$ mkdir local1
kyle-no-MacBook:remotebranchprac kyle$ mkdir local2
kyle-no-MacBook:remotebranchprac kyle$ cd local1
kyle-no-MacBook:local1 kyle$ git clone git@github.com:lbtc-xxx/hello.git
Cloning into 'hello'...
remote: Reusing existing pack: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
Checking connectivity... done
kyle-no-MacBook:local1 kyle$ cd ../local2
kyle-no-MacBook:local2 kyle$ git clone git@github.com:lbtc-xxx/hello.git
Cloning into 'hello'...
remote: Reusing existing pack: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
Checking connectivity... done
kyle-no-MacBook:local2 kyle$ 

Push from local1

  • First, modify and commit
kyle-no-MacBook:hello kyle$ echo hello-iam-local1 >> README.md 
kyle-no-MacBook:hello kyle$ git commit -am 'commit at local1'
[master e4ef489] commit at local1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ 
  • Push
kyle-no-MacBook:hello kyle$ git remote -v
origin  git@github.com:lbtc-xxx/hello.git (fetch)
origin  git@github.com:lbtc-xxx/hello.git (push)
kyle-no-MacBook:hello kyle$ git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   9eb01ce..e4ef489  master -> master
kyle-no-MacBook:hello kyle$ 
  • Now we can see the change from GitHub.

Fetch and merge from local2

kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ git remote -v
origin  git@github.com:lbtc-xxx/hello.git (fetch)
origin  git@github.com:lbtc-xxx/hello.git (push)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ git fetch origin
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:lbtc-xxx/hello
   9eb01ce..e4ef489  master     -> origin/master
kyle-no-MacBook:hello kyle$ 
  • But the file is not updated yet.
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ 
  • We have to merge it manually.
kyle-no-MacBook:hello kyle$ git merge origin/master 
Updating 9eb01ce..e4ef489
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
kyle-no-MacBook:hello kyle$ 
  • Logs are updated too.
kyle-no-MacBook:hello kyle$ git log
commit e4ef4897880924b430721186b36212b32ef5cf12
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:59:00 2014 +0900

    commit at local1

Pull

  • Do some work at local2
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ echo hello-iam-local2 >> README.md 
kyle-no-MacBook:hello kyle$ git commit -am 'commit at local2'
[master 059de38] commit at local2
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 277 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   e4ef489..059de38  master -> master
kyle-no-MacBook:hello kyle$ 
  • Pull at local1
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:lbtc-xxx/hello
   e4ef489..059de38  master     -> origin/master
Updating e4ef489..059de38
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
hello-iam-local2
kyle-no-MacBook:hello kyle$ 

Push a branch from local1

kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ git checkout -b b1
Switched to a new branch 'b1'
kyle-no-MacBook:hello kyle$ git remote -v
origin  git@github.com:lbtc-xxx/hello.git (fetch)
origin  git@github.com:lbtc-xxx/hello.git (push)
kyle-no-MacBook:hello kyle$ git push origin b1
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
 * [new branch]      b1 -> b1
kyle-no-MacBook:hello kyle$ 
  • Not we can see an new branch in GitHub

Pull a branch at local2

kyle-no-MacBook:hello kyle$ cd ../../local2/hello
kyle-no-MacBook:hello kyle$ git branch
* master
kyle-no-MacBook:hello kyle$ git pull
From github.com:lbtc-xxx/hello
 * [new branch]      b1         -> origin/b1
Already up-to-date.
  • But we can't see any branches yet.
kyle-no-MacBook:hello kyle$ git branch
* master
  • This makes it appear.
kyle-no-MacBook:hello kyle$ git checkout -b b1 origin/b1
Branch b1 set up to track remote branch b1 from origin.
Switched to a new branch 'b1'
kyle-no-MacBook:hello kyle$
  • Do some work at b1 branch at local2, then push
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ echo hello-iam-local2-b1 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'local2-b1'
[b1 bdbe00c] local2-b1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git push
warning: push.default is unset; its implicit value is changing in
...
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   059de38..bdbe00c  b1 -> b1
kyle-no-MacBook:hello kyle$ 
  • fetch and merge at local1
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ git fetch origin
kyle-no-MacBook:hello kyle$ git branch
* b1
  master
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
hello-iam-local2
kyle-no-MacBook:hello kyle$ git merge origin/b1
Updating 059de38..bdbe00c
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
hello-iam-local1
hello-iam-local2
hello-iam-local2-b1
kyle-no-MacBook:hello kyle$ 

checkout --remote

Create a branch and push

kyle-no-MacBook:hello kyle$ git branch b4
kyle-no-MacBook:hello kyle$ git checkout b4
Switched to branch 'b4'
kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local1/hello
kyle-no-MacBook:hello kyle$ echo b4-local1 > README.md 
kyle-no-MacBook:hello kyle$ git commit -am 'local1-b4'
[b4 75dc62a] local1-b4
 1 file changed, 1 insertion(+), 6 deletions(-)
kyle-no-MacBook:hello kyle$ git push origin b4
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 503 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
 * [new branch]      b4 -> b4
kyle-no-MacBook:hello kyle$ 

fetch and checkout

kyle-no-MacBook:hello kyle$ pwd
/Users/kyle/tmp/remotebranchprac/local2/hello
kyle-no-MacBook:hello kyle$ git fetch
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 1), reused 6 (delta 1)
Unpacking objects: 100% (6/6), done.
From github.com:lbtc-xxx/hello
 * [new branch]      b4         -> origin/b4
kyle-no-MacBook:hello kyle$ git checkout --track origin/b4
Branch b4 set up to track remote branch b4 from origin.
Switched to a new branch 'b4'
kyle-no-MacBook:hello kyle$ cat README.md 
b4-local1
kyle-no-MacBook:hello kyle$ 

Listing remote branches

kyle-no-MacBook:hello kyle$ git branch -r
  origin/HEAD -> origin/master
  origin/b1
  origin/b2
  origin/b3
  origin/b4
  origin/master
kyle-no-MacBook:hello kyle$ 


Using Branches


Posted on Sunday Mar 09, 2014 at 04:01PM in Technology


Create a branch

git branch [BRANCH_NAME]
  • It goes like this:
kyle-no-MacBook:hello kyle$ git branch
* master
kyle-no-MacBook:hello kyle$ git branch branch1
kyle-no-MacBook:hello kyle$ git branch
  branch1
* master
  • We are still in master branch.
  • To change the branch to use as working tree:
kyle-no-MacBook:hello kyle$ git checkout branch1
Switched to branch 'branch1'
kyle-no-MacBook:hello kyle$ git branch
* branch1
  master
kyle-no-MacBook:hello kyle$ 
  • There's a shortcut:
kyle-no-MacBook:hello kyle$ git checkout -b branch2
Switched to a new branch 'branch2'
kyle-no-MacBook:hello kyle$ git branch
  branch1
* branch2
  master
kyle-no-MacBook:hello kyle$ 

Make an commit at an branch

kyle-no-MacBook:hello kyle$ echo fuge >> README.md 
kyle-no-MacBook:hello kyle$ git status
# On branch branch1
# 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:   README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:hello kyle$ git commit -a
[branch1 114ec77] fuge
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
kyle-no-MacBook:hello kyle$ 

Back to master branch

kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
kyle-no-MacBook:hello kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ 
  • No changes are made to master branch.

Merge to master branch

kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
kyle-no-MacBook:hello kyle$ git merge branch1
Updating 9eb01ce..114ec77
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
kyle-no-MacBook:hello kyle$
  • If you have another branch that created from the master branch, you'll need to merge for it too.
kyle-no-MacBook:hello kyle$ git checkout branch2
Switched to branch 'branch2'
kyle-no-MacBook:hello kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  27  3  9 16:20 README.md
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ git merge master
Updating 9eb01ce..114ec77
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
kyle-no-MacBook:hello kyle$ 

Delete an branch

kyle-no-MacBook:hello kyle$ git branch -d branch1
Deleted branch branch1 (was 114ec77).
kyle-no-MacBook:hello kyle$ 

Resolve a conflict

  • Create 2 branches from the master.
kyle-no-MacBook:hello kyle$ git checkout -b branch1
Switched to a new branch 'branch1'
kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
kyle-no-MacBook:hello kyle$ git checkout -b branch2
Switched to a new branch 'branch2'
kyle-no-MacBook:hello kyle$ 
  • Commit to each branches that will makes conflict
kyle-no-MacBook:hello kyle$ echo branch1 >> README.md
kyle-no-MacBook:hello kyle$ git commit -a -m 'branch1'
[branch1 2a74c5b] branch1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git checkout branch2
Switched to branch 'branch2'
kyle-no-MacBook:hello kyle$ echo branch2 >> README.md
kyle-no-MacBook:hello kyle$ git commit -a -m 'branch2'
[branch2 f44a522] branch2
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ 
  • Make an attempt to merge both branches
kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
kyle-no-MacBook:hello kyle$ git merge branch1
Updating 114ec77..2a74c5b
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git merge branch2
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
kyle-no-MacBook:hello kyle$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#   both modified:      README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:hello kyle$ 
  • README.md is below now:
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
<<<<<<< HEAD
branch1
=======
branch2
>>>>>>> branch2
kyle-no-MacBook:hello kyle$ 
  • So we have to edit it to resolve conflict manually:
kyle-no-MacBook:hello kyle$ vi README.md
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
branch1 & branch2
kyle-no-MacBook:hello kyle$
  • After you done edit, then you can add & commit.
  • Add tells git that you have done resolving.
kyle-no-MacBook:hello kyle$ git commit -a -m 'resolve conflict'
[master 04c3da3] resolve conflict
kyle-no-MacBook:hello kyle$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
kyle-no-MacBook:hello kyle$ git log
commit 04c3da3496f6acc3acce525df855e5173b93f9d7
Merge: 2a74c5b f44a522
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:32:35 2014 +0900

    resolve conflict

commit f44a5224c39a6df457eb37a6c77fe593de8b5643
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:28:32 2014 +0900

    branch2

commit 2a74c5b1eebf19717212b6a75e540ba88591bfdb
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:28:19 2014 +0900

    branch1

Check the latest commit of each branches

kyle-no-MacBook:hello kyle$ git branch -v
  branch1 2a74c5b branch1
  branch2 f44a522 branch2
* master  04c3da3 [ahead 4] resolve conflict
kyle-no-MacBook:hello kyle$ 
  • This shows branches that already merged
kyle-no-MacBook:hello kyle$ git branch --merged
  branch1
  branch2
* master
kyle-no-MacBook:hello kyle$ 
  • This shows branches that not merged yet
kyle-no-MacBook:hello kyle$ git branch --no-merged 
  branch2
  master
kyle-no-MacBook:hello kyle$ 


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.