Git - 버전 컨트롤

dragonappear·2022년 1월 9일
0

Git

목록 보기
2/3

Git repository

$ git init: 깃 레포지토리를 생성하는 명령어

git init을 실행하면 .git 디렉토리가 생성되어야 한다.

  • 깃은 파일 이름을 보존하는동안 파일의 각각의 변화점들을 기록하고 저장한다.
  • 깃은 파일의 매 버전마다 내용들을 확인할 수 있다.
  • 깃은 또한 특정 버전으로 파일을 되돌릴 수 있다.

Version control

Stage & Commit

  • Working tree , stage , repository

  • Working tree (also called working directory)
    • 파일을 편집 혹은 저장하기 위한 디렉토리
  • stage (also called staging area)
    • 버전화할 파일들이 대기열에 있는 영역
  • repository
    • 스테이지에서 대기 중인 파일 버전을 만든 후 저장하는 장소

  1. cal.py 파일을 working tree에서 편집한 후 저장한다.
  2. 수정된 cal.py 파일 버전화할려면 파일을 스테이지에 올린다.
  3. commit command를 사용하여 파일을 스테이지에서 레포지토리로 커밋한다. -> 파일의 새로운 버전을 생성한다.

working tree에서 파일을 수정

  1. $ git status : 깃 상태를 확인하는 명령어
  2. 파일 생성
class FourCal:
	def __init__(self,first,second):
		self.first = first
		self.second = second
	def setdata(self,first,second):
		self.first = first
		self.second = second
	def add(self):
		result = self.first + self.second
		return result


3. git status 다시 확인

아래와 같은 상태이다.

working tree에서 stage로 추가하기

  1. $ git add

  1. $ git status

파일의 새로운 버전 생성하기

  1. $ git commit -m "message"
  • 스테이지에 있던 cal.py은 레포지토리에 추가되었다.

  1. $ git status

  1. $ git log
  • 레포지토리에서 버전을 확인한다.

스테이징과 커밋을 동시에 처리하기

  1. cal.py 변경
class FourCal:
	def __init__(self,first,second):
		self.first = first
		self.second = second
	def setdata(self,first,second):
		self.first = first
		self.second = second
	def add(self):
		result = self.first + self.second
		return result
	def sub(self):
		result = self.first - self.second
		return result
  1. git commit-am 옵션 사용

  1. git log


Commit log

git log는 커밋 로그들을 보여준다.

  • commit hash : 커밋번호
  • Head -> master : 가장 최근의 브랜치를 가리킴
  • Authors
  • Date
  • Commit message

변경 체크하기

  1. cal.py 수정
class FourCal:
	def __init__(self,first,second):
		self.first = first
		self.second = second
	def setdata(self,first,second):
		self.first = first
		self.second = second
	def add(self):
		result = self.first + self.second
		return result
	def subtraction(self):
		result = self.first - self.second
		return result
  1. $ git status

  1. $ git diff
    레포지토리에 있는 파일의 가장 최근 버전과 수정된 파일이 얼마나 다른지 보여준다.


File Status by version

Tracked file과 Untracked file 차이점

  1. cal.py 수정
class FourCal:
	def __init__(self,first,second):
		self.first = first
		self.second = second
	def setdata(self,first,second):
		self.first = first
		self.second = second
	def add(self):
		result = self.first + self.second
		return result
	def subtraction(self):
		result = self.first - self.second
		return result
	def mul(self):
		result = self.first * self.second
		return result
  1. cal2.py 생성
class MoreFourCal(FourCal):
	def pow(self):
		result = self.first == self.second
		return result
  1. $ git status
  1. working tree, untracked file 차이
    $ git add cal.py
    $ git add cal2.py

  2. $ git status

  1. 파일 새로운 버전 만들기

$ git commit -m "add mul method and cal2.py
$ git log

  1. git log에 --stat 옵션 추가하기

unmodified, modified, staged

  1. cal2.py 수정
class MoreFourCal(FourCal):
	def pow(self):
		return self.first ** self.second
  1. $ git status

  1. working tree에서 stage로 변경 추가하기

  1. 새로운 버전 만들기

summary


Reset & Revert

워킹트리에서 수정된 파일들 원상복귀하기

  1. Changing cal.py
class FourCal:
        def __init__(self,first,second):
                self.first = first
                self.second = second
        def setdata(self,first,second):
                self.first = first
                self.second = second
        def add(self):
                return self.first + self.second
        def subtraction(self):
                return  self.first - self.second
        def mul(self):
                return  self.first * self.second
  1. git status
  1. Discarding changes in working directory

-> $ git checkout -- cal.py == $ git restore -- cal.py
-> $ cat cal.py

파일 언스테이징

  1. cal2.py 수정
class MoreFourCal(FourCal):
        def pow(self):
                result = self.first ** self.second
                return result
  1. working tree에서 stage로 이동

$ git add cal2.py
$ git status

  1. Unstaging file

$ git reset HEAD cal2.py or $ git restore --staged cal2.py
$ git status

최신 커밋으로 resetting

  1. cal2.py 수정
    $ vim cal2.py
class MoreFourCal(FourCal):
        def pow(self):
                result = self.first ** self.second
                return resul
        def div(self):
                if self.second == 0:
                        return 0
                else:
                        return self.first/self.second
  1. $ git commit -am "add div method"

  2. git log

  1. resetting the latest commit

$ git reset HEAD^

리셋 뒤에 스테이징하지 않은 변경사항 = 커밋과 스테이징이 모두 취소되었음을 의미한다.

  1. $ git log

특정 커밋으로 Resetting

  1. 파일 생성 후 스테이징, 커밋
print("1")

$ vim rev.py
$ git add rev.py
$ git commit -m "print1"

  1. 파일 변경 후 스테이징, 커밋
print("1")
print("2")

$ vim rev.py
$ git add rev.py
$ git commit -m "print2"

  1. 커밋 여러번 하기

  1. git log 확인

  1. "print2" 커밋 해쉬 복사

ecf8e1758dd93701cf9f4c66cf678929c538ed87

  1. git reset 명령어 사용(--hard 옵션 + 커밋 해쉬)

$ git reset --hard the commit hash or the print2

HEAD의 현재 위치는 ecf8e17입니다 print2 -> 최신 커밋이 되었다는 의미

$ git log

커밋 삭제없이 Revert

  1. rev.py 변경 후 커밋

  1. commit hash 복사

f2687d7affe4be2c10f6028f72ca19f82cc9394f

  1. 커맷 해시 사용해서 revert

$ git revert the commit hash of print 5

  1. 파일 버전 확인

  1. 파일 확인

0개의 댓글