오늘도 개발

[Git] Git과 Github 본문

웹 프로그래밍/Git

[Git] Git과 Github

Sueeeeeee 2022. 3. 17. 17:16

Git

- Git : 버전 관리 프로그램

- Github : Git을 사용하는 프로젝트를 올릴 수 있는 웹사이트

Repository

한 프로젝트의 모든 파일이 저장된 곳. 

프로젝트 폴더라고 생각하면 된다.

 

- local repository : 내 컴퓨터에 저장된 프로젝트 폴더

- remote repository : 온라인(github.com)에 저장된 프로젝트 폴더

주요 명령어

git init

git repository를 초기화하는 명령 .git 폴더 생성.

git init을 실행한 폴더의 모든 변경사항을 추적하기 시작함.

 

git status

repository 내 파일 상태 확인

 

git add

commit할 파일을 staging area에 올림.

 

git commit -m 'message'

commit 실행 (현재 버전 스냅샷 찍기)

 

git log

commit 이력 보기(commit 하나가 버전 하나)

 

git push

스테이징에 있는 파일을 remote repository에 올림

사용 방법

1. git 초기화

- git을 사용할 디렉터리에서 실행해야 함

git init

2. remote repository와 local repository 연결하기

github.com에 이미 존재하는 remote repository를 내 컴퓨터에 다운받는 경우에만 필요한 과정

# git clone <repository url>
git clone https://github.com/eungang3/gittest.git

내 컴퓨터의 local repository에서 작업하다가 github.com에 remote repository를 만들어서 올리는 경우.

# origin이라는 변수의 값으로 remote repository인 https://github.com/eungang3/gittest.git를 넣겠다는 뜻
# 통상 origin 변수에는 remote repository의 주소를 넣음 
git remote add origin https://github.com/eungang3/gittest.git

 

2. Add

local repository에 있는 파일 중 commit할 파일을 staging area에 추가하기

# 폴더 내 모든 파일 추가
git add .

# 명시한 파일만 추가
git add index.html

 

3. Commit

- add한 파일들의 스냅샷 남기기

git commit -m "some message"

# remote에 push한 적 있는 파일인 경우 add와 commit 한번에 가능
git commit -am "some message"

- Remote repository에 있는 파일과 add한 파일의 다른 점 확인하기

git status

 

4. Push

commit한 파일들을 remote repository에 올리기

- upstream(연결할 remote repository)과 upstream의 브랜치를 명시해 주어야 함

# origin의 master 브랜치에 올리겠다는 뜻
git push origin master

- upstream을 생략하고 push, pull할 수 있게 설정하는 법 (브랜치 생성하고 push할 때마다 설정해야 함)

# 앞으로 해당 local의 현재 branch의 upstream은 항상 origin master라고 git에게 알려줌

git push --set-upstream origin master
# 줄여서 git push -u origin master로 써도 됨

 

설정 완료한 이후엔 push, pull만 써도 됨

git push

 

5. Pull

remote repository에 있는 파일을 local repository로 가져오기

git pull

이전 commit으로 롤백하기

git log

repository의 커밋 내역

# 페이지로 확인 (확인 후 q로 나오기)
git log

# 터미널에서 바로 확인
git --no-pager log --oneline

git reflog

repository의 모든 커밋 내역. 하드 리셋 후 log에 뜨지 않는 내역도 들어있음.

git reset

1. 옵션을 hard로 주는 경우 : git reset --hard <commit hash>

working directory, stage area, repository 모두 리셋됨.

remote repository에 저장된 상태로 복구하는 경우 git reset --hard origin/master 

add, commit 실행 이전 시점으로 롤백

 

예) 

git log 실행

f8 work.txt 텍스트를 3으로 변경 <- HEAD

9a work.txt 텍스트를 2로 변경

53 work.txt 텍스트를 1로 변경

 

git reset --hard 53 실행

git log 실행

53 work.txt 텍스트를 1로 변경 <- HEAD

 

staging area에 올라온 것 아무것도 없고현재 work.txt 파일 속 텍스트는 1로 뜸.

 

2. 옵션을 soft로 주는 경우 : git reset --soft <commit hash>

