Gitジャーナル(まだ別のカンニングペーパー)


Git(2015 ?)での初期の頃から.私は私が私がいくつかの点で使用しなければならなかった最もユニークなGitコマンドに注意する個人的なジャーナルを私に運びました.
今日、私はこのジャーナルを公開しています.🤞
免責事項:あなたはよく知られていない場合は、ここで友好的です.
しかし、このジャーナルの流れの大部分は全く「初心者」ではありません.
それにしても、質問をしたり、いろいろなアプローチを提案したりするのもいいでしょう.

エントリー

  • 1. Git configuration
  • 2.1. Change commits author/email
  • 2.2. Change commits author/email - chapter 2
  • 3. Cleanup local branches
  • 4. Pretty Commit logs
  • 5. Renaming case sensitive files
  • 6. Override a file with another branch
  • 7. Override a branch with another branch
  • 8. Quicker amend
  • 9. Checkout a PR from a forked branch
  • 10. Convert a PR branch with merge commits to a clean rebased git history
  • 11. Different git configs based on path location
  • 12. Restart SSH Agent
  • 13. Include another repo into your local repo
  • Gitの設定


    問題:私は別のプロジェクトのための別のGitアカウントを使用する必要があります.正しいアカウントを使用するためには、次のように設定します.
    # Setting the account globally for all the projects...
    git config --global user.name "your-username"
    git config --global user.email "[email protected]"
    
    # ... or just to a specific project
    # (you need to be in the git project folder)
    git config --local user.name "your-username"
    git config --local user.email "[email protected]"
    

    2.1 .変更は、著者/メールをコミット


    問題:私は間違ったメールを間違って使った.別の著者に変更する必要があります
    git commit --amend --reset-author 
    

    2.2 .変更は、著者/電子メール-第2章をコミット


    問題:私は、私が間違ったメールで複数のコミットをしたと理解しました.この魔法のコマンドはトリックを作りました.
      git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ];
        then 
            export GIT_COMMITTER_NAME="New Author Name";
            export [email protected];
            export GIT_AUTHOR_NAME="New Author Name";
            export [email protected];
        fi; git commit-tree "$@"'
    

    クリーンアップローカルブランチ


    問題:ときgit branch , 私は地元の古い枝をたくさん持っていました.私は、彼ら全員を削除したかったです.
    # delete all branches expect master
    git branch | grep -v "master" | xargs git branch -D
    
    or
    # Delete all merged branches except the current one
    git branch --merged | grep -v '*' | xargs git branch -D
    

    かなりコミットログ


    問題:出力git log 醜くて読めない.もっとフレンドリーなものが欲しい.
    # Add this to your global .gitconfig
    [alias]
    lg = log --graph --pretty='format:%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    
    今だけ使用git lg . ログは非常に読みやすい、特にgitの歴史と合併ブランチを理解する.

    ファイルのファイル名の変更


    問題:Macで、私はすでにプッシュされたファイルの名前を変更する必要がありましたHello.js -> hello.js . 伝統的な手動の名前を変更するMacOSで動作しません.解決方法
    git mv -f path/to/Hello.js path/to/hello.js
    

    ファイルを別のブランチでオーバーライドする


    問題:私は間違ってファイルを変更しましたpackage-lock.json ) そして、それを元に戻して、同じようにしたかったですmaster 枝.
    git checkout master package-lock.json
    
    ファイルは、どのようにして、あなたは再びそれを再度コミットする必要がありました.

    7 .別のブランチでブランチをオーバーライドする


    問題:ブランチBをブランチAのようにする必要がありました.
    # push branch-a code into branch-b (forced)
    git push origin branch-a:branch-b -f
    
    2021年の注意:私がなぜこれをしたか覚えていません.今日は別の方法でこれを行う.
    # checkout branch-a
    git checkout branch-a
    # push its code to branch-b (forced)
    git push origin branch-b -f
    

    8 .高速修正


    問題:私は頻繁にコミットを修正します.私は、それをするより速い方法が欲しかったです.
    # your global .gitconfig
    [alias]
    sneak = commit --amend --no-edit
    
    # amend a commit right away without being prompt to edit the commit message.
    git sneak
    
    P . S .つぶやきからこれを盗んだことを覚えていますが、誰か覚えていません.

    分岐した枝からPRをチェックアウトする


    問題:私はチェックアウトされた誰かの他のPRは、forrekレポから作成された広報.私の地元のレポもフォークだった.
  • 1 .オリジナルのレポをフォークに追加します.Git/.gitconfigファイル
  • [remote "upstream"]
        url = https://github.com/author/original-repo-name.git
        fetch = +refs/heads/*:refs/remotes/upstream/*
    
  • 2 .この3魔法の別名をグローバルに追加します.Gitconfig.
  • [alias]
    # Checkout a PR
    pr = "!f() { git fetch -fu ${2:-upstream} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f"
    pr-clean = "!git checkout master ; git for-each-ref refs/heads/pr/* --format=\"%(refname)\" | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done && git branch -D pr"
    pr-id = "!f() { git fetch upstream pull/\"$1\"/head:pr && git checkout pr; }; f"
    
  • 3 .このGitエイリアスを使って、IDによって新しいPRをチェックアウトします.
  • git pr-id 781 # 781 is the PR id.
    
    # to pull the new commits, run the following:
    git pr-clean && git pr-id 781
    

    10 . PRブランチを変換してマージをコミットし、クリーンリビジョンのGit履歴にコミットします。


    問題:私はコミットの完全なPRブランチを持ちました.私はコミットを壊すことなく、それをマージしたかったが、きれいなGit史を保っていました.(マージがなければコミットします).
    フロー概要:差分を含む新しい分岐を作成します.このブランチを使用して、自分のPRブランチをリセットし、コミット履歴を書き換える.
    # - 0. Safety guard: Create a backup of your feature branch just in case things go wrong.
    git checkout -b your-branch--backup
    
    
    # - 1. Get the diffs between your `feature-branch` and `master`.
    ## 1.1 go to master and pull the latest version 
    git checkout master && git fetch
    
    ## 1.2 get the diffs between master and your branch
    git merge --squash origin/your-branch
    
    
    # The files were modified to look like `your-branch`.
    # verify it by yourself in your code editor...
    
    # - 2. Create a new branch with these diffs
    git checkout -b your-branch--clean
    
    # - 3. Commit the changes to the new branch.
    #      You can do it in a single commit or multiple commits.
    ## This commits all the changes at once
    git add . && git commit -m "The new code"
    
    # - 4. Rewrite `your-branch` to match the `your-branch--clean`,
        # cleaning all the messy git history
    ## 4.1 go to your original branch...
    git checkout your-branch
    
    ## 4.2 ... and rewrite it to be exactly like the cleaned branch
    git reset --hard origin/your-branch--clean
    
    
    # - 5. That's it! Everything should work as before,
        # but now with a clean commit history.
        # Give it a final check. If all good, push it!
    gift push -f
    
    もう一度PRに行きなさい.まったく同じように見えます.
  • 変更されたファイルの同じn
  • dinの同じn - supは、加えられて/取られました.
    しかし今、コミットの歴史は今クリーンです!✨
  • 何かがうまくいかない場合は、最初に作成したバックアップブランチを使い始めてください.

    パス位置に基づく異なるGit構成


    問題:私は複数のプロジェクトのための異なったGit設定を持っていました--例えば、異なるプルConfigと異なるユーザー.プロジェクトごとに複数のローカルコンフィグレーションを作成するよりも、より良い解決策が必要でした.
    参照:Conditional git configs
  • 1 .設定ごとにギフト設定を作成します.
  • .gitconfig
    .gitconfig-configA
    .gitconfig-configB
    
    # .gitconfig
    
    [includeIf "gitdir:~/Documents/git/folderA/"]
      path = .gitconfig-configA
    [includeIf "gitdir:~/Documents/git/folderB/"]
      path = .gitconfig-configB
    
    [core]
       excludesfile = /Users/your-pc-name/.gitignore
    
    [alias]
    lg = log --....
    # other alias common to everyone...
    
    # .gitconfig-configA.
    # project under folderA use userA and pull ff
    
    [user]
      name = Your Name A
      email = [email protected]
    
    [pull]
      ff = only
    
    # .gitconfig-configB
    # projects under folderB use userB and pull rebase
    
    [user]
      name = Your Name B
      email = [email protected]
    
    [pull]
      rebase = true
    

    SSHエージェントの再起動


    問題:私のMacOS(Catalina 10.15.7)は正しくsshでreposを引っ張るのを防ぎます.
    私がsshキーを持っていると想像してください[email protected] 設定.
    # The eagle SSH is list properly in pub key...
    cat ~/.ssh/*.pub
    
    # But does not show up in the SSH list...
    ssh-add -L
    
    解決策は、SSHエージェントが再び動作するように再起動することです.
    eval "$(ssh-agent -s)"
    
    # it will output a number (eg 00123). Use it in the next command
    
    kill -9 00123
    
    # finally, verify the SSH connection again.
    ssh [email protected]
    
    # Output: "Hi eagle! You've successfully authenticated"
    
    参照:Stackoverflow - how to restart ssh-agent

    13 .リポジトリに別のレポを含める


    問題:採用プロセスの一部として(私はインタビュアーとして)、私はレポを介して提出された複数のコード演習を確認します.これらのreposはaのコピーですtemplate repo .
    それぞれのエクササイズのレポを確認するには、それらをクローニングするのではなく、何度も何度も繰り返して、私はローカルテンプレートrepoにそれらをローカルに追加します.
    # You can give any unique name to the remote repo. eg "bunny"
    
    # add the repo and checkout its main branch
    git remote add bunny https://github.com/author/reponame.git
    git fetch bunny
    git checkout bunny main
    
    の利点remote add オーバーclone :
  • クリーナー:すべての同様のフォルダなしで私のコンピュータクリーナーを保つ.
  • 高速:必要はありませんnpm i 依存関係がすでにある(主なブランチに基づいているので)
  • 簡単に比較する:それは同じコードベースのため、いくつかの他のGitコマンドで期待結果に行使を比較する方が簡単です.
  • それは今のすべてです!(2021年8月14日)
    次回は問題に直面しているので、この日記を新しいエントリで更新します.