JHH9232 Blog

git manual


|

목차


Git

git config

Option

  • --global : 전역 설정 ~/.gitconfig에 저장

  • --local : 로컬 설정 .git/config에 저장

  • --list : 설정 확인

git username 설정

git config --global user.name "username"

git email 설정

git config --global user.email "email"

git editor 설정 (기본에디터는 nano)

git config --global core.editor "vim"

git alias

git config --global alias.co "checkout"

위와 같이 설정할 시 git co 명령어로 git checkout 명령어를 사용할 수 있다.

Git 원격 저장소에서 삭제된 branch를 로컬 저장소에서 자동으로 지우기

git config --global fetch.prune true

git diff tool 변경

git config --global diff.tool bcompare
git config --global merge.tool bcompare
git config --global difftool.bcompare.cmd "$HOME"/'.bcomp.sh "$LOCAL" "$REMOTE"'
git config --global difftool.bcompare.trustExitCode true
git config --global mergetool.bcompare.cmd "$HOME"/'.bcomp.sh "$LOCAL" "$REMOTE" "$BASE" "$MERGED"'
git config --global mergetool.bcompare.trustExitCode true
git config --global mergetool.bcompare.keepBackup false

git ssh 명령어 설정

git config --global core.sshCommand "ssh -F $HOME/.ssh/config -i $HOME/.ssh/id_rsa -o UserKnownHostsFile=$HOME/.ssh/known_hosts -o StrictHostKeyChecking=no"

항상 현재 브랜치로 푸시시키기

git config --global push.default current

git add

변경내용 스테이징

git add <file/path>

git mv

파일을 이동시킬때 또는 이름을 변경할 때 사용

git mv <src file/path> <dst file/path>

git rm

git에서 파일을 삭제할 때 사용

git rm <file/path>

Option

  • --cached : 인덱스에서만 삭제

git commit

커밋

Option

  • -m : 커밋 메세지 설정

git push

서버로 커밋, 태그 (변경사항)을 푸시

git push <option>

Option

  • -u (--set-upstream) : 푸시할 remote 지정 (git push -u origin feature/mysql_dbset)
    -u 옵션은 최초 한번 설정한 후 생략하여도 푸시가 가능하다. git push origin feature/mysql_dbset

  • -f : 강제 푸시 (서버의 변경사항을 덮어씀, 실무에서 사용하지 않도록 함)

  • -d : 원격 저장소의 브랜치 삭제 (git push origin -d feature/mysql_dbset)

etc

푸시를 편하게 진행하는 방법 : https://www.daleseo.com/git-push/ \

항상 현재 브랜치로 푸시시키기 를 참조.
해당 설정을 진행하면 git push 시 현재 브랜치로 자동 푸시된다.


git fetch

서버의 변경 내용을 확인함


git pull

서버의 변경 내용을 확인하고 적용함

모든 브랜치의 내용을 가저욤

git branch -r | grep -v "\->" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git branch -r | grep -v "\->" | while read remote; do git branch --set-upstream-to="$remote" "${remote#origin/}"; done
git fetch --all
git pull --all
다른 브랜치에서 특정 브랜치의 내용을 가져올 때
  • git pull origin <branch name>

예시 ) develop 브랜치에서 master 변경내용을 가져오고 싶을 때
현재 develop 브랜치에 있다고 가정

git pull origin master
다른 브랜치에서 또다른 브랜치의 내용을 가져올 때
  • git pull origin <another branch>:<other branch>

예시 ) debug 브랜치에서 master 변경 내용을 가져오고(pull) 싶으나
현재 develop 브랜치에 있다고 가정

git pull origin master:debug

git clone

원격 저장소를 다운로드 함

특정 경로만 clone 하기

Option

  • --config : 복제할 때 설정을 변경할 수 있음

아래와 같은 옵션을 통해 clone 시 특정 ssh key, 유저명 지정 가능

git clone $1 --config user.name="$gituser" --config core.sshCommand="ssh -i $curpath/clone_id_rsa -o StrictHostKeyChecking=no"

git init

.git 디렉토리를 생성하고 깃 프로젝트를 생성/초기화함


git merge

브랜치간 내용을 머지

Option

  • --squash : 커밋을 하나로 합침

git rebase


git checkout

브랜치간 이동, conflict 해결, 파일 복원 등 다양한 용도로 사용됨

git checkout <branch name> : 브랜치간 이동

git checkout <filename> : 파일 복원 or conflict 해결

git checkout <commit hash> : 특정 커밋의 변경사항을 적용
  • --theirs : 서버의 변경사항을 적용
  • --ours : 로컬의 변경사항을 적용

git reset

해당 커밋 해쉬로 되돌린다.
이외에도 커밋 또는 스테이징을 취소 (git add 취소) 하는 역할을 진행한다.

