현상관리(svn, git)/설치, 명령어

[git] branch 생성, 체크아웃, 삭제 - 커맨드

Mary's log 2024. 9. 1. 00:07

* Window 디폴트 branch명 'master',
   Mac 디폴트 branch명 'main'   

* 다른 터미널을 써도 되지만 되도록 git bash 사용
* 커맨드는 키워드를 같이 써야 효율적으로 사용가능.
키워드 관련은 검색해서 필요한 것만 메모
 


 
 
* git bash 실행 

- $ git remote update

Mac에서 브랜치 master로 잡는 오류가 계속 나서 update로 해결 


- 현재 경로 조회,  .git 경로가 있는 경로로 이동

$ pwd                     // 현재 경로 
/c/Users

$ cd ../                 // 현재에서 1단계 상위 경로로 이동
$ cd /c/abc123/Swift     // 절대경로 이동, 경로파일명은 tab 자동완성

$ ls                     // 현재 경로 dir, file
$ ls -al                 // 현재 경로 dir, file + info

 
- 브랜치 생성, 조회

$ git branch    // 모든 브랜치 조회
* master        // Windows 기본
* main          // Mac 기본

$ git branch secondBranch // 브랜치 추가

$ git branch
* master
  secondBranch

 
- 현재 연결되어있는 브랜치에서 -> 다른 브랜치로 가는걸
  checkout 이라 한다, secondBranch로 체크아웃한다.

$ git checkout secondBranch
Switched to branch 'secondBranch'
A       testImg.bmp

 
 
브랜치 삭제
키워드 추가하여 강제 삭제
삭제 관련 키워드는 검색 ㄱㄱ

$ git branch tempBranch

$ git branch
  master
* secondBranch
  tempBranch
  
$ git branch -d tempBranch
Deleted branch tempBranch (was a040c03).

$ git branch
  master
* secondBranch

 
 
* 현재 깃 로그 확인
commit id, 브랜치, 작성자명, 작성자메일, 일자일시, 커밋메시지 확인 가능
상세 키워드나 포맷을 추가하면 → 보고 싶은 내용이나 형식으로 조회 가능

$ git log

commit 2f156a50sdfscb7609e51e4d899dea (HEAD -> secondBranch, origin/master, master)
Author: abc123 <abc123@gmail.com>
Date:   Sat Aug 31 02:36:57 2024 +0900

    second commit_textFile_(Windows)

commit 577d2dsdfb0af984852a99f0a49d22
Author: abc123 <abc123@gmail.com>
Date:   Sat Aug 31 01:29:13 2024 +0900

    first commit_README_(Windows)

 
* git에 add 상태 확인
현재 체크아웃한 브랜치
  ㄴ git add가 되거나, 안 된 파일 (이걸 unstaging, staging이라고도 한다)
친절하게 (use "git ~" ) 어떤 커맨드를 쓰라고 말해줌

$ git status
On branch secondBranch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   testImg.bmp
        
        
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        testDoc.docx

 
 
 
* remote 확인, 제거
한 번 연결하면 remove 하기 전까진 계속 원격 연결되어있음

$ git remote -v
origin  https://github.com/abc123/localProject.git (fetch)
origin  https://github.com/abc123/localProject.git (push)

 
 
-  커밋메시지 입력하여 커밋 (branch : secondBranch인 상태)
git bash에서는 현재 checkout branch명이 맨마지막에 조회되니까 잘 보고 확인
secondBranch에 커밋됨

abc123@DESKTOP-123ABCD MINGW64 /c/localProject (secondBranch)

$ git commit -m "third commit_textImg_(Windows)"
[secondBranch a040c03] third commit_textImg_(Windows)
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testImg.bmp

 
-  브랜치 ( secondBranch ) 에 푸시

$ git push -u origin secondBranch
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 298 bytes | 298.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote:
remote: Create a pull request for 'secondBranch' on GitHub by visiting:
remote:      https://github.com/abc123/localProject/pull/new/secondBranch
remote:
To https://github.com/abc123/localProject.git
 * [new branch]      secondBranch -> secondBranch
branch 'secondBranch' set up to track 'origin/secondBranch'.

 

 

- defaultBranch 변경

Using 'master' as the name for the initial branch. This default branch name
is subject to change. To configure the initial branch name to use in all
of your new repositories, which will suppress this warning, call:

$ git config --global init.defaultBranch <name>

Names commonly chosen instead of 'master' are 'main', 'trunk' and
'development'. The just-created branch can be renamed via this command:

$ git branch -m <name>



 
 


 
* github.com repository로 가서 확인해본다
 
 
 - master 옆에 2개 브랜치 > 클릭하면 master[기본], secondBrach 조회됨. 
현재 master 브랜치에서
 > 첫번째 커밋
 > 두번째 커밋           // 확인 가능

 
 - secondBranch 조회
master 브랜치에서 commit push했던 'README.md', 'textFile.txt'는
secondBranch 브랜치에도 추가되지만,
secondBranch 브랜치에서 commit push했던 'testImg.bmp'은
master 브랜치에선 보이지 않고 여기 브랜치에만 commit push된 걸 확인 가능

 
 
 
* 만약
branch : master [default]
             ㄴ README.md  > commit push
branch : secondBranch
             ㄴ textFile.txt       > commit push
branch : thirdBranch
             ㄴ testImg.bmp  > commit push
             ㄴ testDoc.doc   > commit push
 
간단하게만 말하면
리드미는 나머지 2개 브랜치에서 다 보이지만
각 브랜치에 커밋푸시된 파일들은 각 브랜치에서 별도 관리 되고 있는 것. 자세한 건 좋은글 많으니 검색ㄱㄱ.
 
 
💡 회사마다 다른데
1개의 'dev 개발' 브랜치에 개발자들이 remote해서 소스 개발수정하여 commit push하고 테스트를 거쳐서 검증 완료되면
'최종 배포 버전' 또는 버전별 1개의 브랜치에 검증완료 시점을 merge머지하기도 한다.
아니면
개발자들에게 각자의 브랜치를 주고 담당 파트를 개발수정하여 commit push하고 테스트를 거쳐서 검증 완료되면
'최종 배포 버전' 또는 버전별 1개의 브랜치에 모든 브랜치를 merge머지할 수도 있다. 
 
경력이 있거나 시니어나 형상관리 담당자나 회사 운영 방식에 따라 다를 듯하다.
 
 
 
 
 

참고 문헌

더보기


깃 로그 상세 확인 커맨드

https://kin3303.tistory.com/294