Environment
git version 2.20.1 (Apple Git-117)
user setting
git config --global user.name "xxx" git config --global user.email "xxx@xxx.com" # confirm settings git config --list # Detect changes in capitalization git config core.ignorecase false
※ globalを汚したく無い場合は --local
で。
stagedしたものをunstagedにする
git reset HEAD
commit messageの修正
push前の場合
git commit -m "xxx" git commit --amend -m "yyy"
push済の場合
git log --oneline git rebase -i HEAD~3 # 戻したいcommit SHA-1の位置を指定。"pick"を"edit"へ変更。 git commit --amend # コミットメッセージを変更。 git rebase --continue git push -f origin {branch}
push前のcommitを確認する
git log origin/hotfix/fix_xxx..hotfix/fix_xxx
push前のcommitを取り消す
git reset --soft[hard] HEAD^
- soft: commitだけを取り消してstaged changesへ変更点を戻す。
- hard: commitを取り消してworking directoryの変更点も全て戻す。
ファイルの変更を取り消して最後のcommit状態に戻す。
git checkout HEAD -- /path/to/file
push済のcommitを上書きして取り消す
# remain changes git revert <commit_sha>
ここで指定したcommitの直後のcommitは取り消されない。
git log --oneline --graph --decorate * aaaaaaa (HEAD -> xxx, origin/xxx) add: aaa * bbbbbbb add: bbb * ccccccc add: ccc
つまり bbbbbbb
を指定してrevertしたとすると aaaaaaa
は残ったままである。
push済のcommitを何事も無かったように取り消す
# force clear all pushes git reset --hard HEAD^ git reset --hard <commit_sha> git push -f origin <branch>
上記を実行前、他の人がローカルにpullしていた場合は以下を実行する必要がある。
git fetch git reset --hard origin/<branch_name>
そうしないと他の人はgit pullしてもローカルにcommitは残ったままで、ahead xxx commitsの状態となる。
branch name変更 (remote)
- xxx: before
- yyy: after
git push -u origin feature/xxx git branch -m feature/xxx feature/yyy git push origin feature/yyy
cache削除
# clean cache only file git rm --cached {FILE_NAME} # clean under current directory git rm -r --cached .
localにおける変更をcommit対象としない
# assume git update-index --assume-unchanged {FILE_NAME} # no assume git update-index --no-assume-unchanged {FILE_NAME}
branch間の差分確認
file差分
git diff {x_branch} {y_branch}
commit差分
git log --no-merges master..feature/xxx commit xx (origin/feature/xxx, feature/xxx) Author: xxx <xxx@xxx.com> Date: Wed Jan 23 13:09:47 2018 +0900 xxx commit xx (origin/feature/xxx, feature/xxx) Author: xxx <xxx@xxx.com> Date: Mon Jan 21 13:35:33 2018 +0900 xxx
stash操作
stash
git stash save # changed content git stash list git stash list -p git stash show <stash名> # git stash show stash@{0}
取り出し
git stash apply stash@{0} git stash drop {want to remove stash_name}
特定のcommitからbranchを切る
単純にcommitをcheckoutしてbranchを切ろうすると invalid branch name: HEAD
となる。現ブランチが存在しないからである。
git checkout {commit_sha} git status HEAD detached at {commit_sha} nothing to commit, working tree clean git branch -m feature/xxx fatal: Invalid branch name: 'HEAD'
checkout時にbranch名を指定してcheckoutする。
git checkout {commit_sha} -b feature/xxx
Tag打ち
# 最新commitからtag打ち git tag -a {tag_name} -m 'create tag' # tag list git tag # tag show git show {tag_name} # 全tag push git push origin --tags
remoteで消去されているbranchをlocalへ反映させる
# pull時に以下のようなエラーが出る. git pull error: cannot lock ref 'refs/remotes/origin/xxx': 'refs/remotes/origin/yyy' exists; cannot create 'refs/remotes/origin/xxx' ! [new branch] xxx -> origin/xxx (unable to update local ref) error: some local refs could not be updated; try running # remoteで削除済のbranchをpruneする. git remote prune origin Pruning origin URL: git@xxx.com:xxx/xxx.git * [pruned] origin/xxx * [pruned] origin/xxx * [pruned] origin/develop/xxx * [pruned] origin/develop/xxx # OR fetch[pull]時に `--prune` optionを付与することでremoteで削除済のbranchを反映する. git fetch[pull] --prune From xxx - [deleted] (none) -> origin/xxx
file / directory名の変更
file / directory名を変更しても別々のものとして扱われてしまう。
git config core.ignorecase false git status Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: path/to/WantUpperCamelCase.rb modified: path/to/wantuppercamelcase.rb
無理やり感あるが git rm -f
をして renamed
のstatusにする。
git rm path/to/wantuppercamelcase.rb -f git status Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: path/to/wantuppercamelcase.rb -> path/to/WantUpperCamelCase.rb deleted: path/to/WantUpperCamelCase.rb
deleted
の状態をもとに戻す。
git checkout -- path/to/WantUpperCamelCase.rb
Trouble shooting
ssh key が server 側に登録されているにも関わらず、local から当該 repository の操作 (pull, push 等) する際に user name, password 入力を促される場合
git config の remote url を確認する。
$ git config --list
remote.origin.url=https://git.xxx.com/xxx/xxx.git
な URL となっている場合は、以下のように URL 形式を変更する。
$ git remote set-url origin git@git.xxx.com:xxx/xxx.git