본문 바로가기

시스템/Git, Docker

Git 명령어 정리

Pull 명령어

Branch 명령어


Pull 명령어

'origin'은 clone 할시 자동으로 설정되는 원격 저장소의 이름이다.

'master'는 로컬 브랜치

'origin/master'는 원격 저장소 'origin'의 'master' 브랜치를 나타낸다.

 

위의 경우와 같이 원격 저장소와 로컬 저장소 강의 차이가 발생하는 경우 pull을 실행하여 소스를 병합한다.


Branch 명령어

1) Brach 목록 보기 및 관리하기

# 모든 Branch 목록 보기
$ git branch -a

# Branch 생성하기
$ git branch <branchname>

# Branch 전환하기
$ git checkout <branchname>

# Branch 생성과 동시에 전환하기
$ git checkout -b <branchname>

# Brach 이름 변경하기
$ git branch -m {기존 Branch 이름} {새로운 Branch 이름}

# Branch 삭제하기
$ git branch -d {삭제할 Branch 이름}

# 원격 저장소 Branch 생성하기
$ git push origin {로컬 Branch 이름}

# 원격 저장소의 Branch 삭제하기
$ git push --delete {원격 저장소 별칭} {원격 Branch 이름}

# (예시) 원격 저장소의 Branch 삭제하기:  origin 원격 저장소의 test 이름의 브랜치 삭제
$ git push --delete origin test

 

git checkout -b <branchname>

==> git branch <branchname> + git checkout <branchname> + git commit 과 동일하다.

 

 

예시) 원격 저장소의 특정 Branch를 로컬 저장소의 새로운 Branch로 가져오기

$ git checkout -b {새로운 로컬 Branch 이름} {원격 저장소 별칭}/{원격 Branch 이름}
$ git checkout {새로운 로컬 Branch 이름}
$ git pull origin {원격 Branch 이름}

# e.g.
$ git checkout -b temp origin/master
$ git checkout temp
$ git pull origin master

2) Branch 병합하기

(...)

 


※ Miscellaneous

git diff

git diff        // Unstaged 된 수정 사항 확인(마지막 커밋 전 것과 비교)

git diff --stat // 위의 내용을 요약하여 보여준다.

git diff <branch명> <다른 branch명>   //로컬 브랜치간 비교

git diff <branch명> origin/<branch명> //로컬과 원격 브랜치간 비교

git diff <commit hash> <commit hash>  //커밋간 비교

 

※ git diff 는 statged 되기 전 변경사항을 보여준다. 이미 add 명령을 통해 stage 된 코드라면, 아래의 명령을 통해 비교한다.

git diff --staged

 

git checkout

git checkout src/hello.c    // 특정 파일만 되돌리기

 

※ git branch를 전부(-a, all) 출력해보면 로컬(master) 브랜치와 원격(origin/master)브랜치가 출력된다.

origin/HEAD는 그냥 origin/master는  가리키는 브랜치라고 이해 하였다.

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

 

git remote 주소 변경 및 추가하기

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

$ git remote set-url origin https://github.com/user/repo2.git
# Change the 'origin' remote's URL

$ git remote add origin https://github.com/user/repo.git
# Set a new remote

 

remote: HTTP Basic: Access denied 문제 발생 건

// 계정정보 패스워트 설정을 초기화 하고 다시 입력
$ git config --system --unset credential.helper

// 아이디, 비번을 다시 물어보지 않는다.
$ git config credential.helper store

 

error: Your local changes to the following files would be overwritten by merge

$ git pull origin master
error: Your local changes to the following files would be overwritten by merge:

$git stash # 다음 방법으로 해결

stash는 현재 디렉토리 파일을 임시로 백업하고 깨끗한 상태로 돌린다.

 

 

 

[Reference]

stackoverflow.com/questions/3527856/show-git-diff-on-file-in-staging-area

backlog.com/git-tutorial/kr/stepup/stepup2_4.html

반응형