DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on • Edited on

n350071 Git note

👍 History

👍 Merging

Merging

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 
Enter fullscreen mode Exit fullscreen mode

Conflict

# マージの複雑なコンフリクトのときに使う 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 
Enter fullscreen mode Exit fullscreen mode

Revert

# 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 
Enter fullscreen mode Exit fullscreen mode

👍 Pull Request

make empty Pull Request

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 
Enter fullscreen mode Exit fullscreen mode

👍 Branch Management

Rename

Local

# 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. 
Enter fullscreen mode Exit fullscreen mode

Remote

# remove old branch in remote git push origin :[old name] # push the renamed branch git push origin [new name] 
Enter fullscreen mode Exit fullscreen mode

search remote branches

git branch -a | grep [search-word] 
Enter fullscreen mode Exit fullscreen mode

Checkout from the remote branch

git fetch git checkout -b branch_name origin/other_name 
Enter fullscreen mode Exit fullscreen mode

👍 Develop ~ Commit

Stash

# 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} 
Enter fullscreen mode Exit fullscreen mode

Clean

git clean -dfn # check git clean -df # remove un tracked files git rm --cached file/to/path.rb # remove a tracked file 
Enter fullscreen mode Exit fullscreen mode

Diff

# Show diff for the staged files git diff --cached # Show diff in an editor git diff HEAD^ --name-only | xargs atom 
Enter fullscreen mode Exit fullscreen mode

Commit

# 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] 
Enter fullscreen mode Exit fullscreen mode

👍 salvage(復活, 取り消し)

removed file

find the commit that remove the deleted file, and checkout from it.

git rev-list -n 1 HEAD -- deleted/file.rb git checkout commit_id^ -- /deleted/file.rb 
Enter fullscreen mode Exit fullscreen mode

restore, revival from git reset --hard HEAD^

find the log, and reset the "reset"

git reflog -n 3 git reset --hard "HEAD@{1}" 
Enter fullscreen mode Exit fullscreen mode

👍 Repository Management

Clone and rename the directory

# git clone url [directory-name] git clone git@github.com:n350071/rails-on-k8s.git rails-on-k8s-refactor 
Enter fullscreen mode Exit fullscreen mode

👍 Setting

GitHub

Remote repository

# 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> 
Enter fullscreen mode Exit fullscreen mode

.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 
Enter fullscreen mode Exit fullscreen mode

.git_commit_template

$ cat .git_commit_template # === format === # :emoji: issue-id issue-title # ==== example ==== # :+1: SR-400 認証機能の実装 # :bug: BG-128 Thanksメッセージは2通ではなく1通 # ==== Emojis ==== # 👍 小さな機能追加 :+1: # 🎉 大きな機能追加 :tada: # 🐛 バグ修正 :bug: # 💚 リファクタリング :green_heart: # ✅ テストの追加 :white_check_mark: # 📝 ドキュメント追加 :memo: # 🚀 パフォーマンス改善 :rocket: # 🆙 バージョンアップ :up: # 😅 動くようにした :sweat_smile: # ⛔️ 下書き :no_entry: # ✏️ 記事投稿 :pencil: 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)