COMMAND

  • git reset HEAD <filename> : 스테이징 취소 (git add 취소)
  • git reset HEAD^ : 마지막 커밋 취소
  • git reset HEAD~2 : 최신 2개의 커밋 취소
  • git reset <commit> : 해당 커밋으로 히스토리 되돌림

Option

  • --mixed : commit 취소 및 변경된 파일들은 unstaged 상태로 워킹 디렉터리에 보존
  • --soft : commit 취소 및 변경된 파일들은 staged 상태로 워킹 디렉터리에 보존
  • --hard : commit 취소 및 변경된 파일들은 모두 삭제됨

git revert


git branch

git branch <branch name> : 브랜치 생성

git branch <branch name> <commit hash> : 해당 커밋 해시를 가지는 브랜치 생성
  • --orphan : 빈(고아) 브랜치 생성

  • -d --delete : 브랜치 삭제

  • -D : 브랜치 강제 삭제 (--delete --force)


git cherry-pick

특정 커밋의 변경사항을 현재 브랜치에 적용한다.

git cherry-pick <commit hash>

Option

  • -n : 커밋은 생성하지 않고 변경사항만 스테이징

git remote

Option

  • -v : 원격 저장소 목록 확인

Command

  • rm : 원격저장소 삭제
  • add : 원격저장소 추가

nginx의 특정 브랜치를 나의 리포지토리로 복사하였 을 때 remote를 사용한 내용

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

# git remote rm origin
# git remote -v

# git remote add origin git@github.com:jhh9232/nginx-1.18.git
#
# git remote -v
origin  git@github.com:jhh9232/nginx-1.18.git (fetch)
origin  git@github.com:jhh9232/nginx-1.18.git (push)

# git remote add nginx https://github.com/nginx/nginx.git

# git remote -v
nginx   https://github.com/nginx/nginx.git (fetch)
nginx   https://github.com/nginx/nginx.git (push)
origin  git@github.com:jhh9232/nginx-1.18.git (fetch)
origin  git@github.com:jhh9232/nginx-1.18.git (push)

git stash

변경 사항을 임시 저장함.
주의 :Untracked 파일은 저장되지 않음

git stash

git stash <command>

Command

  • clear : 모든 stash 삭제
  • apply stash@<stash number> : stash 적용 (git stash apply stash@{stash_number})
  • drop stash@<stash number> : stash 삭제
  • list : stash 목록
  • pop : stash 적용 후 삭제 (apply + drop)
  • push : stash 생성/저장
  • show : stash 내용 확인 (스태시된 파일들의 변경사항 등 세부 정보 확인)

Opotion

  • --keep-index (-k) : 일반적으로 stash 진행 시 staged 된 파일도 모두 스태쉬 진행하지만
        git add (staged)된 내용을 제외한 나머지를 stash에 저장한다.
    • 단 Untracked 파일은 stash되지 않는다.
  • --include-untracked (-u) : Untracked 파일도 stash에 포함한다.

스테쉬 옵션 상세 설명

아래와 같이 4개의 파일이 있다고 가정하자.

스테이징된 파일 : a.c
변경된 파일 (스테이징되지 않은 파일) : b.c, c.c
새로 생성된(git project에 존재하지 않는) 파일 : d.c \

# git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a.c

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   b.c
        modified:   c.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        d.c

이 때 git stash -k (--keep-index)를 실행하면, staged된 파일인 a.c파일과 Untracked 파일인 d.c 파일은 제외한 나머지 파일들이 스테쉬에 저장된다.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        d.c

만약 git stash -k -u (--keep-index --include-untracked)를 실행하면, staged된 파일인 a.c을 제외한 나머지 파일들이 스테쉬에 저장된다.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a.c

git tag

깃의 특정 커밋 해쉬를 태그로 지정할 수 있다.

git tag <tag name> <commit hash>

태그 정보 보기

git tag

관련 Command

  • git show <tag name>: 태그 정보 확인
  • git push origin <tag name> : 태그를 원격 저장소로 푸시 참조
  • git push --delete origin <tag name> : 원격 저장소로 푸시된 태그 삭제 참조

Option

-d <tag name> : 태그 삭제


git submodule

깃 서브모듈은 다른 깃 저장소를 현재 깃 저장소의 하위 디렉토리로 가져오는 기능이다.

Command

  • git submodule add <git repository url> : 서브모듈 추가
    • git submodule add -b <branch> <git repository url> : 서브모듈 추가 (지정한 브랜치로 체크아웃)
  • git submodule update : 서브모듈 업데이트
    • --init : 서브모듈 초기화 옵션
    • --recursive : 서브모듈 내 서브모듈 업데이트
  • git submodule status : 서브모듈 상태 확인
  • git submodule init : 서브모듈 초기화

  • git submodule deinit <submodule path> : 서브모듈 제거
    • -f : 강제 옵션

git-svn


git-svn init


git-svn fetch


git-svn dcommit

Comments