Git CLIから始めて、あなたの日常プロジェクトでこれらのワークフローを使ってください


私は知っている、私は知っている、多くのミニチュートリアル、cheatsheet、およびHOWTOのGitについてですが、それらのいくつかは本当にクレイジーです!Gitを試してみる初心者がどのようにチェリー・ピックについて最初に学び始めて、どのように修正を受けるか、または何をするかを見ることができません.それで、あなたがバージョン管理システムとしてGitを知っていると推定してthis 12min intro from Telusko ), しかし、あなたはCLIコマンドを使用し始めたことがない、私はここで私はすべての私の個人的なプロジェクトでGitを使用して行くことを得たいくつかのメモを書くよ!

内容を以下に示します:

  • git help
  • set up global configuration variables
  • git init VS. git clone
  • git status
  • git diff

  • git work-flow (on master branch)
  • git add -A
  • git commit
  • git log
  • git push
  • git remote
  • git pull
  • Schema: Working Directory, Staging Area, Git Remote Repository

  • time to create a new branch!
  • git branch, git checkout
  • merge a branch: git merge (+mini-work-flow)
  • delete a branch
  • git Complete Work-flow - work from another branch
  • create a new repo from a locally existing/completed project (mini-work-flow)

  • locally mistakes that could've been made
  • we need to discard changes to a modified file/files with git checkout -- .
  • we mess up a commit -m message; modify the last commit message without doing another commit
  • we forgot to add a file to the last commit
  • we made commits to the master branch instead of our working branch
  • types of git resets (soft, mixed, hard)
  • fatal/big whoops: we did a hard reset on some changes but we realized that we actually need them: git reflog
  • undoing a commit after pushing to remote server. Fix without changing the git history
  • using the git stash command
  • GITヘルプ

    Display help (all commands) in CLI:

    git help
    

    Check Git version installed on PC:

    git --version
    

    Git help used on commands will display the help info in a new Browser tab (Chrome/Firefox/Edge etc). This will be really handy to understand all those parameters used in various commands.

    git help <command> # is the same as git <command> --help
    
    # eg:
    git help config
    git config --help # is the same as git help config
    git add --help
    

    グローバル設定変数の設定

    • Needed to push local repository to GitHub remote server
    • Also useful when working in a team to see who changed the code (with the blame command aka 'see who and when was this file last modified')
    • For name: you can choose any name, it can be different from your github account
    • For email: it must be the exact same email used on your github account
    git config --global user.name "John Doe" 
    git config --global user.email "[email protected]" 
    
    git config --list # list all configurations you made
    

    独自のローカルリポジトリinitを作成します

    • Create own local empty repo (init) (it will create a new folder .git with all the informations about that repo):
    cd my_new_project_folder_name
    git init
    
    • Copy/Download an existing repo (clone) (this will also copy all of the earlier commits/history made to that repository)
    git clone <url> <optional:where_to_clone>
    # eg:
    git clone https://github/username/android-app AndroidAppFolder
    

    GITステータス

    git status
    

    Git keeps track of modified/added/removed files and also which files are [are not] tracked.
    However, sometimes you don't want to track some files (eg. personal files, personal configurations files, cache files auto-generated after each build) => create .gitignore file where you can write the files you don't want to include (ignore them forever):

    mystuff.txt
    *.pyc
    .DS_Store
    

    ^^ These files won't show up anymore when calling git status.
    Also, .gitignore should be included (git add .gitignore) to prevent a team collaborating on a project from committing generated cache files => don't add .gitignore to .gitignore itself lol.

    ジットディフ

    Git diff shows the changes made to the code within modified files (git status shows only which files have been modified/created).

    git diff
    

    GITワークフロー(マスターブランチ)

    • Add files to the staging area (= add all the files that are ready to be commited except the files from .gitignore)
    git add -A 
    
    • Commit all the files added (tracked files) to the local repository.
    git commit -m "Message from this commit"
    
    • Add all the files to the staging area then commit:
    git commit -a -m "Message from this commit"
    
    • Git reset [file.ext] will make all the files [or only file.ext] untracked (out of the staging area) => The changes to that file.ext will not be committed!
    git reset [file.ext]
    
    • Git log shows all the made commits (with a hash number, author, date of each commit). By default, the log opens in Vim text editor, if installed.
    git log
    git log -3 # shows last 3 commits
    

    git push -u origin master
    # git push -u <remote> <branch>
    # -u or --set-upstream is to save/add upstream (tracking) reference(the <remote> and <branch>), in order to just write "git push" without specifying again the <remote> and <branch> 
    
    • Git remote shows all the remotes (GitHub repositories) where you can push the last (local) commit. Git remote -v is for verbose (all info about the remotes).
    git remote
    git remote -v
    

    To add a remote:

    git remote add origin https://github.com/username/projectname/.git
    # git remote add <new_branch_name> <link_to_repository>
    

    OBS: When cloning a remote (a GitHub repository) with git clone, a remote named "origin" will be available by default.


    Get (locally) the last state (last changes/updates) of the project if someone made changes on the global repository (the remote to github.com server):
    git pull origin master
    

    If this error occurs when pulling: "your local changes to the following files would be overwritten by merge" and you want to drop/overwrite all the changes made from the local repository and get the latest updates from the global repository, use:

    # Drop local changes, revert to the HEAD reference (last local commit in the master branch)
    git reset --hard # NEVER USE: git checkout HEAD^ file/to/overwrite
    git pull origin master
    # HEAD^ is short for HEAD^1, which means the one commit before HEAD. You could also do HEAD^2 for the commit before that one
    

    Or, discard/give up all the changes (modified files) and go back to the last (local) commit state of files:

    git checkout -- . 
    

    NOTE1: git checkout HEAD^ filename (will overwrite the file to the state of commit before last local commit in the master branch)

    NOTE2: git checkout HEAD^ without specifying a file will DROP THE WHOLE LAST COMMIT!!! and will revert to the commit before last commit (however you will be in a detached head state branch, so you can revert this action by just changing back to the master branch: git checkout master)

    NOTE3: git checkout HEAD^1 is roughly the same as git reset HEAD^1, but:

  • Use reset if you want to undo the staging of a modified file !!!
  • Use checkout if you want to discard changes to unstaged file/s !!! (however it is still possible to recover lost files with git reflog and cherry-pick, check locally mistakes section .
  • スキーマ:ワーキングディレクトリ、ステージング領域、gitリモートリポジトリ


    CLIから新しい分岐を作成する

  • git branch, git checkout To create and move to a new branch:
  • git branch <new_branch_name>
    git checkout <new_branch_name>
    

    Show all branches/active branch with:

    git branch
    git branch -v
    
  • Merge a branch: git merge (mini-workflow) If all your modifications to the code are great and pass all the (unit) tests, merge your branch with the master branch:
  • git checkout master # change to master branch
    git pull origin master # get last updates before making any changes to master
    git branch --merged # show branches that are/aren't merged with master branch
    git merge <my_new_branch_ive_worked_on>
    git push origin master
    
  • Delete a branch (mini-workflow) After you added the features from your branch and merged with master, you can delete your branch you worked on:
  • git branch --merged
    git branch -d <my_branch_ive_worked_on> # locally delete the branch
    git branch -a # show all branches: we still have <my_branch> globally
    git push origin --delete <my_branch_ive_worked_on> # globally/definitely delete the branch
    

    完全なワークフロー

    git config --global user.name
    git config --global user.email
    git clone <url> <where_to_clone>
    git branch <my_new_branch_name>
    git checkout my_new_branch
    # (make changes to the code ...)
    git status
    git commit -m "Add @function in views.py | Solve bug in models.py that fixes #8"
    git push -u origin my_new_branch
    # (wait for unit tests to complete)
    # (if all unit tests pass, then do these)
    git checkout master
    git pull origin master
    git merge my_new_branch
    git push origin master
    # (now time to delete my_new_branch)
    git branch -d my_new_branch
    git branch -a
    git push origin --delete my_new_branch
    

    ローカルの既存/完了したプロジェクト(ミニワークフロー)から新しいrepoを作成します

    • On GitHub.com website:
      • Create a new repository (write name & description)
      • (optional) Create a Readme.MD file
    • On CLI (locally):
      • Open the terminal in that folder/project's path
      • Write the following commands:
    git init
    git remote add origin https://github.com/username/projectname/.git
    git remote -v
    git pull origin master # needed to update the commit history of new repo (especially if Readme.MD or LICENSE was created)
    git status
    git add -A
    git status
    git commit -m "Initial commit from local project"
    git push origin master
    

    ローカル・ミス

    もし、私たちがSingleKeepファイルに変更を加えたならば、そのファイルを変更したくないのです。

    git checkout single_file.py
    

    And if we want to discard all changes/modifications to our files:

    git checkout -- .
    

    Also, if we want to delete/get rid of untracked files (newly created files):

    git clean -fd # force directories
    

    我々はコミットMメッセージを台無しにする。別のコミットを行わずに最後のコミットメッセージを変更したい

    WARNING: The following commands in this section will change the hash of previous commits => THIS WILL CHANGE GIT HISTORY => IF OTHER PEOPLE WILL PULL THE CHANGES AFTER EXECUTING THESE COMMANDS, THE CHANGED HISTORY COULD CAUSE BIG PROBLEMS TO THEIR REPOSITORIES. We can only change git history when we're the only one owners of the repository.

    git commit --amend -m "Corrected commit message"
    

    最後のコミットにファイルを追加するのを忘れました。私たちは、再びコミットすることなくファイルを加えたいです。

    git add file.c # get the file in the staging area
    git commit --amend # this will add the new file to last commit, also it opens a log in Vim, exit with :wq
    git log --stat # show file changes in commits
    # The last commit hash will be changed, so the git history will be changed
    

    我々は、我々の働く支店の代わりにマスター支店に委託しました。FIX :マスターへのコミット(ハッシュ)を移動し、マスターブランチの状態を返す

    # from master's branch
    git log
    # grab/copy (the first 6-7 characters of) the commit hash that we want to cherry-pick
    # change to our working branch
    git checkout [my-branch-name]
    git cherry-pick 1b818d3b
    git log
    
    # Now delete the commit from master
    git checkout master 
    git log
    # grab/copy the hash of the commit before our wrong commit
    git reset --hard 2e75207
    git log
    git clean -fd
    

    WARNING: Again, this will change git history and will cause consequences when working in a team!!! I'll write some alternatives in the next sections.

    Gitリセットの種類

    ソフトリセット


    git log # grab the hash of the commit we want to keep, the commits after that commit will be removed
    git reset --soft 2e7520782
    
    git log
    git status
    
    Git soft resetは指定されたコミットに我々を戻します、しかし、ステージング領域(「追加されたファイルは、Careedされる準備された地域」)で不必要なコミット(我々が取り除いたもの)から変更されて新しいファイルを保つでしょうgit reset HEAD -- . .

    mixed reset (デフォルト)


    git log # grab the hash of the commit we want to keep, the commits after that commit will be removed
    git reset 2e7520782
    
    git log
    git status
    
    Git mixed/default resetは指定されたコミットに我々を戻します、しかし、働くディレクトリ(「実行されていないファイルを実行している前に、我々は取り除かれたもの」)から変更された、そして、新しいファイルを保つでしょうgit checkout -- . .

    ハードリセット


    git log # grab the hash of the commit we want to keep, the commits after that commit will be removed
    git reset --hard 2e7520782
    
    git log
    git status
    
    Git hard resetは指定されたコミットに設定し、指定されたコミットにある状態に一致するようにファイル内のすべての変更を行います.
    しかしながら、ハードリセットは追跡されていないファイルに影響しません(新しく作成されたファイルは不要なコミットからですが、新しいファイルを作成しなかった場合には関係ありません).我々はこれらの追跡されていないファイルを取り除くことができますgit clean -fd .
    注意:git clean -fd 我々が誤ってプロジェクトディレクトリ(ローカルRepo)でアーカイブを解凍するとき、役に立つかもしれません、そして、我々は、つくられるすべての新しいファイルを手動で削除したくありません.

    致命的な変更:いくつかの変更をハードリセットしましたが、実際にそれらを必要としていることに気付きました。

    This "fix" works if we screwed up with git checkout HEAD^1 or git reset --hard HEAD^ . (HEAD^ is short for/same with HEAD^1).

    Luckily, git garbage collector (gc) collects/deletes (forever) lost commits after 30 days (IF WE DIDN'T ALREADY RAN git gc COMMAND).

    git reflog
    # grab the hash before executed reset command
    git checkout [0c8189]
    git log # happily see our changes back 
    
    git branch
    # HOWEVER, we're in a detached head state - we are on a branch that would be trashed in the future, so we need to save those changes in a newly-created branch
    git branch backup
    git checkout master
    git branch
    

    Now we've successfully recovered our lost changes, we can merge the backup branch with master ( git merge backup ) OR if our changes are already in master branch (do check), we can delete the backup branch ( git branch -d backup ).


    リモートサーバーにプッシュした後にコミットを解除します。Git履歴を変更せずに修正

    Undo a commit (when other people already pulled the changes), without rewriting the git history. We use git revert to create a new commit on top that reverses the changes of earlier commits.

    git log # select the commit hash THAT WE WANT TO UNDO (the wrong commit)
    git revert [1b818d3] # will also show a message in Vim, :wq to exit
    git log # you can see the new revert commit
    
    # You can also see the revert diff
    git diff [1b818d3] [hash from revert commit]
    

    git stashコマンドを使用する

    Useful for changes that you are not ready to commit yet, but you need to switch branches (or revert back to another code) work temporarily in another part of the project.
    NOTE: If you don't commit your changes (modified files) and you switch to another branch, your code will be lost.

    git branch my_branch
    git checkout my_branch
    # Make changes to the code, realize you have to switch branch for a moment
    git stash save "Worked on login function"
    # git diff / git status will show "working tree clean" -> after pushing to stash, all modifications to files are gone.
    
    # You can now switch branches / cherry-pick commits / work on other part of project, when you come back:
    # Option 1:
    git stash list
    git stash apply stash@{0} # after executing this, the saved stash will still be listed in stash list
    # Option 2:
    git stash pop # grabs the very first (top) stash, applies changes then drops that stash from stash list
    

    You can also drop/delete stashes in stash list:

    git stash list
    git stash drop stash@{2}
    
    # Or delete all the stashes in the list (assume that all those changes were junk/no longer needed)
    git stash clear
    

    NOTE: You can't merge two stashes (eg. git stash pop twice) -> will show Error: files would be overwritten by merge, please commit your changes or stash them before you merge.

    NOTE: The same stash list is accessible to every branch => Useful scenario: If you've written all your changes to code in a wrong branch (master) you need to commit to another branch, just stash the changes git stash save "Worked on login function" , git checkout another_branch , then grab changes from the stack ( stash apply stash@{?} / stash pop ).

    これらのチュートリアルから得られたクレジット/メモ(詳細な説明については非常にお勧めします)。

  • (51m) Git Tutorials for Beginners from Telusko