1 . 로컬 버전 관리
2 . 중앙집중식 버전 관리 (CVCS)
3 . 분산 버전 관리 시스템 (DVCS)
'git config’라는 도구로 설정 내용을 확인하고 변경할 수 있다. Git은 이 설정에 따라 동작한다. 이때 사용하는 설정 파일은 세 가지나 된다.
Git을 설치하고 나서 가장 먼저 해야 하는 것은 사용자이름과 이메일 주소를 설정하는 것이다. Git은 커밋할 때마다 이 정보를 사용한다. 한 번 커밋한 후에는 정보를 변경할 수 없다.
사용자이름과 이메일을 등록하지 않으면 git을 사용할 수 없다.
# git bash 열고
git config --global user.name "github ID" # 아이디 등록
git config --global user.email "github 이메일" # 이메일 등록
git config -l # 설정정보 확인
git config --global core.pager '' # 좀 더 간편하게 설정정보 확인
주로 다음 두 가지 중 한 가지 방법으로 Git 저장소를 쓰기 시작한다.
mkdir git-test # 적당한 경로에 디렉터리 생성
cd git-test # 디렉터리 접속
git init # 해당 디렉터리에 git 저장소 만들기, 반드시 해당 디렉터리에 들어가서 init을 해야한다.
ls -a # .git파일 존재
ls .git # 디렉터리내에 git설정 파일 보기
git status # git 상태보기
echo "hello" > hello.txt # 파일 생성
git add hello.txt # 해당 파일을 Staging Area에 올린다
-> 파일이 Staging Area에 올라온 것을 확인
echo 'printf("hello world")' > hello.py # 파일 하나 더 생성
-> status로 확인해보면 add하지 않았기 때문에 Working dir에 있는 상태인것을 알 수 있다.
echo "# hello world" > hello.md # 파일 하나 더 생성
git add . # 현재 Working 디렉터리에 있는 여러 파일을 staging Area에 올린다
git commit
# 버전에 대한 reference로 commit 메세지를 남긴다, 보통 영어로 작성한다.
-> 메세지는 기본적으로 동사, 현재형으로 작성
-> 예를 들어 add를 통해 올라온 3개 파일이 있는데 2개가 x라는 기능, 1개가 y라는 기능을 한다면 3개를 같이 commit하는 것이 아니라, x기능을 하는 2개파일을 묶어서 먼저 commit하고 그 뒤에 y라는 파일을 따로 commit해야 한다.
Add hello world series # 제목
Add hello.md file
Add hello.py file
Add hello.txt file
# commit하고자 하는 파일들
새로운 파일 hello.c 생성 및 hello.txt파일 수정
git add .
git commit -m 'Add hello.c & Modifiy hello.txt' # commit하기
git log # commit한 기록 보기
< git ignore >
vi .gitignore # gitignore파일 작성
*.log # 모든 로그파일 무시
echo 'hello' > hello.log # 로그파일 생성
git add .
# .gitignore만 올라온다. log파일은 무시된다
# 이미 txt파일이 존재하는 상태
vi .gitignore
*.txt # 모든 txt파일 무시
git add .
git branch feature1 # 브랜치 생성
git branch # 현재 브랜치 보기
git switch feature1 # 해당 브랜치로 전환
vi hello.md # 기존에 존재하는 md파일 수정
git add .
git commit -m 'Modify hello.md'
git log or git log --oneline # 로그 확인
git switch master # master브랜치로 전환
cat hello.md # md파일 읽기
git switch feature1 # 다시 feature1브랜치로 전환
echo 'hello' > korea.md # 새로운 파일 생성
git add .
git commit -m 'create korea.md'
git switch master # 다시 master로 전환
1 . Fast-forward merge
echo hello1 > hello1.txt
git add .
git commmit -m "hello1" # master에 hello1 파일 생성
git branch feautre1
git switch feature1
echo hello2 > hello2.txt
git add .
git commmit -m "hello2" # feature1에 hello2 파일 생성
git switch master
git merge feature1 # 병합하는 기준(현재 master)으로 switch해서 병합하고자 하는 branch를 입력
git log --oneline --all --graph # 그래프로 로그 보기
2 . three-way merge
echo hello3 > hello3.txt
git add .
git commit -m "hello3"
# master에 hello3파일의 새로운 commit생성
git switch feature1
echo hello4 > hello4.txt
git add .
git commit -m "hello"
# feature1에 hello4파일의 새로운 commit생성
git switch master
git merge feature1
touch first
git add .
git commit -m '1' # master에 새로운 commit 추가
git branch test
git switch test
echo test1 > test.txt
git add .t
git commit -m 'a' # test 브랜치에 test.txt라는 commit생성
git switch master
echo first > test.txt
git add .
git commit -m 'a' # master에도 내용만 다르게 test.txt commit생성
git merge test # conflict 발생되서 merge되지 않는다
vi test.txt # 해당 파일 들어가보면 아래와같이 text가 존재
git diff # working dir에 있는 파일간의 차이점
git diff --staged # Staging Area에 올라온 파일간 차이점
git diff --staged hello.txt # staging Area에 올라온 특정 파일간 차이점
git log --oneline # commit된 파일의 파일명을 알 수 있다.
git diff c3fa6eb..1cdca6d # commit된 파일간 차이점
git diff master..f1 # branch간 차이점
git switch --detach 1cdca6d # head를 commit된 1cdca6d 바꾼다
git restore --staged c # 실수로 add한 경우
git reset HEAD~1
git reset --hard HEAD~1 # 파일의 내용까지 돌아감
git revert HEAD~1 # 특정 부분만 취소
github : 원격(remote) repository
remote저장소에서 local로 가져오는 것을 pull
- repo생성 시 public선택하면 인증없이 아무나 pull이 가능하다.
local에서 remote저장소로 보내는 것을 push
- 인증이 필요하다
git remote # 원격 repo 존재 확인
git remote -v # 원격 repo 자세한 정보 확인
mkdir git-test # 적당한 디렉터리 생성
echo "# git-test" >> README.md
git init # git 초기화
git add README.md
git commit -m "first commit"
git branch -M main # master에서 main으로 변경
git remote add origin https://github.com/suhwan12/git-test.git # 원격 저장소 등록
push를 하기위해서는 인증이 필요한데 passwd 인증이 아닌 token으로 인증해야한다.(= HTTPS 인증)
git push -u origin main # 생성한 토큰 입력
이제 push와 pull이 가능해진다.
vi test.txt # test.txt 파일 생성후 text입력
git add .
git commit -m 'test' # commit
git push # repo에 push
반대로 github에서 Add file로 새로운 파일을 생성 후 쉘에 pull명령어 입력
git pull
git branch feature1
git switch feature1 # 파일 한개
git push origin feature1 # 브랜치를 push한다
git switch main
git merge feature1
git push
HTTPS : 일일이 push할 때마다 토큰으로 인증을 진행하기에 불편하다.
SSH : ssh-keygen으로 키를 생성하여 키를 통해 유연하게 진행이 가능하다.
ssh-keygen # 키가 없다면 키 생성
cat ~/.ssh/id_rsa.pub # 퍼블릭 키파일 복사
git remote remove origin # 기존의 origin을 제거
git remote add origin < ssh주소 > # ssh로 원격 repo등록
git remote -v # 등록한 원격 repo 확인
git push -u origin main # push 할 origin을 main으로 설정
📒 한번 push한 파일은 되돌리는 것이 매우 힘들다. 특히 협업하는 사람이 많은 기업에서는 더더욱이 불가능에 가깝다. 따라서 반드시 push하기 전에 로컬에서 꼼꼼히 확인하고 신중하게 push를 해야한다.
📒 git pull = git fetch + git merge