Git Clone

원격 저장소 복제(Clone)하기 - HTTP

$ git clone {https://gitlab.com/johndoe/example.git}

원격 저장소 복제(Clone)하기 - SSH

$ git clone {git@gitlab.com:johndoe/example.git}
Git Config

유저 이름과 이메일 설정하기

$ git config --global user.name "John Doe"
$ git config --global user.email "johndoe@email.com"

merge –no-ff가 기본으로 동작하도록 설정

$ git config --global merge.ff false

pager 끄기

$ git config --global pager.branch false
$ git config --global pager.tag false
Git Repository

Git에 존재하는 폴더 올리기

$ cd {existing_folder}
$ git init --initial-branch={master}
$ git remote add origin {git@gitlab.com:johndoe/example.git}
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin {master}

Git에 존재하는 프로젝트 올리기

$ cd {existing_repo}
$ git remote rename origin old-origin
$ git remote add origin {git@gitlab.com:johndoe/example.git}
$ git push -u origin --all
$ git push -u origin --tags
Git Ignore
.gitignore 파일 자동 생성 사이트: gitignore.io

.gitignore를 적용하기 전에 Git에 올린 필요없는 파일 및 폴더 git cache에서 삭제하기

$ cd {existing_folder}
$ git rm -r --cached . # dot이 있음을 잊지말자. 캐시된 모든 것을 지우는 것을 의미한다.
$ git add . # 다시 모든 것을 추가해주면 .gitignore에 있는 제외 규칙에 있는 폴더 및 파일을 제외하고 다시 추가해준다.
$ git commit -m "apply .gitignore & clear cache"
$ git push
Create Branch

새로운 브렌치를 생성

$ git branch {new branch}

새로운 브렌치를 생성한 뒤 해당 브렌치로 이동

$ git checkout -b {new branch}
Update Branch

로컬 브렌치 이름 변경

$ git branch -m {변경전 이름} {바꿀 이름}

Default branch 변경

$ git config --global init.defaultBranch {branch}
Delete Branch

로컬 브렌치 삭제

$ git branch -D {branch}

지운 로컬 브렌치 되살리기

$ git reflog --no-abbrev
$ git checkout -b {branch_name} {브렌치 지우기 전 commit}

서브 디렉토리에 있는 모든 브렌치 삭제

$ git branch -D $(git branch -a | grep {서브 디렉토리 패턴} | awk '{print $1}')
Git Commit

변경사항 저장 후 commit하기

# 변경사항 모두 저장
$ git add .
# git commit
$ git commit -m "commit message"
Commit History

commit history 조회 (삭제된 브렌치의 커밋도 조회 가능)

$ git reflog

특정 commit 시점으로 이동하기

$ git checkout {commit}

현재로 돌아오기

$ git checkout -
Update Commit

바로 직전 commit에 변경사항 추가하기

# 변경사항 모두 저장
$ git add .
# git commit
$ git commit --amend

과거의 특정 commit 수정 (merge상태 유지)

$ git rebase -i --rebase-merges {commit}
Merge Branch

B 브랜치를 A 브랜치로 병합

$ git checkout A
$ git merge B
Merge Conflict

Conflict 난 파일 수정후 아래의 명령어 입력

# 수정한 파일만 추가
$ git add {수정한 파일}
# 전체 파일추가
$ git add . 
# merge 계속 진행하기
$ git merge --continue
Merge Abort

merge를 진행하던 도중 취소하려고 할 때 사용

$ git merge --abort
Remote Setting

현재 remote repository 확인 명령어

$ git remote -v

git remote 변경 명령어

$ git remote set-url origin {https://github.com/johndoe/example.git}

git remote 동기화 명령어

$ git remote update origin --prune
Remote Branch

Remote branch 리스트 확인하기

$ git branch -r

Remote branch가져오기

$ git checkout -t origin/{branch_name}
Git Rebase

git rebase 명령어

$ git rebase -i

Base branch 변경

$ git rebase --onto {새로운_branch_or_commit} {변경전_branch_or_commit}

과거의 특정 commit 수정 (merge상태 유지)

$ git rebase -i --rebase-merges {고치려는 commit 전 commit}