LoginSignup
2

More than 3 years have passed since last update.

Git remote branch運用

Last updated at Posted at 2017-06-02

1. gitで双方向にmergeしない

自分のブランチにmasterをmergeしない

local branchでの作業を終えたら、branchを消去してmasterブランチのみ残す

command

masterブランチからexperimentalを作る

git branch experimental
git checkout experimental
git push experimental
git checkout master
git merge --no-ff experimental
git branch -d experimental #ローカルブランチを削除
git push --delete origin experimental #リモートブランチを削除

git remote branch delete: unqualified destination

git fetch -p origin

2. サイドlocal branchが必要な場合はリモートのgitブランチをローカルにチェックアウトする

リモートのgitブランチをローカルにチェックアウトする

git checkout -b other_branch origin/other_branch

3. リモートレポジトリ同時をmerge

  • git checkout master
  • git merge remotes/origin/develop01

4. リモートブランチとローカルブランチの差分を表示する

command

  • 現在のローカルブランチとリモートブランチの差分を表示
    • git diff remotes/origin/master
  • 現在のローカルブランチとリモートブランチの差分を簡易表示
    • git diff --name-status remotes/origin/master

reference

5. Git Log

6. Git diff

7. gitのコミットログの内容を修正

  • 編集対象のコミットを選ぶ
    • git rebase -i HEAD~3
  • コメントを編集する
    • git commit --amend
  • 編集したコメント保存
    • git rebase --continue
  • Reference
    • gitのコミットログの内容を修正

8. Conflict

9. Gitで状態別に一括追加

  • コマンド一発!Gitで状態別に一括追加する方法
    • git ls-files --others --exclude-standard | xargs git add
  • others
  • regex='.*json'
  • for file in git ls-files --others --exclude-from=.gitignore; do
  • if [[ $file =~ $regex ]]; then continue; fi
  • git commit add $file
  • done
  • Reference

10. まとめて.gitignoreする方法

  • 無視したいファイルをリポジトリから削除(管理対象から外れるだけで、ローカルにあるファイルは削除されない)
    • git rm --cached hoge.tmp
  • 大量のファイルが登録されている場合
    • git rm --cached git ls-files --full-name -i --exclude-from=.gitignore

11. Gitにおけるローカルブランチ/リモートブランチの整理

ブランチの種類

  • ローカルブランチ
    • ローカルで開発するときにcheckoutで開いてコミットしていくお馴染みのもの
  • リモート追跡ブランチ
    • ローカルに保持している、クローン元リポジトリのブランチを取り込んだもの
  • リモートブランチ
    • リモート追跡ブランチを通じてしか知り得ない

挙動整理

  • ローカルでコミット無し、リモートでコミットされていた場合
    • fetchでリモートの情報を取得する
      • (git checkout develop)
      • git fetch
    • ローカルブランチにリモートのコミットCが継ぎ足される
    • リモート追跡ブランチorigin/developが更新さる
    • ただし、ローカルブランチdevelopは変更されない
    • なので、fetchしたら速やかにマージする
      • git checkout develop
      • git merge origin/develop
  • ローカルでコミット有り、リモートでコミット無しの場合
    • リモートに更新情報が無いのでfetchやpullを行っても何も変化なし
    • ローカルコミットをリモートに反映させる必要が有る
  • ローカルとリモート両方でコミット有りの場合
  • https://qiita.com/forest1/items/db5ac003d310449743ca

12. Git 過去の特定のコミット位置からブランチを切りたい

commit history

  • git log --all --decorate --graph --oneline

command

  • git checkout -b <new_branch> <commit_hash>

reference

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2