Love Every Moment

〔GITHUB〕초보자를 위한 깃허브 사용법 (1) - 로컬 저장소 본문

PROGRAMMING::ETC/GIT

〔GITHUB〕초보자를 위한 깃허브 사용법 (1) - 로컬 저장소

해 송 2021. 2. 19. 21:29
반응형

 

 

1.  로컬 저장소

 

(1) mkdir 디렉터리 이름     '디렉터리 이름'을 이름으로 갖는 디렉터리 생성

(2) cd 디렉터리 이름      현재 위치에서 접근 가능한 '디렉터리이름' 디렉터리로 이동

(3) cat 파일 이름     '파일 이름'의 파일 내용을 화면에 출력

(4) ls     현재 디렉터리의 내용을 출력 

(5) git init     Git 저장소 초기화하기

 

"tutorial" 이라는 디렉터리를 로컬 저장소에 생성하고 초기화 해보자.

mkdir tutorial  →  cd tutorial  →  git init

 

 


 

2.  vim 편집기

 

(1) 입력 모드 전환

  • [i]    현재 위치부터 쓰기
  • [o]   다음 줄부터 쓰기
  • [a]   한 칸 뒤부터 쓰기

(2) [esc]    일반 모드 전환

(3) [:]       명령 모드 전환

(4) [w]     (명령 모드) 저장 

(5) [q]      (명령 모드) 종료

 

"hello.py" 라는 파일을 생성하여 저장하고 실행해보자.

vim hello.py →  print("Hello world")  →  [esc]  →  :wq [enter]  →  cat hello.py  혹은  python hello.py

  


 

3.  커밋하기 (1) 

 

(1) git status         저장소 상태 확인하기     

Untracked files  hello.py

(2) git add 파일이름         파일 기록 추적 시작하기       

Changes to be committed:

new file: hello.py

(3) git commit             커밋 메세지 작성하고 커밋하기

 

"hello.py" 를 Git 이 추적할 수 있도록 저장소에 추가하고 커밋해보자.

git status →  git add hello.py  →  git status    git commit  

 

 


 

4.  브랜치 생성, 이동, 작업

(1) git branch         현재 어떤 브랜치가 있는지 확인하기  (* : 현재 작업 중인 브랜치를 표시)

hotfix* master        

(2) git branch 브랜치이름          '브랜치이름' 브랜치 생성하기

(3) git checkout 브랜치이름        '브랜치이름' 브랜치로 이동하기

switched to branch 'hotfix'

(4) git checkout  -b  브랜치이름        '브랜치이름' 브랜치 생성과 동시에 이동하기

 

"hotfix" 라는 브랜치를 만들고 이동하여 파일 내용을 수정해보자. 

git branch   →  git branch hotfix  →  git branch    git checkout hotfix  혹은   git checkout -b hotfix
ls   vim hello.py  →  [O]  →  print("Tell Your World")    →  [esc]  →   :wq   [enter]  →   cat hello.py

 


 

5.  커밋하기 (2)

 

(1) git status         커밋할 준비가 되지 않은 변경된 파일이 있는지 알려준다 

Changes not staged for commit:

modified:  hello.py

커밋할 파일이 없는 경우: 

On branch hotfix nothing to commit, working tree clean

(2) git commit  -a        변경된 저장소 파일 모두를 커밋하기

git add 파일이름  →  git commit  명령을 한꺼번에 실행하는 기능을 한다

(3) git commit  -m  "커밋메세지"         vim 을 사용하지 않고 간단하게 커밋 메세지 남기기

 

"hotfix" 브랜치에서 변경된 파일 내용을 커밋해보자.

git commit  -a    →   [I]  →  added output "Tell Your World"  [esc]       :wq  [enter]    →  git status
또는   git commit  -am "added output 'Tell Your World' "    처럼 간단하게 커밋 메세지를 남길 수도 있다.

 

 


 

6.  브랜치 병합

 

(1) git merge 브랜치이름       현재 작업 중인 브랜치에 '브랜치이름' 브랜치를 가져와 병합하기

hello.py   |    1 +

1 file changed, 1 insertion (+)

 

(2) ls  /  cat hello.py  /  python hello.py     브랜치 내용 병합 여부 확실하게 확인하기

master 브랜치에서 작업했던 print("Hello World") 가 아니라,

hotfix 브랜치에서 작업했던 print("Hello World") print("Tell Your World") 가 실행되는 것을 확인할 수 있다.

 

"master" 브랜치를 기준으로 하여 "hotfix" 브랜치를 병합해보자.

git checkout master    git status  →   ls    cat hello.py    git merge hotfix  →  ls  →  cat hello.py

 

 


 

7.  브랜치 독립성 확인

 

"master" 브랜치의 파일과 "hotfix" 브랜치의 파일을 각각 수정하여 서로 영향을 주는지 확인해보자.

vim hello.py  print("Tell his world") 추가 git commit -a   added output "Tell his world" 커밋메세지
git checkout hotfix    vim hello.py  print("Tell her world") 추가  git commit -a  → added output "Tell her world" 커밋메세지   cat hello.py  →  hotfix 브랜치의 파일 내용을 독립적으로 수정 가능 확인!

 


 

 

8.  불필요한 파일 무시하기

 

(1) touch .gitignore        아무것도 없는 빈 파일 만들기

(2) ls  -a       파일 이름의 맨 앞이 '.'으로 시작하는 경우 기본 ls 명령어로 출력되지 않기 때문에 사용

(3) gitignore.io          공식 웹사이트에서 자신이 사용 중인 운영체제 입력하여 파일 복사 붙여넣기 

 

불필요한 파일이나 폴더를 무시하여 Git 이 추적하지 않도록 해보자.

touch .gitigonore   ls -a  gitignore.io  git add .gitignore   git commit -m "added '.gitignore' file"

 


 

9. 충돌 해결

 

(1) git merge hotfix    두 개의 브런치에서 같은 파일을 수정했기 때문에 병합하려면 충돌이 생김

CONFLICT (content): Merge conflict in hello.py

Automatic merge failed;  fix conflicts and then commit the results.

 

(2) cat hello.py            <<<<<<< HEAD        충돌 부분의 시작        =======       충돌 부분의 끝        >>>>>>> hotfix

print("Hello world")                                        print("Hello world")
print("Tell your world")                                  print("Tell your world")
<<<<<<< HEAD                                          print("Tell his world")
print("Tell his world")                                    print("Tell her world")
=======
print("Tell her world")                                    위와 같이 수정하고 저장하여 커밋한다
>>>>>>> hotfix

 

브랜치를 병합하는 과정에서 같은 파일을 수정해서 생긴 충돌을 해결해보자.

git checkout master → git merge hotfix →cat hello.py→vim hello.py→git commit -am "conflict resloved"

 

 


 

10.  git log :  기록 보기

 

(1) git log  --graph         브랜치 분기와 병합 내역을 아스키 그래프로 보여줌

(2) git log  --stat              각 커밋에서 수정된 파일의 통계 정보를 보여줌

(3) git log  -p                   각 커밋에 적용된 실제 변경 내역을 보여줌

(4) git log  --word-diff             diff 명령의 실행 결과를 단어 단위로 보여줌

(5) git log  --name-only           커밋 정보 중에서 수정된 파일의 목록만 보여줌

(6) git log  --relative-date        정확한 시간이 아니라 1일 전, 1주 전의 형식으로 보여줌

 

 

※ 이 글은 <만들면서 배우는 Git + Github 입문> 을 참고하여 작성되었습니다.

 

컴퓨터 코딩 프로그래밍 

 

반응형
Comments