본문 바로가기

Devops/Git

[Git] Git command (Branch 조회, 생성, 삭제) 명령어 모음

Branch list 조회

skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git branch (로컬 브랜치만 리스트)
* git-test-a
  main

skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git branch -a (--all) (remotes 브랜치 포함해서 리스트)
* git-test-a
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-b
  remotes/origin/main

skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git branch --list *a (pattern으로 검색)
* git-test-a

 

현재 브랜치 기반으로 새 브랜치 생성하기

신규 브랜치 생성
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a)
$ git checkout -b git-test-a-new
Switched to a new branch 'git-test-a-new'

신규 로컬 브랜치 생성 확인
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a-new)
$ git branch -a
  git-test-a
* git-test-a-new
  git-test-b
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-b
  remotes/origin/main

Git push 시 upstream 지정하라고 메나지 나옴
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a-new)
$ git push
fatal: The current branch git-test-a-new has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin git-test-a-new

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

Upstream에 push하여 Remote 브랜치 생성
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a-new)
$ git push -u origin git-test-a-new
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: 
remote: Create a pull request for 'git-test-a-new' on GitHub by visiting:
remote:      https://github.com/skkkms/git-test/pull/new/git-test-a-new
remote:
To github.com:skkkms/git-test.git
 * [new branch]      git-test-a-new -> git-test-a-new
branch 'git-test-a-new' set up to track 'origin/git-test-a-new'.

Remote 브랜치 생성 결과 확인
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-a-new)
$ git branch -a
  git-test-a
* git-test-a-new
  git-test-b
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-a-new
  remotes/origin/git-test-b
  remotes/origin/main

 

GitHub에서 Remote 브랜치 생성 모습 (git-test-a상태에서 생성 하였으므로 git-test-a의 commit 내용을 가리키고 있음0

특정 브랜치 기반으로 새 브랜치 생성하기

현재 브랜치 상태 확인
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (main)
$ git branch -a
  git-test-a
  git-test-b
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-b
  remotes/origin/main

origin/git-test-b 기반으로 새 브랜치 생성
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (main)
$ git checkout -b git-test-b-new origin/git-test-b
branch 'git-test-b-new' set up to track 'origin/git-test-b'.
Switched to a new branch 'git-test-b-new'

로컬 브랜치 생성후 push 하면 upstream branch 명을 결정하여 push 하라고 함. 
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b-new)
$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:git-test-b

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.


동일한 이름으로 remote 브랜치 생성을 위해 git push origin HEAD 수행
-> 
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b-new)
$ git push origin HEAD
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: 
remote: Create a pull request for 'git-test-b-new' on GitHub by visiting:
remote:      https://github.com/skkkms/git-test/pull/new/git-test-b-new
remote:
To github.com:skkkms/git-test.git
 * [new branch]      HEAD -> git-test-b-new

Remote 브랜치 생성 확인
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b-new)
$ git branch -a
  git-test-a
  git-test-b
* git-test-b-new
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-b
  remotes/origin/git-test-b-new
  remotes/origin/main

 

Remote 브랜치 생성 후 Github에서 Graph로 확인

 

 

브랜치 삭제하기 (로컬, 리모트)

현재 브랜치 상황
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b-new)
$ git branch -a
  git-test-a
  git-test-b
* git-test-b-new
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-b
  remotes/origin/git-test-b-new
  remotes/origin/main

로컬 브랜치 삭제 (현재 사용중인 브랜치를 바로 삭제할 수 없음)
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b-new)
$ git branch -d git-test-b-new
error: cannot delete branch 'git-test-b-new' used by worktree at 'D:/Temp/GitTest/git-test-a'

로컬 브랜치 삭제를 위해 다른 브랜치로 checkout
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b-new)
$ git checkout git-test-b
Switched to branch 'git-test-b'
Your branch is up to date with 'origin/git-test-b'.

로컬 브랜치 삭제
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b)
$ git branch -d git-test-b-new
Deleted branch git-test-b-new (was d62eac9).

로컬 브랜치 삭제 확인
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b)
$ git branch -a
  git-test-a
* git-test-b
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-b
  remotes/origin/git-test-b-new
  remotes/origin/main

리모트 브랜치 삭제 (push 명령으로 삭제 해야 함)
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b)
$ git push origin -d git-test-b-new
To github.com:skkkms/git-test.git
 - [deleted]         git-test-b-new

리모트 브랜치 삭제 확인
->
skkkm@Kindlove MINGW64 /d/Temp/GitTest/git-test-a (git-test-b)
$ git branch -a
  git-test-a
* git-test-b
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/git-test-a
  remotes/origin/git-test-b
  remotes/origin/main

 

-- The End --