Skip to content

Git Troubleshooting

  • 変なbranchにいる

    $ git rebase --continue
    $  git status
    On branch main
    nothing to commit, working tree clean
    $ git branch -avv
    
    * (no branch, rebasing main) a75ee0e add cdk tips.
      main                       7d86223 initial commit
      remotes/origin/main        7d86223 initial commit
    $ git branch
    * (no branch, rebasing main)
      main
    $ git rebase --continue
    Successfully rebased and updated refs/heads/main.
    $  git status
    On branch main
    nothing to commit, working tree clean
    $ git branch
    * main
    $ git branch -avv
    * main                a75ee0e add cdk tips.
      remotes/origin/main 7d86223 initial commit
    

  • pull時に、fatal: refusing to merge unrelated histories. --allow-unrelated-historiesオプションを指定して、pull

    $ git pull origin main 
    From https://github.com/neko-kick/portfolio_2023
     * branch            main       -> FETCH_HEAD
    fatal: refusing to merge unrelated histories
    $ git pull --allow-unrelated-histories origin main
    From https://github.com/neko-kick/portfolio_2023
     * branch            main       -> FETCH_HEAD
    Auto-merging README.md
    CONFLICT (add/add): Merge conflict in README.md
    Auto-merging mkdocs/docs/aws_cdk/tutorial.md
    CONFLICT (add/add): Merge conflict in mkdocs/docs/aws_cdk/tutorial.md
    Automatic merge failed; fix conflicts and then commit the result.
    

  • カレントブランチ名を取得する

    # カレントブランチを取得
    git branch | grep -E '^\* ' | sed 's/^\* //g'
    
    git pull origin "$(git branch | grep -E '^\* ' | sed 's/^\* //g')"
    

  • 後からgitignoreに追加したいフォルダ

    # 1. .gitignore に対象のフォルダを追加しておく。
    **/hoge/
    # 2. git rm で管理対象から除外
    git rm --cached -r ./mkdocs/docs/hoge
    

  • merge したい

    # ブランチ作成 & チェックアウト 
    git checkout -b feature_xx
    
    # コードを修正する
    
    # feature_xx -> main へmerge 
    git rebase main 
    
    # 適宜修正
    ## git rebase --continue
    
    # チェックアウト & merge 
    git checkout main
    git merge feature_xx -m "修正完了したっす"
    
    git push origin main