n350071 Git note
Tags: git
Published: 2019-09-25 16:54:35

---
title: n350071 Git note
description: 
tags: git
series: note
published: true
cover_image: 
canonical_url:
---

## 👍 History
{% link n350071/git-history-reading-42k5 %}

## 👍 Merging
### Merging
```sh
git merge # not make a merge commit
git merge --no-ff # make a merge commit (to be able to revert it later)

# check conflict before merge
git merge --no-commit [branch name]

# abort
git merge --abort
```

### Conflict
```sh
# マージの複雑なコンフリクトのときに使う
git checkout --conflict=diff3 file.rb # Baseコンフリクトマーカーも表示する
git checkout --conflict=merge file.rb # コンフリクトマーカーの書き直し

# cancel our modified
git checkout develop -- file/to/path.rb

# cancel their modified
git checkout head -- file/to/path.rb
```

### Revert
```sh
# revert the merge commit and back to the first parent (the modified from x in 'git merge x' is deleted)
git revert -m 1 7143dc9d8d835efa3012e9ad624c75965297ee88 -n
git revert --continue

# completely delete the merge commit (You shouldn't do it after you pushed the merge commit)
git reset --hard HEAD^

# revert the reset --hard HEAD^
git reflog -n 3 # list your operations
git reset --hard 9823val1 # back to the operation



```

## 👍 Pull Request
### make empty Pull Request
```sh
git commit --allow-empty -m [the title of the Pull Request]
git push origin [branch name]
git branch -u origin/[remote branch name]
git branch -vv # confirm
```

## 👍 Branch Management
{% link n350071/git-show-and-delete-merged-branch-even-if-it-s-squashed-58il %}

### Rename
#### Local
```sh
# rename the branch name (git branch (-m | -M) [<oldbranch>] <newbranch>)
git branch -m [new name]
git branch -m [old name] [new name] # This is also ok.
```
#### Remote
```sh
# remove old branch in remote
git push origin :[old name]

# push the renamed branch
git push origin [new name]
```

### search remote branches
```sh
git branch -a | grep [search-word]
```

### Checkout from the remote branch
```sh
git fetch
git checkout -b branch_name origin/other_name
```

## 👍 Develop ~ Commit
### Stash
```sh
# save include untracked files
git stash --include-untracked

# save with name
git stash save 'stash_name'

# show diff
git stash show -p stash@{0}

# apply
git stash apply stash@{0}

# apply & remove
git stash pop stash@{0}
```

### Clean
```sh
git  clean -dfn # check
git  clean -df  # remove un tracked files

git rm --cached file/to/path.rb # remove a tracked file
```

### Diff
```sh
# Show diff for the staged files
git diff --cached

# Show diff in an editor
git diff HEAD^ --name-only | xargs atom
```

### Commit
```sh
# Rewrite the previous commimt message
git commit --amend

# Add the staged files to the previous commit
git commit --amend --no-edit

# squash
git rebase -i [previous_commit_id]
```

## 👍 salvage(復活, 取り消し)
### removed file
find the commit that remove the deleted file, and checkout from it.
```sh
git rev-list -n 1 HEAD -- deleted/file.rb
git checkout commit_id^ -- /deleted/file.rb
```

### restore, revival from git reset --hard HEAD^
find the log, and reset the "reset"
```sh
git reflog -n 3
git reset --hard "HEAD@{1}"
```


## 👍 Repository Management
{% link n350071/git-keep-empty-directory-by-gitkeep-aj1 %}

### Clone and rename the directory
```sh
# git clone url [directory-name]
git clone git@github.com:n350071/rails-on-k8s.git rails-on-k8s-refactor
```


## 👍 Setting
### GitHub
{% link n350071/generate-github-ssh-key-and-upload-it-586f %}

### Remote repository
```sh
# check the remote repository
git remote -v

# add (git remote add [name] [url])
git remote add origin git@github.com:n350071/rails-starter-kit-with-docker.git

# Change remote name from 'origin' to 'destination'
$ git remote rename origin destination

# change the default remote repository for 'push'
git push -u <remote_name> <local_branch_name>

```

### .gitconfig
```
$ cat .gitconfig
[alias]
  check = !git checkout $(git branch | sed 's/*//g' | sed 's/ //g' | peco)
  bcp  = !git branch | peco | sed 's/*//g' | sed 's/ //g' | tr -d '\n'  | pbcopy
[log]
  date = iso-local
[core]
  quotepath = false
[commit]
  template = /Users/naoki/.git_commit_template
```

### .git_commit_template
```
$ cat .git_commit_template

# === format ===
# :emoji: issue-id issue-title

# ==== example ====
# :+1: SR-400 認証機能の実装
# :bug: BG-128 Thanksメッセージは２通ではなく１通

# ==== Emojis ====
# 👍 小さな機能追加 :+1:
# 🎉 大きな機能追加 :tada:
# 🐛 バグ修正 :bug:
# 💚 リファクタリング  :green_heart:
# ✅ テストの追加 :white_check_mark:
# 📝 ドキュメント追加 :memo:
# 🚀 パフォーマンス改善 :rocket:
# 🆙 バージョンアップ :up:
# 😅 動くようにした :sweat_smile:
# ⛔️ 下書き :no_entry:
# ✏️ 記事投稿 :pencil:
```