로컬 저장소에서만 할 수 있는 git의 핵심 기능
- "hello world" 를 출력하는 프로그램을 만든 후, 해당 프로그램을 수정할 일이 생기는 상황에서 기존에 있던 코드에 영향이 가지 않게 작업하기
- [과정]
- 파일 생성 또는 추가
- 파일 수정
- 수정 내역을 저장소에 제출
목표 | 명령어 | 설명 |
---|---|---|
저장소 생성 | git init | 실행할 위치를 git저장소에 초기화 |
저장소에 파일 추가 | git add 파일명 | 해당 파일을 git이 추적할 수 있게 저장소에 추가 |
저장소에 수정 내역 제출 | git commit | 변경된 파일을 저장소에 제출 |
저장소 상태 확인 | git status | 현재 저장소의 상태 출력 |
저장소에 브랜치 추가 | git branch 이름 | 브랜치 만듦 |
작업 중인 브랜치 변경 | git checkout 브랜치이름 | 현재 작업 중인 브랜치이름을 변경 |
브랜치 병합 | git merge 브랜치이름 | 현재 작업 중인 브랜치에 '브랜치이름'의 랜치를 끌어와 병합 |
branch관련 명령어를 이용하면, 현재 프로젝트를 통째로 복사하고 붙여 넣기 할 필요 없이 브랜치만 옮기는 것으로 완전히 다른 작업 흐름을 가져가도록 할수 있다.
다른 브랜치에 작업하다가 다시 원래 브랜치로 돌아가면 모든 작업 내역을 그대로 복원된다.
결국 원래 브랜치의 파일이나 작업 흐릅에 전혀 영향을 주지 않으면서 새로운 작업 흐름을 만들 수 있다.
[절차]
- 브랜치 생성
- 임시 브랜치로 체크아웃
- 파일 생성또는 추가
- 파일 수정
- 수정 내역을 저장소에 제출
- 원래의 master브랜치로 체크아웃
- master브랜치에 임시 브랜치를 병합
.gitignore
파일련의 파일 목록과 파일을 구분할 수 있는 패턴의 모음으로 라인 하나가 패턴 하나를 가리킨다.
굳이 추적해야 할 필요가 없는 파일들을 무시하기 위해 사용된다.
https://www.gitignore.io/ 에서 해당 파일을 운영체제나 IDE에 맞춰서 자동으로 생성해주는 웹 앱을 사용하면 좋다.
실습1 : git 초기화 + 병합
leath@heewon2 MINGW64 ~
$ mkdir git_tutorial ## 디렉터리 만들기
leath@heewon2 MINGW64 ~
$ cd git_tutorial
leath@heewon2 MINGW64 ~/git_tutorial
$ git init ## git초기화
Initialized empty Git repository in C:/Users/leath/git_tutorial/.git/
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ vim hello.py ## hello.py 파일 생성 및 편집
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ cat hello.py ## 해당 파일에 있는 내용 그대로 출력
print("hello world!")
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ python hello.py ## 해당 파일 실행행
hello world!
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git status ## git의 상태 확인
On branch master ## 최초의 git의 이름은 master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.py
nothing added to commit but untracked files present (use "git add" to track)
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git add hello.py ## git이 hello.py 파일을 추적할 수 있도록 만듦듦
warning: in the working copy of 'hello.py', LF will be replaced by CRLF the next time Git touches it
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.py
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git commit
[master (root-commit) fadf051] create "hello world" program
1 file changed, 1 insertion(+)
create mode 100644 hello.py
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git branch
* master
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git branch hotfix ## hotfix라는 이름의 브랜치 생성성
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git branch ## 현재 내가 있는 브랜치에 *표시가 있음
hotfix
* master
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git checkout hotfix ## hotfix로 브랜치 이동
Switched to branch 'hotfix'
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ git checkout -b hotfix2 ## hotfix2라는 이름의 브랜치를 만드는 동시에 이동동
Switched to a new branch 'hotfix2'
leath@heewon2 MINGW64 ~/git_tutorial (hotfix2)
$ git checkout hotfix
Switched to branch 'hotfix'
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ vim hello.py
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ cat hello.py
print("hello world!")
print("tell your world")
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ git status
On branch hotfix
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: hello.py
no changes added to commit (use "git add"능
total 41
drwxr-xr-x 1 leath 197609 0 May 3 00:14 ./
drwxr-xr-x 1 leath 197609 0 May 3 00:12 ../
drwxr-xr-x 1 leath 197609 0 May 3 00:12 .git/
-rw-r--r-- 1 leath 197609 0 May 3 00:14 .gitignore
-rw-r--r-- 1 leath 197609 74 May 3 00:12 hello.py
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ vim .gitignore
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ git add .gitignore
warning: in the working copy of '.gitignore', LF will be replaced by CRLF the next time Git touches it
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ git commit -m "added .gitignore file"
[hotfix ca3ab7b] added .gitignore file
1 file changed, 202 insertions(+)
create mode 100644 .gitignore
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$
실습2 : 충돌 해결
leath@heewon2 MINGW64 ~/git_tutorial (hotfix)
$ git checkout master
Switched to branch 'master'
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git merge hotfix ## master에 hotfix를 합치려하는데 충돌남
Auto-merging hello.py
CONFLICT (content): Merge conflict in hello.py
Automatic merge failed; fix conflicts and then commit the result.
leath@heewon2 MINGW64 ~/git_tutorial (master|MERGING) ## 충돌
$ cat hello.py
print("hello world!")
print("tell your world")
<<<<<<< HEAD ## 충돌 난 시작점에 나타나는 마크
print("tell his world")
=======
print("tell her world")
>>>>>>> hotfix ## 충돌 난 브랜치와 충돌난 부분을 보여줌
leath@heewon2 MINGW64 ~/git_tutorial (master|MERGING)
$ vi hello.py
leath@heewon2 MINGW64 ~/git_tutorial (master|MERGING)
$ vim hello.py ## 수정
leath@heewon2 MINGW64 ~/git_tutorial (master|MERGING)
$ cat hello.py
print("hello world!")
print("tell your world")
print("tell his world")
print("tell her world")
leath@heewon2 MINGW64 ~/git_tutorial (master|MERGING)
$ git commit -a -m "confilect resolved" ## 해결 후 커밋
[master 2a03862] confilect resolved
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git log
git log :기록 보기
옵션 | 설명 |
---|---|
git log -p | 각 커밋에 적용된 실제 변경 내용을 보여준다. |
git log --word-diff | diff명령의 실행 결과를 단어 단위로 보여준다. |
git log --stat | 각 커밋에서 수정된 파일의 통계 정보르르 보여준다. |
git log --name-only | 커밋 정보 중에서 수정된 파일의 목록만 보여준다. |
git log --relative-date | 정확한 시간을 보여주는 것이 아니라 1일 전, 1주 전처럼 상대적인 시간을 비교하는 형식으로 보여준다. |
git log --graph | 브랜치 분기와 병합 내역을 아스키 그래프로 보여준다. |
예시
leath@heewon2 MINGW64 ~/git_tutorial (master)
$ git log --graph
* commit 2a03862cf1a02f02b6a7144bb1ef7db521d5a923 (HEAD -> master)
|\ Merge: 416ccbe ca3ab7b
| | Author: hiwon-lee <leather2790@naver.com>
| | Date: Fri May 3 00:38:03 2024 +0900
| |
| | confilect resolved
| |
| * commit ca3ab7b2c47f024773050be135fe9064f6da0b5a (hotfix)
| | Author: hiwon-lee <leather2790@naver.com>
| | Date: Fri May 3 00:20:21 2024 +0900
| |
| | added .gitignore file
| |
| * commit 1d59d63b59e04e03034a47d2d6874c4b90508d29
| | Author: hiwon-lee <leather2790@naver.com>
| | Date: Fri May 3 00:12:22 2024 +0900
| |
| | added output "tell her world"
| |
* | commit 416ccbea02ec3032656fd2dffc2396ff652012a4
|/ Author: hiwon-lee <leather2790@naver.com>
| Date: Fri May 3 00:11:05 2024 +0900
|
[git log의 기본 구성 요소]
- 40글자의 SHA-1 체크섬 값
- 커밋한 사용자
- 커밋시각
- 커밋 메시지
728x90
'Git이당' 카테고리의 다른 글
vim 명령어 정리 (1) | 2023.11.09 |
---|---|
Git repository(저장소)만들기 (0) | 2023.11.02 |
Git이란? (1) | 2023.10.18 |