working directory, stage area에 있는 파일은 그대로고 repository만 리셋됨

add 이후 commit하기 직전 시점으로 롤백.

 

예)

git log 실행

f8 work.txt 텍스트를 3으로 변경 <- HEAD

9a work.txt 텍스트를 2로 변경

53 work.txt 텍스트를 1로 변경

 

git reset --soft 9a 실행

git log 실행

9a work.txt 텍스트를 2로 변경 <- HEAD

53 work.txt 텍스트를 1로 변경

 

staging area에 2.txt가 올라와있고

현재 work.txt 파일 속 텍스트는 3으로 뜸.

 

work.txt를 2, 3으로 변경

git add work.txt 실행

git commit -am "work.txt를 2, 3으로 변경" 실행

 

git log 실행

9a work.txt 텍스트를 2, 3으로 변경 <- HEAD

53 work.txt 텍스트를 1로 변경

 

 

3. 옵션을 사용하지 않는 경우 (디폴트 옵션 mixed 적용) : git reset <commit hash>

stage area와 repository만 리셋됨. working directory의 파일은 리셋되지 않음.

add 하기 직전으로 롤백.

 

git log 실행

f8 work.txt 텍스트를 3으로 변경 <- HEAD

9a work.txt 텍스트를 2로 변경

53 work.txt 텍스트를 1로 변경

 

git reset 9a 실행

git log 실행

9a work.txt 텍스트를 2로 변경 <- HEAD

53 work.txt 텍스트를 1로 변경

 

현재 work.txt 파일 속 텍스트는 3으로 뜨고

git status를 하면 수정된 work.txt 파일이 staging에 추가되지 않았다고 뜸.

Branching

브랜치란?

독립적으로 작업을 진행하는 공간.

디폴트 브랜치는 main(또는 master)으로 git init 실행시 자동으로 생성된다. 

 

프로젝트 진행 시 기능별로 브랜치를 만들어 작업하다가

작업이 끝나면 메인 브랜치에 병합하는 방식으로 활용할 수 있다. 

 

head란?

현재 사용중인 브랜치의 최신 commit을 나타내는 이름 (디폴트로 master branch 가리킴), *로 표시

 

출처: CS50 Web Programming with Python and JavaScript, Lecture 1

저장소 내 모든 브랜치 보기

git branch

새 브랜치 만들기

# 새 브랜치 만들기
git branch <branch name>

브랜치 전환하기

# 존재하는 브랜치로 이동
git checkout <branch name>

# 새 브랜치 만들고 이동
git checkout -b <branch name>

마스터 브랜치에 병합하기

# 마스터 브랜치로 이동
git checkout master

# 병합
git merge <branch name>

브랜치 push하기

# remote repository에 해당 브랜치가 없는 경우
# remote repository에 브랜치 만들고, local branch의 upstream으로 설정한다는 뜻
git push --set-upstream origin <branch name>

# remote repository에 해당 브랜치가 있는 경우
git push

Merge Conflicts

한 repository에서 작업하는 사람들이 같은 파일을 다른 방식으로 고치고 commit할 때 발생.

(충돌 일어나는 부분 보여주는 화면으로 자동 이동함.)

한 repository의 브랜치를 merge할 때도 발생.

(충돌 일어난 파일 열면 아래와 같이 적혀있음)

 

충돌 일어나는 부분 수정하면 다시 진행 가능

# 한 사람은 b = 2, 다른 사람은 b = 3으로 작성한 경우 
# accept current, accept incoming 중 선택 가능

a = 1
<<<<< HEAD
b = 2
=====
b = 3
>>>>> 56782736387980937883
c = 3

 

 

 

출처

누구나 쉽게 이해할 수 있는 Git 입문

CS50's Web Programming with Python and JavaScript - 1. Git

'웹 프로그래밍 > Git' 카테고리의 다른 글

Git stash  (0) 2023.02.18
Git checkout, restore, switch  (0) 2023.02.18
Git rebase  (0) 2022.08.02
Git flow  (0) 2022.08.02
[Git] push, merge, pull로 브랜치를 항상 최신화하는 방법  (0) 2022.07.09