Git 설치
sudo apt install git-all
git 설치 확인
$ git
git 명령어 도움말 확인
$ git help --all
git 버전 확인
$ git --version
git version 2.34.1
git 초기화
vagrant@kube-control1:~$ mkdir git_config
vagrant@kube-control1:~$ cd git_config/
vagrant@kube-control1:~/git_config$
**$ git init**
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /home/vagrant/git_config/.git/
# default branch 이름 master -> main 변경
**$ git config --global init.defaultBranch main
$ git init**
Reinitialized existing Git repository in /home/vagrant/git_config/.git/
**$ ls -a**
. .. .git
git 사용자명과 사용자 메일 등록
$ git config user.name "dltnals1210"
$ git config user.email "dltnalstnals14@gmail.com"
**$ ls -a .git/**
. .. HEAD branches config description hooks info objects refs
**$ cat .git/config**
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
name = dltnals1210
email = dltnalstnals14@gmail.com
전체 저장소에 사용자명과 사용자 메일 등록
$ git config --global user.name "dltnals1210"
$ git config --global user.email "dltnalstnals14@gmail.com"
새로운 저장소 또 초기화
vagrant@kube-control1:~/git_config$ cd ..
vagrant@kube-control1:~$ mkdir gitstudy_02
vagrant@kube-control1:~$ cd gitstudy_02/
vagrant@kube-control1:~/gitstudy_02$
**$ git init**
Initialized empty Git repository in /home/vagrant/gitstudy_02/.git/
$ ls -a
. .. .git
$ ls .git/
HEAD branches config description hooks info objects refs
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
이전에 등록했던 거 없음 → 새 저장소를 초기화한거라?
상태 확인
**~/gitstudy_02$ git status**
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
파일 생성 후 다시 확인
**$ echo "My Project" > README.md
$ git status**
On branch main
No commits yet
**Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md**
nothing added to commit but untracked files present (use "git add" to track)
add 명령어로 git 이 untracked file 을 추적할 수 있도록 만들기
$ echo "hello" > sub_file.txt
$ git status
On branch main
No commits yet
Changes to be **committed**:
(use "git rm --cached <file>..." to unstage)
new file: README.md
new file: sub_file.txt
sub_file.txt 내용 변경 후 다시 확인
$ vim sub_file.txt
bye
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
**new file: sub_file.txt**
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
**modified: sub_file.txt**
$ git add sub_file.txt
$ git status
On branch main
No commits yet
Changes to be **committed**:
(use "git rm --cached <file>..." to unstage)
new file: README.md
**new file: sub_file.txt**
특정 파일을 추적 관찰하고 싶지 않은 경우
$ vim .gitignore
# 또는
$ vim ~/.config/git/ignore
# 정규 표현식 사용 가능
# 확장자 제외
*.txt
*.log
# 디렉토리 제외
venv/
config/
secret/
# 전체를 제외시켰으나 특정 파일은 add, commit을 해야 할 경우
!requirements.txt
# 기준은 현재디렉토리 기준
# 모든 경로 내의 폴더나 파일을 제외하는 경우
**/info/
**/*.ipynb
주의사항
이미 add되어 있거나 commit된 파일들은 ignore 파일의 영향을 받지 않음
해결방법
git rm --cached <file>git rm --cached -r <directory>git rm --cached -r **/info/제외되지 않은 파일 및 디렉토리들 다시 제외시키고 commit 진행
git commit -m "메시지”: git add 명령어로 staged(추적, 관찰) 모드로 넣어주고
commit 으로 repository 저장소에 저장해야 함
먼저 로그 확인
$ git log --oneline
example.txt에 내용 추가
$ echo "text changed" >> example.txt
$ git status
$ git restore example.txt
$ git status
: 커밋 이력 자체를 되돌림
: 이전 시점으로 타임머신 이동하는 명령어
--soft--mixed--hard--hard 쓰면 안됨 → 작업 디렉토리 복구 안됨$ git log --oneline
$ git commit -m "insertion may files"
$ git log --oneline
vim 에디터를 코어 에디터로 지정
$ git config --global core.editor "vim"
git resetgit revert: 커밋 히스토리 확인
$ git log
가장 최근 커밋 확인
$ git log -1
파일의 어떤 파일이 변경되었는지 변경 내용 확인
$ git log -1 --name-only
git show 사용git show 7af77138e0c49d1216945ea0cbd611cc600e6ebf
줄여진 커밋 해시와 메시지만 출력
$ git log --oneline
그래프 형태로 확인
$ git log --graph --oneline --all
로그 옵션 사용시 커밋 내역을 효과적으로 확인 가능
-name-status : 파일의 변경상태를 정확하게 출력$ git log --name-status--diff-filter : 커밋 로그에서 특정 조건을 만족하는 변경 사항만 필터링하여 출력파일이 새로 추가된 로그 확인
$ git log --name-status --diff-filter=A
두 가지 옵션 같이 사용 가능
$ git log --name-status --diff-filter=AM
$ echo "첫 번째 내용" > file.txt & git add . && git commit -m "첫 번째 커밋"
$ echo "두 번째 내용" >> file.txt && git add . && git commit -m "두 번째 커밋"
$ echo "세 번째 내용" >> file.txt && git add . && git commit -m "세 번째 커밋"
git show특정 커밋의 변경 사항 확인
특정 해시 사용 가능
$ git show ee65fd8
git diff : 특정 커밋과 특정 커밋 사이의 변경 사항 확인 가능git diff <해시>^ <해시>$ git diff 6af4ec0 ee65fd8
$ git diff 6af4ec0^ ee65fd8
$ git diff ee65fd8^ ee65fd8 git log -p : 특정 커밋의 변경 사항과 로그까지 확인$ git log -p ee65fd8
$ git log -p 6af4ec0: 특정 커밋을 가리키는 가벼운 이동 가능한 포인터
브랜치를 사용하면, 하나의 소스 코드에서 독립적인 여러 기능 개발 및 버그 수정 가능
다양한 개발의 복잡성을 고려해서 다양한 작업을 분리할 때 사용함
특성
확인
$ git branch
$ git branch -r
$ git branch -a
dirA 디렉토리의 변경사항을 main 브랜치에 반영
gitstudy05 디렉토리에서 작업
main 브랜치로 이동
$ git checkout main
-> 없다는 에러 발생
$ echo "hello" > readme.txt
$ git add .
$ git commit -m "first commit"
$ git branch
$ git checkout master
브랜치 생성 후 이동
$ git checkout -b feature/update-dirA
dirA 생성
$ git status
$ mkdir dirA
$ git status
파일을 디렉토리에 추가해야 내용이 나옴
$ echo "file A1" > dirA/file1.txt
$ echo "file A2" > dirA/file2.txt
$ git status
$ git add dirA/
$ git status
$ git commit -m "Update dirA contents"
main 브랜치 이동
$ git checkout main
feature/update-dirA의 작업 내용을 main에 병합
$ git merge feature/update-dirA
불필요한 branch는 제거
$ git branch -d feature/update-dirA
$ git log
브랜치의 master와 main
깃의 버전에 따라 master, main으로 이름 갈림
전역 git 설정 확인 및 디폴트 브랜치 maste → main 변경
$ git config --global --get init.defaultBranch
$ git config --global init.defaultBranch main
$ git config --global --get init.defaultBranch
잘못된 브랜치에 커밋한 경우 잘못된 커밋을 올바른 브랜치로 옮기고, 기존 브랜치에서 커밋을 제거
cherry-pick이용
$ echo "test" > branch_main.txt
$ git add .
$ git commit -m "create main branch"
$ git branch
$ git checkout -b feature/experimental
$ echo "<h1>Hello</h1>" > index.html
$ git add .
$ git commit -m "Add homepage file index.html"
$ git log --oneline
$ git checkout main
$ git log --oneline
cherry-pick 명령어 사용 시 브랜치 커밋을 불러올 수 있음
브랜치명 입력시 마지막 커밋만 불러옴
특정 커밋을 불러오고 싶으면 해시 넣어주면 됨(브랜치 상관 없음)
$ git cherry-pick feature/experimental
브랜치 이동 후 커밋 제거
$ git checkout feature/experimental
$ git log --oneline
$ git reset --hard a445622
여러 개의 커밋을 옮겨야 하는 경우
git cherry-pick <시작 해시>^ <끝 해시>