困ったときのGit

## git add を取り消す
- git reset HEAD "file-name"

```
[root@Ogiman ogiman_repo]# git status
# On branch test-branch2
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test
#

[root@Ogiman ogiman_repo]# git reset HEAD test
Unstaged changes after reset:
M       test

[root@Ogiman ogiman_repo]# git status
# On branch test-branch2
# 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:   test
#
no changes added to commit (use "git add" and/or "git commit -a")
```
## git commit を取り消す　
 - git reset --soft HEAD^
 - 「git reset --soft」 → ワークディレクトリの内容はそのままでコミットだけを取り消す。 
        - [HEAD^]はgit log の最新のlogを意味する

```ruby:git-reset-soft
[root@Ogiman ogiman_repo]# git log
commit c7f11dbe4805771567a93d7eadcc2f2c3d9d2f94
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 22:02:53 2017 +0900

    fourth commit
[root@Ogiman ogiman_repo]# git reset --soft HEAD^

[root@Ogiman ogiman_repo]# git status
# On branch test-branch2
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test
#

[root@Ogiman ogiman_repo]# git log
commit f519176b2bcf7193937c41f80af454203e3aec86
Merge: f6d8f00 b461e72
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 21:53:21 2017 +0900

    Merge branch 'master' into test-branch2

    Conflicts:
        test
``` 
 - git reset --soft HEAD^
 - 「git reset --hard」 → コミット取り消した上でワークディレクトリの内容も書き換える。
        - [HEAD^]はgit log の最新のlogを意味する

``` ruby:git-reset-hard
[root@Ogiman ogiman_repo]# git log
commit 5b204621425eee417bd3fb628727f64be0caa33b
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 22:08:44 2017 +0900

    fourth commit

[root@Ogiman ogiman_repo]# git reset --hard HEAD^
HEAD is now at f519176 Merge branch 'master' into test-branch2

[root@Ogiman ogiman_repo]# git log
commit f519176b2bcf7193937c41f80af454203e3aec86
Merge: f6d8f00 b461e72
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 21:53:21 2017 +0900

    Merge branch 'master' into test-branch2

    Conflicts:
        test
[root@Ogiman ogiman_repo]# git status
# On branch test-branch2
nothing to commit, working directory clean
```
## 指定したIDからHEADまでのgit commit を取り消す
- git reset --[soft|hard] "commit-ID"

```
[root@Ogiman ogiman_repo]# git log
commit 45bdf1058d33fb7203ab0732e9602eb34f037072
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 22:26:24 2017 +0900

    2

commit 639afb4cb46b2bc046786d23306f63f262aedd04
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 22:25:56 2017 +0900

    1

[root@Ogiman ogiman_repo]# git reset --soft 639afb4cb46b2bc046786d23306f63f262aedd04

[root@Ogiman ogiman_repo]# git status
# On branch test-branch2
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   test2
#
```

## 指定したコミットを取り消す
- git revert "commit-ID"

```
[root@Ogiman ogiman_repo]# git log -1
commit dd11ba3ee634054f0c53557b52cfd3c9fd9ca017
Author: ogiman0216 <ogiman@hoge.com>
Date:   Mon Feb 19 23:38:15 2018 +0900

    add test v1
[root@Ogiman ogiman_repo]# git revert dd11ba3ee634054f0c53557b52cfd3c9fd9ca017
[test-branch2 95ce2df] Revert "add test v1"　★commitIDを指定
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@Ogiman ogiman_repo]# git log -1
commit 95ce2dfcc0eb22e2954b5cb0adeef00383af20d0
Author: ogiman0216 <ogiman@hoge.com>
Date:   Mon Feb 19 23:40:05 2018 +0900

    Revert "add test v1"　★rebertの場合、ログが残る

    This reverts commit dd11ba3ee634054f0c53557b52cfd3c9fd9ca017.
[root@Ogiman ogiman_repo]# git push origin test-branch2　★revert内容をpushする
Username for 'https://github.com': ogiman0216
Password for 'https://ogiman0216@github.com':
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 324 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/ogiman0216/ogiman_repo.git
   dd11ba3..95ce2df  test-branch2 -> test-branch2
[root@Ogiman ogiman_repo]#
```


