Git CLI 사용을 시작하고 일상적인 프로젝트에서 이러한 워크플로우를 사용합니다.

알아요, 알아요. Git에 관한 미니 튜토리얼, 메모지, howto가 많아요. 그런데 그중 일부는 정말 미쳤어요!나는 Git를 처음 배운 사람이 어떻게 cherry pick and commit amends 같은 것을 먼저 배울 수 있는지 모르겠다.따라서 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
    

    기존 저장소를 PC의 로컬 저장소로 복제하는 대신 로컬 빈 저장소 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 워크플로우 완료 - 다른 지점에서 작업

    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
    

    기존 로컬 / 완료된 프로젝트에서 새 재구매 작성(미니 워크플로)

    • 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
    

    이 가능하다, ~할 수 있다,...

    개별 _ 파일에 대한 변경 사항이 있지만 파일에 대한 변경 사항을 더 이상 유지하지 않으려면 다음을 수행합니다.

    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
    

    우리는commit-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
    

    우리는 업무 지점이 아니라 주요 지점에 승낙했다.수정: 커밋(산열)을 마스터로 이동하고 마스터 브랜치의 상태로 돌아갑니다.

    # 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은 지정한 커밋을 되돌려주지만 변경된 파일과 새 파일은 임시 저장 영역 (파일 제출 준비 추가됨) 의 불필요한 커밋 (우리가 삭제한 파일) 에서 보존됩니다. 우리는 여전히 우리의 작업을 잃어버리지 않았지만, git reset HEAD -- . 을 사용하여 포기할 수 있습니다.

    블렌드 재설정 (기본값)


    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 혼합/기본 재설정은 지정한 커밋으로 돌아가지만 수정된 파일과 새 파일은 작업 디렉터리에서 필요하지 않은 커밋 (삭제된 파일) 에서 유지됩니다 ("추적되지 않은 파일, 추가를 수행하기 전에"영역). - 작업을 잃어버리지 않지만 사용할 수 있습니다 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 하드 재설정은 지정한 커밋으로 돌아가 파일의 모든 변경 사항을 지정한 커밋의 상태와 일치시킵니다. 작업을 잃어버렸습니다.
    단, 하드 리셋은 추적되지 않은 파일에는 영향을 주지 않습니다. (필요하지 않은 제출에서 새로 만든 파일은 필요하지 않지만, 우리가 새 파일을 만들지 않았다면 이와 무관합니다.)추적되지 않은 파일을 삭제하려면 git clean -fd 을 사용하십시오.
    참고: git clean -fd 프로젝트 디렉터리 (로컬 리포) 의 압축 파일을 의외로 풀고 새로 만든 모든 파일을 수동으로 삭제하지 않으려면 유용할 수 있습니다.

    치명적: 우리는 일부 변경 사항을 하드 리셋했지만, 우리는 실제로 그것들이 필요하다는 것을 깨달았다.git reflog (또는 마지막 제출을 삭제했다)

    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


  • 좋은 웹페이지 즐겨찾기