DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

Posted on • Updated 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ใƒกใƒƒใ‚ปใƒผใ‚ธใฏ๏ผ’้€šใงใฏใชใ๏ผ‘้€š

# ==== 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)