Kohei Nozaki's blog 

Entries tagged [git]

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.


Discard all changes and sync to the HEAD of the repository


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


Assume this:

kyle-no-MacBook:gitprac5 kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  5  3  9 09:40 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$ 

Wasting working tree

kyle-no-MacBook:gitprac5 kyle$ echo hoge >> hoge.txt
kyle-no-MacBook:gitprac5 kyle$ echo untracked > untracked.txt
kyle-no-MacBook:gitprac5 kyle$ echo tracked > tracked.txt
kyle-no-MacBook:gitprac5 kyle$ git add tracked.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   tracked.txt
#
# 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:   hoge.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   untracked.txt
kyle-no-MacBook:gitprac5 kyle$ ls -l
total 24
-rw-r--r--+ 1 kyle  staff  10  3  9 09:45 hoge.txt
-rw-r--r--+ 1 kyle  staff   8  3  9 09:45 tracked.txt
-rw-r--r--+ 1 kyle  staff  10  3  9 09:45 untracked.txt
kyle-no-MacBook:gitprac5 kyle$ 

Do it

  • These 2 commands will work:
git reset --hard HEAD
git clean -f
  • Let's do it:
kyle-no-MacBook:gitprac5 kyle$ git reset --hard HEAD
HEAD is now at 31e0ee6 initial
kyle-no-MacBook:gitprac5 kyle$ ls -l
total 16
-rw-r--r--+ 1 kyle  staff   5  3  9 09:46 hoge.txt
-rw-r--r--+ 1 kyle  staff  10  3  9 09:45 untracked.txt
kyle-no-MacBook:gitprac5 kyle$ git clean -f
Removing untracked.txt
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$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 
  • When you want to delete directories, you can use “git clean -fd”.