상세 컨텐츠

본문 제목

git 명령어 모음 및 기초 정리

Computer Science/DevOps

by Yongari 2023. 3. 3. 15:17

본문

출처 : https://unsplash.com/ko/%EC%82%AC%EC%A7%84/wX2L8L-fGeA

 

 

브랜치란?

 

브랜치 : 독립적으로 작업을 진행하기 위한 개념

브랜치 기능의 장점: 
1. 한 소스코드에서 다양한 작업이 가능함

2. 소스코드의 한 시점과 동일한 상태를 만듬(백업? 스냅샷?기능) 
3. 각각의 브랜치에서 생긴 변화가 다른 브랜치에 영향을 주지 않고 독립적으로 코딩 진행이 가능함

 

 

브랜치의 종류

통합 브랜치 (Integration Branch, master, main으로 불림)
각각의 브랜치를 통합해서 배포될 소스가 기록되는 브랜치 

피처 브랜치 (Feature Branch)
기능추가(로그인, 웹 UI, API), 버그 수정과 같은 단위 작업을 위한 브랜치
보통 feature/login, feature/test-api 와 같이 브랜치를 만들기도 함
통합 브랜치로부터 만들어내고 피처브랜치에서 작업이 완료되면 통합브랜치에 다시 머지(병합)하는 방식으로 작업이 진행됨

 

git command 정리 

 

1. 새 브랜치 생성 

1-1 git branch "새 브랜치 이름"

git branch dev2

 

2. 새 브랜치 생성 후 해당 브랜치로 전환

2-1. git switch -c "새 브랜치 이름"

2-2. git checkout -b "새 브랜치 이름"

git checkout dev2
'dev2' 브랜치로 전환합니다


3. 브랜치 목록 확인 

3-1 git branch

 git branch
  dev
* dev2

 

4. 브랜치 목록과 각 브랜치의 커밋 확인

4-1 git branch -v 

 git branch -v
  dev  50fd609 test
* dev2 50fd609 test

 

5. 브랜치 삭제

5-1 git branch -d 삭제할 브랜치

5-2 git branch -D "해당 명령어는 병합하지 않은 브랜치를 강제 삭제하는 방법"

git branch -d dev2
dev2 브랜치 삭제 (과거 50fd609).

 

6. 브랜치 전환

6-1 git switch "전환할 브랜치"
6-2 git checkout "전환할 브랜치"

git switch dev3
'dev3' 브랜치로 전환합니다

 

7. 브랜치 병합

- master 브랜치로 dev 브랜치를 병합할 때, 머지(merge)할 때 (master <- dev)

7-1 git checkout master (master 브랜치로 전환)

7-2 git merge dev (병합할 브랜치이름을  merge 뒤에 입력한다.)

$git checkout dev
'dev' 브랜치로 전환합니다

$ git merge dev3
이미 업데이트 상태입니다.

 

8. 로그에 모든 브랜치를 그래프로 표현
8-1 git log --branches --graph --decorate

git log --branches --graph --decorate
* commit 50fd6091ca754d5c157835095d1e7444b3612d19 (HEAD -> dev, dev3)
  Author: test <test@gmail.com>
  Date:   Thu Mar 2 17:09:30 2023 +0900
  
      test

 

 

9. 아직 commit 하지 않은 작업을 스택에 임시로 저장

9-1 git stash 

git stash
Saved working directory and index state WIP on dev: 50fd609 test

 

10. fork한 깃헙 레파지토리를 본인 local에서 작업할 때 

10-1 git clone https://github.com/yongari/test.git 

git clone https://github.com/yongari/test.git 
''에 복제합니다...
Username for 'https://github.com': test
Password for 'https://test@github.com': github developer settings personal access token값


11. git 상태 확인

git status 

git status
현재 브랜치 dev
커밋할 사항 없음, 작업 폴더 깨끗함

 

12. staging area에 파일 옮기기

git add . (현재 경로 전체)

git add test3.txt (test3.txt 파일을 추가)

touch test3.txt
$ git status
현재 브랜치 dev
추적하지 않는 파일:
  (커밋할 사항에 포함하려면 "git add <파일>..."을 사용하십시오)
	test3.txt

