最終更新日: 2026-07-24
TOP(About this memo)) > 一覧(Git) > ブランチ・マージ
git branch -a:remotes/origin/〜と表示されるのがリモート追跡ブランチ。git branch -vv:[origin/develop: behind 2]のように表示されるのが上流ブランチ。git statusでも現在のブランチと上流ブランチがわかる。git branch:すべてのブランチを表示(*がついているのが今のブランチ)。git branch <newbranch>:新しいブランチを作成する。git checkout -b <newbranch>:新しいブランチを作成し、すぐにそのブランチに切り替える。git branch <newbranch>してからgit checkout <newbranch>するのと同義。
git branchとgit checkout -bの違い)git add .
git commit -m "****"
git push -u origin <newbranch> # -uをつけないと上流ブランチに設定されない
developから派生したfeature-xxxを削除する場合)git checkout develop
git branch -d feature-xxx
git branch -m <変更前> <変更後>
git push -u origin <変更後>
git push origin :<変更前>
git branch -vv
master b104f69 [origin/master: ahead 1] xxxx xxxxx xxxxxxxxxxxx xxx.
cool-feature b104f69 [origin/cool-feature] xxx xxxxxxxx xxxxxxxx xxxx xxxxx.
git branch <ローカルブランチ名> -u <リモートブランチ名>
git branch <ローカルブランチ名> --set-upstream-to=<リモートブランチ名>
git branch -vvで確認するとローカルブランチscheduleに上流ブランチが設定されていない場合、git branch -u origin/scheduleを実行すると、git branch -vvでscheduleに上流ブランチが設定されていることを確認できる。git branchでリモート追跡ブランチを起点にブランチを作成する
origin/cool-featureが存在する状態でgit branch origin/cool-feature
結果:Branch remote set up to track remote branch cool-feature from origin.
git checkoutにリモートブランチ名を指定する(実際にはこのケースが多いかもしれない(?))
origin/cool-featureが存在する状態でgit checkout cool-feature
結果:Branch cool-feature set up to track remote branch cool-feature from origin.
Switched to a new branch 'cool-feature'
-u(--set-upstream)」オプションを付ければ、自動的にpushするブランチとpush先のリポジトリ・ブランチが上流リポジトリ・上流ブランチとして設定される。フィーチャーブランチを初めてリモートにpushするときに便利なので、よく利用する。git push -u origin cool-feature
git push --set-upstream origin cool-feature
.git/configに直接書き込む(ほぼ使わない)
cool-featureの上流ブランチをorigin/cool-featureに設定git config branch.cool-feature.remote origin
git config branch.cool-feature.merge refs/heads/cool-feature
--no-ffをつけておく--no-ffが入るはずで、マージコミットが増えていくのを避けたい場合はローカルでgit merge origin/stagingしてからpushすることもある(IME)。MERGE_HEAD参照が、指定したブランチの先頭を指すように設定される。<<<、===、>>>の記号を使って、開発者に競合箇所を明示するように更新される。マージに関係ないファイルが変更されることはない。// ブランチの状態、現在masterブランチをチェックアウトしている
o---o :topic
/
-o----o :master <- HEAD
// git merge topic 実行後
o---o :topic
/ \
-o----o----o :master <- HEAD
// マージしたいブランチ(topic)が現在のブランチ(master)の直接的に先に進んでいる場合
// ブランチの状態、現在masterブランチをチェックアウトしている
o---o :topic
/
-o :master <- HEAD
// git merge topic 実行後
// この場合、変更が存在しないのでマージにあたってコミットが作られない(fast-forwardマージ)
// つまり、参照だけ移動される
o---o :topic, master <- HEAD
/
-o
--no-ffオプションを加えることで、強制的に新しいコミットを生成することも可能。// git merge --no-ff topic 実行後
o---o :topic,
/ \
-o --------o :master <- HEAD
git reflog
18828ad HEAD@{0}: merge topic: Merge made by the 'recursive' strategy.
1c17f4f HEAD@{1}: checkout: moving from topic to master
4f07825 HEAD@{2}: commit: Add header.
1c17f4f HEAD@{3}: checkout: moving from master to topic
1c17f4f HEAD@{4}: commit: New comit .
3b1e279 HEAD@{5}: commit (initial): Inital
HEAD@{1}がマージの直前の状態を表しているので、git resetを使って強制的に参照・インデックス・作業ツリーすべての状態を戻す。git reset --hard HEAD@{1}
git reset origin/master --hard
git merge --theirsといったコマンドもあるが、新しいコミットが作られてしまうためNG。--soft:現在のbranchの先頭だけをセット--mixed(デフォルト):現在のbranchの先頭とインデックスをリセット--hard:現在のbranchの先頭、インデックス、作業ツリーを全部リセットgit statusでconflictしたものを確認する。コンフリクトしていないものはstagingされているが、コンフリクトしているものはboth modified: 〜で出てくる。<<<<<<< HEAD
自分の環境の変更点
=======
マージを試みた他の環境での変更点
>>>>>>> [commit id]
git rebase -iでまとめると、きれいになりそう。
git pushしてしまった内容であればgit push -fで戻す。git push -fは絶対にやってはいけない。git rebase -i <修正したいコミットの一つ前のコミットID>
git commit --amend --reset-author
git rebase --continue
git logで修正されたか確認。git push -f(あまりおすすめしない)。git tag <タグ>
git tag -a v1.2 -m 'version 1.2' 9fceb02git tag <既存のタグ> <他のタグ>
git push origin <タグ>git tag -f <タグ名> <コミット番号>
git push -f <タグ名> # リモートの方も変える場合
git tag -d <タグ>
git push -d origin <タグ> # リモートの削除
git checkout feature
git log --oneline
git checkout master
git cherry-pick <コミットID>
-mをつける。
git cherry-pick -m 1 <commit_id>
git revert <コミットのハッシュ値>
git revert <commit> --no-edit # メッセージ編集画面を開かない