Let's Run Jinyeah

Git add & commit & push & 상태확인 본문

Programming/Git&Github

Git add & commit & push & 상태확인

jinyeah 2020. 1. 10. 21:14

1. Git Repository 생성

"Git Repository란?

  • git이 모든 변화를 추적하고 commit을 기록하는 디렉터리
  • . git이 숨겨진 파일로 존재함

Create a repo from Scratch

<working-directory>~ $ git init

 

Clone an Existing Repository

  • 주의! git repository안에 새로운 git repository를 만들 수 없다.
<working-directory>~ $ git clone <URL>

2. Git 상태 확인

git status

  • git repository 내의 파일 상태
    • Changes to be committed: added but not commited changes
    • Changes not staged for commit: not added
    • Untracked: not followed by Git

git log

  • 실행된 commit들에 대한 정보를 보여줌
    • SHA - commit ID
    • Author
    • Date
    • The message

git show <SHA of commit>

  • 특정 commit에 대한 정보를 보여줌 

3. Git add & Git commit & Git push

Git과 Github 구조

git add

  • working directory에 있는 파일을 Staging area로 이동
<working-directory>$ git add <file1>     //add 특정파일 

<working-directory>$ git add .           //add 디렉토리에 있는 모든 파일 

<working-directory>$ git status          //git에 의해 관리되는 파일들의 상태 확인
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    
 	  modified: src/options.py

 

git commit

  • Staging area에 있는 파일을 git repository에 저장
  • 내가 하는 첫 번째 commit이라면 git에서 사용할 텍스트 편집기를 설정한다. default 편집기는 리눅스의 텍스트 편집기인 vim이다.
  • commit을 하면 Default commit edit message가 뜨며 vim가 실행된다.
    • vim을 종료하거나 code editor를 닫으면 다시 정상적으로 commit이 완료된다.
<working-directory>$ git config --global core.editor "code --wait"  //visual studio code

<working-directory>$ git commit -m "short-messages-for-commit"
Default commit edit message

<working-directory>$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
	(use "git push" to publish your local commits)
nothing to commit, working directory clean

※ When to commit?

   Commit은 단일 작업 단위로 한다. 따라서 여러 작업을 했을 경우, 단일 작업에 관련된 파일들만 add하고 commit한다.

※ How to write commit message?

   What the commit does! (Not How or Why)

 

git push

  • Remote repository(Github)에 commit 반영
<working-directory>$ git remote  //리모트 저장소 확인
origin

<working-directory>$ git branch  //current branch 확인
* master                         //다수의 branch가 있을 경우 앞에 '*'이 붙은 것이 current branch

<working-directory>$ git push <remote> <current-branch>   //push origin master

<working-directory>$ git status  //git에 의해 관리되는 파일들의 상태 확인                          
On branch master
Your branch is up-to-date with 'origin/master'.
noting to commit, working directory clean

 

※ git push를 하면 remote repsitory에 push되지 않았던 commit들이 모두 push된다. 특정 commit만 push하고 싶다면?

<working-directory>$ git push <remote> <commit-SHA>:<remote_branch_name>   //git origin <commit-SHA>:master

참고) commit ID(SHA)는 git log를 통해 확인할 수 있다.

참고자료

https://www.udacity.com/course/ud775

 

'Programming > Git&Github' 카테고리의 다른 글

로컬 저장소를 새로운 Github 저장소에 등록  (0) 2021.02.11
Git Merge 충돌 해결 단계  (0) 2021.02.11
Git Push 실패 - Pull & Fetch & Merge  (0) 2021.02.11
Git reset & checkout & ignore  (0) 2020.02.03
Git Branch & Merge  (0) 2020.02.03
Comments