커밋할 사항을 추가하지 않았지만 추적하지 않는 파일이 있습니다 (추적하려면 "git
add"를 사용하십시오)


git status
현재 브랜치 dev
커밋할 변경 사항:
  (스테이지에서 제외하려면 "git restore --staged <file>..."을 사용하시오)
	새 파일:       test3.txt

 

13. git commit 메시지 추가 후 commit 할 때

git commit -m "test3.txt 수정"

git commit -m "test3.txt 추가" 
[dev 7fc20f0] test3.txt 추가
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test3.txt

 

14. commit한 기록을 되돌려서 바로 이전으로 돌아가는 방법

git reset HEAD^1

git reset HEAD~1

git reset HEAD^

git reset HEAD^
:~/work/test2$ git status
현재 브랜치 dev
추적하지 않는 파일:
  (커밋할 사항에 포함하려면 "git add <파일>..."을 사용하십시오)
	test3.txt

커밋할 사항을 추가하지 않았지만 추적하지 않는 파일이 있습니다 (추적하려면 "git
add"를 사용하십시오)

 

15. 내 github origin repository의 main으로 push하는 법

git push origin main

성공시
git push -u origin main
오브젝트 나열하는 중: 7, 완료.
오브젝트 개수 세는 중: 100% (7/7), 완료.
Delta compression using up to 4 threads
오브젝트 압축하는 중: 100% (5/5), 완료.
오브젝트 쓰는 중: 100% (5/5), 2.07 KiB | 2.07 MiB/s, 완료.
Total 5 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:test/studying_go.git
   12bdfa3s..0776b8d  main -> main
'main' 브랜치가 리모트의 'main' 브랜치를 ('origin'에서) 따라가도록 설정되었습니다.

실패시 
git push origin main
error: src refspec main does not match any
error: 레퍼런스를 'origin'에 푸시하는데 실패했습니다

 

16. 내 commit log를 보는 법

git log

commit 0776b8d1627317d5255bf08fe1297db90fe333aa8c (HEAD -> main, origin/main)
Author: test <test@gmail.com>
Date:   Fri Mar 3 15:04:56 2023 +0900

    adding algorithm/stack_queue.go

commit 12bdfa3787334d0a9290c2cdabd131a3fc6358359a
Author: test <test@gmail.com>
Date:   Thu Mar 2 22:38:51 2023 +0900

    studying rlp

commit 001e3b66ec230f4476e99677b96fc480c607d77bb4
Author: test <test@gmail.com>
Date:   Thu Mar 2 22:29:55 2023 +0900

    Studying rlp encoding

commit 00ab5f38873b43d3e889026a6a138c74077bf0a1e8
Author: test <test@gmail.com>
Date:   Thu Mar 2 17:22:40 2023 +0900

    Add algorithm/rabinKarp.go and Study grammer
:

 

17. local working directory를 git 관리하에 들어가게 하는 법

git init

~/work/test3$ git init
/home/robertseo/work/test3/.git/ 안의 빈 깃 저장소를 다시 초기화했습니다

 

18. 혼자 작업 진행 후 commit 기록을 남긴다. 이후 내 remote repository와 연결해서 이 코드를 적요할 때

origin이라는 이름으로 내 remote repository를 등록하기

 git remote add origin https://github.com/test/test.git 

~/work/test3$ git remote add origin https://github.com/test/test.git

 

19. main브랜치에 커밋한 기록을 방금 등록한 origin remote repository에 올리는 법

git push origin main

성공시
git push -u origin main
오브젝트 나열하는 중: 7, 완료.
오브젝트 개수 세는 중: 100% (7/7), 완료.
Delta compression using up to 4 threads
오브젝트 압축하는 중: 100% (5/5), 완료.
오브젝트 쓰는 중: 100% (5/5), 2.07 KiB | 2.07 MiB/s, 완료.
Total 5 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:test/studying_go.git
   12bdfa3s..0776b8d  main -> main
'main' 브랜치가 리모트의 'main' 브랜치를 ('origin'에서) 따라가도록 설정되었습니다.

실패시 
git push origin main
error: src refspec main does not match any
error: 레퍼런스를 'origin'에 푸시하는데 실패했습니다

 

20. 동료의 repository를 내 local에 friend라는 이름으로 등록하기

git remote add friend https://github.com/friend/test.git 

 git remote add origin https://github.com/friend/test.git

 

21. remote repository가 잘 연결됐는지 확인

git remote -v 

git remote --verbose

 

git remote -v
origin	https://github.com/test/test.git (fetch)
origin	https://github.com/test/test.git (push)

 

22. 동료의 레파지토리를 friend로 등록했고 main 브랜치가 업데이트 됐을 때 먼저 동기화하고자 할 때 

git pull friend main 

git pull https://github.com/shypang/test.git
Username for 'https://github.com': test
Password for 'https://test@github.com': github developer settings personal access token값

23. 충돌이 해결되고 staging area에 올라간 파일은 자동으로 commit 메시지가 생성됨, 자동 생성된 commit 메시지를 

적용하는 명령어는?

git commit

 

 

24. 브랜치 생성 후 해당 브랜치로 이동할 때는? 

git switch -c feat/signup

git checkout -b feat/signup

git checkout -b feat/signup
새로 만든 'feat/signup' 브랜치로 전환합니다

 

25. 생성한 브랜치 목록과 내가 어디 브랜치에 있는지 확인

git branch

:~/work/test3$ git branch
* feat/signup
  main

 

 

 

26. feat/signup-oauth 브랜치 생성 후 전환하기

git switch -c feat/signup-oauth

git checkout -b feat/signup-oauth

git checkout -b feat/signup-oauth
새로 만든 'feat/signup-oauth' 브랜치로 전환합니다

 

27. feat/signup 브랜치로 이동하기

git switch feat/signup

git checkout feat/signup

git switch feat/signup
'feat/signup' 브랜치로 전환합니다


 

28. feat/signup-oauth 브랜치 내용을 feat/signup 브랜치로 병합하는 명령어

git merge feat/signup-oauth

git merge feat/signup-oauth
이미 업데이트 상태입니다.

(다른 로그가 나올수도 있습니다. 작업한 것이 없는 브랜치에서 테스트했습니다. 단순참고)

29. 회원가입 기능 구현이 완료된 feat/signup 브랜치를 remote repository에 업로드

git push origin feat/signup

성공시
git push -u origin feat/signup
오브젝트 나열하는 중: 7, 완료.
오브젝트 개수 세는 중: 100% (7/7), 완료.
Delta compression using up to 4 threads
오브젝트 압축하는 중: 100% (5/5), 완료.
오브젝트 쓰는 중: 100% (5/5), 2.07 KiB | 2.07 MiB/s, 완료.
Total 5 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:test/studying_go.git
   12bdfa3s..0776b8d  main -> main
'main' 브랜치가 리모트의 'main' 브랜치를 ('origin'에서) 따라가도록 설정되었습니다.

실패시 
git push origin main
error: src refspec main does not match any
error: 레퍼런스를 'origin'에 푸시하는데 실패했습니다

 

30. 작업하던 코드를 잠시 다른 공간에 저장하는 방법

git stash

touch test.txt2
:~/work/test3$ git add .
:~/work/test3$ git stash
Saved working directory and index state WIP on signup: 37a77d0 first commit

 

 

 

 

git hub repository 초기 셋팅 스크린샷

github 처음 사용할 때는 레파지토리를 원격에 만들고 로컬에 있는 레파지토리를 올리는게 어렵습니다.그래서 혼자 기록하는 용도로 쓰실 때는 다음 커맨드를 많이 쓰시다보면 익숙해집니다.~

…or create a new repository on the command line

echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/shypang/test.git
git push -u origin main

 

…or push an existing repository from the command line

git remote add origin https://github.com/shypang/test.git
git branch -M main
git push -u origin main

 

…or import code from another repository

You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

https://github.com/{test}/test/import

 

'Computer Science > DevOps' 카테고리의 다른 글

Node.js 웹앱의 도커라이징  (0) 2023.01.06

관련글 더보기