## コンフリクト発生させて、解消させてみる

- testファイルをorogin/masterへpush


```
[root@Ogiman ogiman_repo]# vi test

以下を記載
test

[root@Ogiman ogiman_repo]# git add test
[root@Ogiman ogiman_repo]# git commit
[master 27cad8b] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test
[root@Ogiman ogiman_repo]# git push origin master
```
![FireShot Capture 2 - ogiman0216_ogiman_repo - https___github.com_ogiman0216_ogiman_repo_tree_master.png](https://qiita-image-store.s3.amazonaws.com/0/144316/9407e31a-9038-3142-5832-f69507ba22d2.png)

- masterからtest-branch1を作成

```
[root@Ogiman ogiman_repo]# git checkout -b test-branch1
Switched to a new branch 'test-branch1'

[root@Ogiman ogiman_repo]# git branch
  master
* test-branch1
```

- testファイルを以下に編集

```
[root@Ogiman ogiman_repo]# vi test

以下を記載
test1
```
- 修正後にpushする

```
[root@Ogiman ogiman_repo]# git status
# On branch test-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:   test
#
no changes added to commit (use "git add" and/or "git commit -a")

[root@Ogiman ogiman_repo]# git add test
[root@Ogiman ogiman_repo]# git commit
[test-branch1 8cbbd0b] second commit
 1 file changed, 1 insertion(+), 1 deletion(-)

[root@Ogiman ogiman_repo]# git push origin test-branch1
```
![FireShot Capture 2 - ogiman0216_ogiman_repo - https___github.com_ogiman0216_ogiman_repo_tree_master.png](https://qiita-image-store.s3.amazonaws.com/0/144316/d01d82f7-090a-c31c-0ac5-5c38523fa1c8.png)

- リモートでtest-branch1のpullrequestをmasterへマージ
- branchをmasterへ

```
[root@Ogiman ogiman_repo]# git checkout master
Switched to branch 'master'
[root@Ogiman ogiman_repo]# git branch
* master
  test-branch1
```
- masterからtest-branch2を作成

```
[root@Ogiman ogiman_repo]# git checkout -b test-branch2
Switched to a new branch 'test-branch2'
[root@Ogiman ogiman_repo]# git branch
  master
  test-branch1
* test-branch2
```

- testファイルを以下に編集

```
[root@Ogiman ogiman_repo]# vi test

以下を記載
test2
```
- 修正後にpushする

```
[root@Ogiman ogiman_repo]# git status
# On branch test-branch2
# 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:   test
#
no changes added to commit (use "git add" and/or "git commit -a")

[root@Ogiman ogiman_repo]# git add test
[root@Ogiman ogiman_repo]# git commit
[test-branch2 f6d8f00] third commit
 1 file changed, 1 insertion(+), 1 deletion(-)

[root@Ogiman ogiman_repo]# git push origin test-branch2
```
- リモートでtest-branch2のpullrequestをmasterへマージ
- リモートでコンフリクト発生
![FireShot Capture 2 - ogiman0216_ogiman_repo - https___github.com_ogiman0216_ogiman_repo_tree_master.png](https://qiita-image-store.s3.amazonaws.com/0/144316/b3f14e02-4853-9656-c856-7d148c5e0aff.png)

- branch をmasterへ

```
[root@Ogiman ogiman_repo]# git checkout master
Switched to branch 'master'
[root@Ogiman ogiman_repo]# git branch
* master
  test-branch1
  test-branch2
```

- origin/masterをダウンロード

```
[root@Ogiman ogiman_repo]# git fetch
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 2
Unpacking objects: 100% (4/4), done.
From https://github.com/ogiman0216/ogiman_repo
   27cad8b..b461e72  master     -> origin/master 
```

- masterへ取り込む

```
[root@Ogiman ogiman_repo]# git merge origin/master
Updating 27cad8b..b461e72
Fast-forward
 README.md | 1 +
 test      | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 README.md
```
- test-branch2へ

```
[root@Ogiman ogiman_repo]# git checkout test-branch2
Switched to branch 'test-branch2'
[root@Ogiman ogiman_repo]# git branch
  master
  test-branch1
* test-branch2
[root@Ogiman ogiman_repo]# git merge master
Auto-merging test
CONFLICT (content): Merge conflict in test
Automatic merge failed; fix conflicts and then commit the result.
```
- コンフリクト修正

```
[root@Ogiman ogiman_repo]# git diff
diff --cc test
index 180cf83,a5bce3f..0000000
--- a/test
+++ b/test
@@@ -1,1 -1,1 +1,5 @@@
++<<<<<<< HEAD
 +test2
++=======
+ test1
++>>>>>>> master

[root@Ogiman ogiman_repo]# vi test
```
- 修正後に再度pushする

```
[root@Ogiman ogiman_repo]# git status
# On branch test-branch2
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Changes to be committed:
#
#       new file:   README.md
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#       both modified:      test
#
[root@Ogiman ogiman_repo]# git add test
[root@Ogiman ogiman_repo]# git commit
[test-branch2 f519176] Merge branch 'master' into test-branch2
[root@Ogiman ogiman_repo]# git push origin test-branch2
```

- リモートでコンフリクトが解消されていることを確認
![FireShot Capture 2 - ogiman0216_ogiman_repo - https___github.com_ogiman0216_ogiman_repo_tree_master.png](https://qiita-image-store.s3.amazonaws.com/0/144316/7c1d700d-0b54-1bef-0354-408167d45d09.png)


## 他のブランチから一部分だけ持ってくる
- git cherry-pick "commit-ID"

- test-branch1の以下を抜き取る

```
commit 05da546dbff5ff334d14e31e48a4c7165caa615d
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 22:33:43 2017 +0900

    23
```
- masterから実施する

```
[root@Ogiman ogiman_repo]# git cherry-pick 9b7ecda0d1c3327c224645a33a75cc96d7e234
bd
[master 05da546] 23
 1 file changed, 1 insertion(+)
 create mode 100644 test2

[root@Ogiman ogiman_repo]# git log
commit 05da546dbff5ff334d14e31e48a4c7165caa615d
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 22:33:43 2017 +0900

    23

commit b461e72f6ca1f2b1b1bc8c49612de1854fa6061a
Merge: 398449c 8cbbd0b
Author: ogiman0216 <ogiman@hoge.com>
Date:   Sun Sep 10 21:44:14 2017 +0900

    Merge pull request #4 from ogiman0216/test-branch1

    second commit
```

## リモートのあるブランチをローカルのブランチにpull
- git pull origin "remote-branch":"local-branch"
- 以下はtest-branch2の情報
![FireShot Capture 2 - ogiman0216_ogiman_repo - https___github.com_ogiman0216_ogiman_repo_tree_master.png](https://qiita-image-store.s3.amazonaws.com/0/144316/e3fcc948-aa56-a5b2-fb4d-53ad353c0f77.png)

```
[root@Ogiman ogiman_repo]# git pull origin test-branch2:test-branch1
From https://github.com/ogiman0216/ogiman_repo
   8cbbd0b..9b7ecda  test-branch2 -> test-branch1
Already up-to-date.

[root@Ogiman ogiman_repo]# git checkout test-branch1
Switched to branch 'test-branch1'

[root@Ogiman ogiman_repo]#
[root@Ogiman ogiman_repo]#
[root@Ogiman ogiman_repo]# ls -l
total 12
-rw-r--r--. 1 root root 14 Sep 10 21:48 README.md
-rw-r--r--. 1 root root  9 Sep 10 22:24 test
-rw-r--r--. 1 root root  6 Sep 10 22:21 test2
```

## ちょっと役立つ使い方
- 指定範囲のcommit数
    - git log --date=iso --pretty=format:"[%ad] %h : %s" --name-status --since="2017/10/26"