버전 관리 시스템(Version Control System)
Documentation
Pro Git (한국어도 있다.)
pwdcdcd ~mkdircpcp <file 1> <file 2>lsclearexitgit <command> --helpgit init {<path>}.git 디렉토리 생성, 버전 정보 트래킹 시작git statusgit add <>git config--global user.name <username>--global user.email <email address>git commit#로 시작하는 line은 처리되지 않음i:wq-a-mgit commit -m 'commit message'git commit -am 'message' 이런 식으로 다중 옵션 사용 가능--amendgit loggit log -pgit log <commit id>git log <branch name 1>..<branch name 2>git log --reversegit diffgit diff <commit id 1>..<commit id 2>add하고 diff 하면 차이점이 없으니 안보이겠죠?git resetrevert와 비슷하나 다름git reset {--hard} <최신 상태로 하고자 하는 commit의 id> {--hard}--hard 옵션은 쉽게 사용할 수 있지만 위험하다git revertreset과 비슷하나, commit을 취소하면서 새로운 버전 생성git init 시에 .git 디렉토리가 생성되고 여기는 git이 파일 변경 사항을 추적하는 정보가 들어 있다.
이 디렉토리를 분석하는 방식으로 원리를 파악하자
git addobjects/##/####...####index어떤 파일이 어떤 id 정보에 담겨있는지 기록되어 있음
예를 들어 object/78/981922..994e85라면,
100644 78981922..994e85 0 f1.txt
cp) 시에는 파일이 같은 index를 가지게 된다.구글에 'SHA1 Online' 검색(hash 알고리즘)
commit의 원리COMMIT_EDITMSG0logs/HEADlogs/refs/heads/masterobjects/##/####...####tree ####...### parent ####...### author yck <yck@example.com> ########## +0900 committer yck <yck@example.com> ########## +0900 1(commit message)refs/heads/masterObject 파일
object 파일엔 크게 세 가지가 있다.
- 파일의 내용을 담고 있는
blob- 디렉토리의 파일명과 내용에 해당되는
blob에 대한 정보를 담고 있는treecommit
status의 원리변경 사항이 없을 때, nothing to commit임을 어떻게 알 수 있을까?
index와 최신 commit의 tree를 비교구글에 'git working directory vs index vs repository' 등의 키워드로 검색하면 괜찮은 시각 자료들이 많다.
git branchgit branch <브랜치명>git branch -d <브랜치명>git checkout <브랜치명>git log --branches --decorate {--graph} {--oneline}HEAD는 현재 checkout된 branch를 나타냄--graph--onelinegit mergeexp라는 branch의 내용을 master branch로 병합하려면 어떻게 해야 할까?master로 checkout한 뒤에 merge한다!git checkout mastergit merge exp지옥 git 20강
👀 Pro Git <-> 보고 이해하기
git stash한 브랜치에서 작업하다가 작업이 끝나지 않았는데 다른 브랜치로 checkout하여 다른 작업을 해야 하는 경우가 있다.
그런 경우 끝나지 않은 작업을 commit하지 않으면 checkout하지 못하는 경우가 발생한다. 완료되지 않은 작업을 commit하기도 애매한데..
그럴 때 stash를 하면 작업 내용을 숨겨놓았다가 HEAD version으로 이동하여 현재 branch 상태를 깔끔하게 만들고 다른 branch로 checkout할 수 있다.
💥 주의사항: 추적되고 있는 파일에만 stash 작업 가능
git stash {save}Saved working directory and index state WIP on exp: 9161e43 1 HEAD is now at 9161e43 1git status를 확인하면 'nothing to commit'이 뜨고 작업 내용도 '감춰진다'git stash applygit stash listgit stash dropgit stash apply; git stash drop;git stash pop서로 다른 브랜치에서 같은 파일에 대해 작업을 한 뒤에 merge하게 되면 충돌이 발생할 수 있다.
같은 파일이라도 다른 부분에 대해 작업하면 괜찮을 수 있는데, 같은 부분을 다르게 작업한 뒤 병합하려고 하면 충돌이 발생한다.
$ git merge <branch name>
Auto-merging source.code
CONFLICT (content): Merge conflict in common.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: common.txt
no changed added to commit (use "git add" and/or "git commit -a")
both modified$ vim common.txt
function b() {
}
<<<<<<< HEAD
function a(master) {
=======
}
function a(exp) {
>>>>>>> exp
}
function c() {
}
======= 구분자를 기준으로<<<<<<< HEAD (Current Change)>>>>>>> exp (Incoming Change)git reset을 해서 버전을 되돌려도 이전 버전이 삭제되지는 않는다
.git 디렉토리의 파일들(ORIG_HEAD 등)을 보면 알 수 있음그러면 reset한 뒤에 다시 원복하고 싶으면 어떻게 할까?
$ git reset --hard ORIG_HEAD
이렇게 하고 git log를 확인하면 reset이 취소되었음을 알 수 있다.
git reflog에서 내역 확인 가능
git checkout <commit id>detached HEAD state가 된다.git branch를 확인하면 master 브랜치가 아닌 93c9517과 같이 commit을 가리키고 있음을 확인할 수 있다..git의 HEAD 파일을 보면 HEAD가 직접 commit id를 가리키고 있음을 알 수 있다. (HEAD가 브랜치를 직접 가리키지 않고 commit을 가리키는 'detached' 상태)git checkout master 등으로 돌아갈 수 있음❓❓ '24. 1. 18. 아직 세 가지 구조가 100% 이해되지 않는다
git add했을 때 해당되는 곳git reset의 옵션들git reset --help로 확인 가능--soft, --mixed, --hard가 주로 언급됨--hard--mixed--soft'kdiff3'라는 오픈소스 머지 도구 설치
git config --global merge.tool kdiff3
git mergetool // 충돌 난 파일을 kdiff3라는 mergetool 이용하여 merge
| Branch 1 | Base | Branch 2 |
|---|---|---|
| A | A | null |
| B | B | B |
| 1 | C | 2 |
| null | D | D |
다음과 같은 상황에서 B2를 B1으로 merge하려면 어떻게 할까?
Base는 신경쓰지 않고 B1과 B2를 비교하여 병합
Branch 1 | Branch 2 | 2-way merge
---|---|---
A|null|?
B|B|B
1|2|?
null|D|?
Base를 참고하여 B1과 B2를 병합
👍 훨씬 좋다
Branch 1 | Base | Branch 2 | 3-way merge
---|---|---|---
A|A|null|null
B|B|B|B
1|C|2|?
null|D|D|null
git remote add origin <path>
git remote -v
origin home/git/git/remote (fetch)
origin home/git/git/remote (push)
git remote remove origin
git push
git push --set-upstream origin master
--set-upstreamgit push
git clone <원격 저장소 주소>
(원격 저장소 서비스 中 1)
작업하고 있던 local repository를 remote repository(GitHub)에 연결하려면?
git remote add origin https://github.com/username/repositoryname.gitorigin이라는 alias 부여git remote -v로 확인pushlocal repository -> remote repository
git push -u origin master-u 옵션--set-upstream회사 컴퓨터와 집 컴퓨터 두 기기에서 작업을 한다고 하자.
집 컴퓨터에서 commit 후 push를 하고 회사 컴퓨터에서 작업을 이어가려면 어떻게 해야 할까?
우선 git pull을 해야 한다. (remote -> local)
작업 끝나면 git push를 습관화하자.
pull 안하고 작업헀을 때push했을 수 있음pull 해서 먼저 병합해주어야 함SSH : Secure SHell
HTTPS와 다른 통신 방법.
$ ssh-keygen
cd ~/.sshid_rsa (private key)id_rsa.pub (public key)cat id_rsa.pub('24. 1. 18.) 일단 스킵. 필요할 때 다시 보기
git remote add origin <remote repo>.git에서는 config라는 파일이 수정됨config 내에 remote repo와 local repo 정보가 저장됨git push현재 branch인 master는 upstream(local repo에 연결된 remote repo)의 branch에 연결되지 않았다. 이런 뜻
fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin masterpull vs fetchfetch는 다운로드 이후 최신 커밋이 원격 저장소 몇 번인지는 기록했지만 local repository의 master 브랜치에는 변화 가하지 않음git merge origin/master로 원격 저장소(origin)의 브랜치와 병합할 수 있음pull은 remote repository에서 불러와 병합까지 해줌git tag 1.0.0 <branch-name or commit-id>git tag, git log에서 태그 확인 가능git checkout <tagname>git tag -a 1.1.0 -m "bug fix"git tag -v 1.1.0git tag -d <tagname>push하면 태그까지 upload되지는 않음git push --tags('24. 1. 18.) 스킵
merge와 비슷하나 다르다git checkout feature, git rebase master