■ Git
- 분산 버전 관리 시스템(VCS, Version Control System)
■ 개념
* 참고 : https://milooy.wordpress.com/2017/06/21/working-together-with-github-tutorial/
• Clone: 저장소 복제하기
• Pull: 개발한 코드를 받아온다.
• Add: 변경 사항을 Index에 저장
• Commit: Index에 있는 사항들을 Local Repository에 저장한다. git commit -m "fix bug"
• Push: Commit을 원격 저장소에 Upload 한다.
• Branch: 한 저장소에서 다른 개발자와 같이 작업할 때 사용, Commit이 해당 Branch를 기준으로 생기게 된다.
=> 같이 곳에서 작업을 하게 되면 충돌이 날 수 있는 가능성이 있기 때문에 Branch를 사용해야 된다.
• Merge: Branch에서 작업이 끝나고, 코드를 합칠 때 사용한다, 각 각의 Branch들을 합친다.
- Type
① Fast-Forward Merge
=> 단순히 Branch를 이동시켜 적용시킨다.
②Merge Commit
=> 양 쪽의 Branch를 통합한다.
=> Fast-Forward Merge가 가능한 경우라도 Non Fast-Forward 병합 옵션을 지정하여 위의 그림 같이 실행할 수 있다. 이 경우, Branch가 그대로 남기 때문에 그 Branch로 실행한 작업을 확인할 수 있기 때문에 관리 면에서 더 유용할 수 있다.
• Pull Request: 병합하기 전에 다른 개발자들에게 Confirm을 받기 위해 요청 메시지를 보낸다.
• Fork: 다른 원격 저장소에 있는 History를 그대로 나의 GitHub 원격 저장소에 복사한다.
=> 다른 사람이 만든 OpenSource에 기여하고 싶을 때는, Fork로 저장소 전체를 내 계정에 복제 -> Commit -> Push 후 원본 저장소로 Pull Request를 보낸다.
• Collaborators: GitHub 프로젝트를 생성하여 진행할 때,
■ 기본 및 명령어 정리
* Git 설치
- # yum install -y git: Git 설치
1) GitHub 사이트에서 저장소 생성
2) 저장소 만들기 or 로컬 저장소(Local Repository)에 복사
# cd <Directory Path>: Directory 이동
① 기존 Directory를 Git 저장소 만들기
# git init
# git add <관리할 파일들> : add를 통해 관리할 파일들 인덱스에 추가
# git add README
# git commit -m 'initial project version' : 변경 사항 commit
② 기존 저장소를 복사하기
# git clone <URL>
3) 사용자 설정
① 전역 사용자/이메일 설정
$ git config --global user.name <User Name>
$ git config --global user.email <User Email Address>
② 저장소 별 사용자/이메일 설정(해당 Directory로 이동)
$ git config user.name "User Name"
$ git config user.email <User Email Address>
$ git config --list : 설정 리스트
$ git config --global core.editor "vim" : Editor 기본 편집기로 vim을 사용하겠다는 의미
$ git config --global core.pager "cat" : log 또는 diff 같은 명령의 메시지를 출력할 때 페이지로 나누어 보여준다. 기본으로 사용하는 명령은 less 다. more 를 더 좋아하면 moremore라고 설정한다. 페이지를 나누고 싶지 않으면 빈 문자열로 설정한다.
설정 지우기
$ git config --global --unset core.page : --unset을 사용하여 지우기
4) Branch 설정
# git branch -a: Branch 리스트 확인
# git branch <Branch Name> : 현재 Branch에서 새로운 Branch 생성하기
# git checkout <Branch Name> : 해당 Branch로 Checkout
# git checkout -b <Branch Name> : 새로운 Branch를 생성하고 해당 Branch로 Checkout
# git branch -d <Branch Name> : 해당 Branch가 현재 Branch에 합쳐져 있을 경우 삭제
# git branch -D <Branch Name> : 해당 Branch가 현재 Branch에 합쳐져 있을 경우와 상관없이 삭제
# git checkout -m <기존 Branch Name> <새로운 Branch Name> : 새로운 Branch Name가 없는 경우 Branch 명으로 변경
# git cherry-pick <Commit ID> : 특정 Commit을 선택해서 합치기
# git cherry-pick -n <Commit ID> : Commit하지 않고, 특정 Commit을 선택해서 합치기
# git merge <Branch Name> : 현재 Branch가 입력한 Branch로 합쳐진다
# git merge --no-commit <Branch Name> : commit하지 않고, 입력한 Branch를 현재 Branch로 합치기
# git diff: 현재 작업 트리와 인덱스의 차이점 확인
5) 파일 생성, 수정, 삭제 후 변경 사항을 인덱스(Stage Area)에 추가
# git add <File/Directory Name>
# git rm <File Name> : Local Repository와 Remote Repository에 있는 파일 삭제
# git rm --cached <File Name> : Local Repository는 유지하고, Remote Repository에 있는 파일만 삭제
6) 상태 확인
# git status: 상태 확인
7) 인덱스에 추가된 내용을 Local Repository에 Commit 한다.
# git commit -m "<Comment>"
8) Local Repository에 있는 내용을 Remote Repository에 반영
# git remote: Remote Repository의 이름 목록을 표시
# git push -u <Remote Repository Name> <Branch Name>: Local Repository의 해당 브랜치에 반영
=> -u 옵션은 원격 저장소로부터 업데이트받은 후 push를 한다는 의미로, clone 한 사람이 여러 명인 경우 다른 사람이 push 한 상태에서 push를 하게 되면 에러가 발생하기 때문이다.
=> 만약 기존에 있던 Remote Repository를 복제한 것이 아니라면 Remote Server의 주소를 Git에게 알려줘야 한다(git remote add 명령어 사용)
# git remote add <Name> <URL> : Remote Repository를 Name으로 추가, ex> git remote add origin URL
# git remote rm <Name> : Remote Repository 제거
# git remote -v : 연결되어 있는 Remote Repository 리스트 확인
9) Local Repository를 Remote Repository에 맞춰서 갱신
# git pull: Remote Repository에 있는 변경 내용이 Local Repository에 Fetch 후, Merge 된다.
# git pull <Remote Repository> : 해당 Remote Repository를 갱신한다.
# git pull <Remote Repository Name> <Branch>: Remote Repository의 Branch를 가지고 온다.
10) 변경 내용 되돌리기
# git reset HEAD [File]: git add 취소하기
# git reset --soft Head^ : Commit을 취소하고 해당 파일들은 Staged 상태(git add 유지) 및 파일 내용 유지
# git reset Head^ : Commit을 취소하고 해당 파일들은 Unstaged 상태 및 파일 내용 유지
# git reset --hard Head^: Commit을 취소하고 해당 파일들은 Unstaged 상태로 Working Directory에 삭제, 즉 모두 취소
11) 다양한 명령어
# git log -n 10: Repository의 Commit History를 탐색한다.
# git grep "검색 단어": Repository의 파일 내용에서 검색한다.
# git reset -soft HEAD^: Commit을 취소하고 해당 파일들
# git log --oneline --decorate --graph --all : Commit History 모든 내용을 그래프 형식으로 출력.
* 참고하면 좋은 글 주소
https://rogerdudler.github.io/git-guide/index.ko.html
============================================================================
Error
- git push 할 때 발생
error: failed to push some refs to 'https://github.com/yoon2ix/docker-django.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
- # git pull origin master: 원격 Repo를 가지고 와서 fetch후 merge 시킨다.
- 다시 push를 하면 정상적으로 동작할 것이다.
'Tool > Git' 카테고리의 다른 글
Git fork & Pull request (0) | 2020.10.13 |
---|