명령어&실습9_branch만들기
정리&요약
git의 branch는 버전관리시스템들보다 간편함.

정리&요약
branch를 해야할 때의 예시(아래): 원소스를 변경하거나 더럽히지 않고, 개발을 하고자 할때
- 고객이 특정기능을 커스터마이징 요구할때, 원소스를 유지하고 추가된 소스들과는 별개로 보관하고 싶을때,
- 불필요하다고 판단되는 기능들을 고객이 요구할때, 원소스를 유지하고 작업을하면, 나중에 그 부분을 지울수 있음
- 메인되는 작업과 테스트만을 위한 작업을 구분해서 작업하고자 할때,

정리&요약
branch는 branch할 때의 시점의 모든 기록을 같이 공유한다.


실습
새로 작업폴더 만듦.
C:\developGit\branch_test

git bash 열기

cd C:/developGit/branch_test
해당경로 이동


> git init


f1.txt파일을 만들고, a입력
> vim f1.txt
insert
a 입력
esc 누르고
:wq


> git add f1.txt


> git commit -m "1"


> vim f1.txt
insert
b 입력
esc 누르고
:wq


> git commit -am "2"
리&요약
git commit -a
유의할점: commit을 한번도 제대로 하지 않는 경우, -a가 먹히지 않음.


> git branch
현재의 branch들 상태보기

정리&요약1
*표시가 되어있음.
의미: 표시된 branch를 사용하게 됨.

정리&요약2
master의 의미
git을 처음 사용하는 순간부터 기본branch를 사용하게됨.
그 branch의 이름이 "master"


> git branch exp
"exp"라는 branch 만들기


> git branch
현재의 branch들 상태보기
exp 브랜치가 생성됨을 확인할 수 있음.


> git checkout exp
"master"에서 "exp" branch를 사용


> git branch
"exp" branch를 사용하고 있음을 확인


> git log
"exp"의 git log기록이 "master"의 로그기록이 같음을 확인할수 있음.
branch한 시점에서의 "master" branch의 log를 같이 갖게됨.


> vim f1.txt
insert 누르기
c 추가입력
esc 누르기
:wq
f1.txt를 수정 "c"를 추가입력


> git add f1.txt


> git commit -m "3"
"3"이름으로 commit하기


> git log
"exp" branch의 commit이 추가됨을 확인할수 있음



> git checkout master
"exp" 에서 "master" branch 바꿈.


> git log
"master" branch 에서의 commit 기록을 볼수 있음.
3번 commit이 없음을 확인할 수 있음.



> cat f1.txt
f1.txt에 입력한 c도 없음을 확인할 수 있음

> git checkout exp
다시 "exp" branch로 이동

> vim f2.txt
f2.txt 파일을 "exp" branch에 만들어둚.

> git add f2.txt
f2.txt를 추가함.
insert키 누름
a 입력
esc 누름
:wq

> git commit -m "4"
4번으로 commit

실제 폴더에 있는 파일에 "f2.txt" 가 있음을 확인할 수 있음(현재 branch "exp")

> git checkout "master"
f2.txt파일이 사라진 것을 확인할 수 있음.
"master" branch에서 commit한 내용에는 f2.txt가 없기때문

> git checkout "exp"
f2.txt가 생겨나는 것을 확인할 수 있음.


명령어&실습10_branch 정보 확인

> git log -- branches --decorate
작업중인 branch 이외의 기록까지 볼 수 있음.
(master)라는 의미:
commit2까지가 master라는  branch의 commit 기록임
(HEAD -> exp)의미
exp라는 branch를 현재 작업중



> git log --branches --decorate --graph
좀더 그래픽화하여 보여줌.
현재는 분기점에서의 확연한 차이점이 없어서 1줄로 표기됨.



확실한 차이점을 만들기 위해서 "master"로 이동
f3.txt를 만들어줌.
> git checkout "master"


> vim f3.txt
insert 누름
a 입력
esc 누름
:wq


> git add f3.txt


> git commit -m "5"
5라는 메세지와 함께 commit


> git log
"master" branch의 기록을 확인


> git log --branches --decorate --graph
이제 2번 commit을 기준으로 분기점이 확실히 나누어지는 것을 확인할 수 있음



> git log --branches --decorate --graph --oneline
좀더 짧게 확인할 수 있음


> git log master..exp
master를 기준으로 exp의 차이점은 뭔지 확인할 수 있음


> git log exp..master
exp를 기준으로 master의 차이점을 확인할 수 있음


> git diff master..exp
master를 기준으로 exp의 차이점을 상세하게 확인할 수 있음






명령어&실습11_branch 병합(merge)
현재 branch는 master

> git merge exp
exp를 master로 merge함

esc키 누름
:wq


> git log --branches --decorate --graph --oneline
아래 사진에서처럼 exp가 master쪽으로 합쳐진 것을 확인할 수 있음


> ls -al
폴더에서도 f2.txt가 추가되어서 늘어난것을 확인할 수 있음


master를 exp로 가져오고 싶다.
> git checkout exp
"exp" branch로 이동

> git merge master
master가 exp쪽으로 merge하기


git log --branches --decorate --graph --oneline
branch "exp"도 5번 commit을 가지게됨을 확인 할 수 있음.
exp와 master는 현재 똑같음을 확인할 수 있음을 확인 할 수 있음


> git checkout master
"master" branch 이동


> git branch -d exp
"exp" branch 삭제하기


git log --branches --decorate --graph --oneline
삭제 됨을 확인할 수 있음




명령어&실습12_merge하면서 겪는것들(fast-forward, recursive' strategy)
merge를 했을때, 겪는것들
- fast-forward: commit을 할 필요없이 바로 merge
Merge made by the 'recursive' strategy: commit을 한 다음에 하는 merge

git의 공식 메뉴얼(한국어 선택하기)




3.2 Git 브랜치 - 브랜치와 Merge 의 기초
에 나와 있는 글

위와 같은 commit 히스토리가 있다고 했을때,

> git checkout -b iss53
Switched to a new branch "iss53"
iss 53이라는 브랜치를 생성하고 'master'에서 'iss53'으로 이동(git branch iss53 과 git checkout iss53을 줄여놓은 말)



> vim index.html
> git commit -a -m 'added a new footer [issue 53]'
index.html 파일을 생성
'added a new footer [issue 53]'로 commit


git checkout master
Switched to branch 'master'
'master' branch로 이동

이때, master에서 갑자기 branch 'hotfix'를 만들고 작업을 하게됨.
> git checkout -b hotfix
Switched to a new branch 'hotfix'
'hotfix'라는 branch를 생성하고 이동
> vim index.html
> git commit -a -m 'fixed the broken email address'
[hotfix 1fb7853] fixed the broken email address
1 file changed, 2 insertions(+)
index.html 파일생성하고
'fixed the broken email address' 메세지와 함께 commit


$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
index.html | 2 ++
1 file changed, 2 insertions(+)

hotfix를 master쪽으로 merge시킴.
이때 실제로는 master 와 hotfix가 merge가 일어나는것이아니라, hotfix쪽으로 merge point가 이동하는것이 됨.
이것을 fast forward라고 부름.

> git branch -d hotfix
Deleted branch hotfix (3a0874c).
'hotfix'  branch 지우기


다시 iss53으로 돌아가기
> git checkout iss53
Switched to branch "iss53"
> vim index.html
> git commit -a -m 'finished the new footer [issue 53]'
[iss53 ad82d7a] finished the new footer [issue 53]
1 file changed, 1 insertion(+)
index.html 만들고, 'finished the new footer [issue 53]' 메세지로 commit


$ git checkout master
Switched to branch 'master'
$ git merge iss53
Merge made by the 'recursive' strategy.
README | 1 +
1 file changed, 1 insertion(+)


최종 결과







명령어&실습13_conflict해결
git bash를 연다.

> cd C:/developGit/practice2
새 작업폴더로 이동


> git init
git 시작을 알림


> vim function.txt
con.txt를 만들어서
insert키 누름
function a() {
}
입력
esc키 누름
:wq

> git add function.txt


> git commit -m '1'


> git branch contest
'contest'라는 branch를 만듦.


> git checkout contest
'contest' branch로 이동


> vim function.txt
insert키 누름

function c(){}를 function a(){} 아래에 기재
esc키 누름
:wq


> git commit -am '2'
'2'라는 메세지로 commit하기


> git checkout master
'master' branch로 이동


> vim function.txt

insert키 누름
function a(){}위에
fucntion b(){}를 기재
esc키 누름
:wq


> git commit -am '3'
방금 바꾼 파일 function.txt를 '3' 메세지와 함께 commit하기


> git merge contest

master를 기준으로 'contest' branch의 내용이 merge됨.
아래와 같은 merge관련 메세지 입력창이 나옮.
입력하지 않아도됨.
esc키 누름
:q


> cat function.txt
파일내용이 function a(){}를 기준으로 위, 아래에 b, c가 추가되었음을 알수 있음.(conflict가 없었음)




그렇다면, 같은 자리, 영역에 수정이 일어나고, 이것을 합치게된다면? conflict가 일어나게됨.
> vim function.txt

insert키 누름
a함수의 매개변수 자리에 con1추가
esc키 누름
:wq

> git commit -am '4'


> git checkout contest
'contest' branch로 이동


> vim function.txt
insert키 누름
아래와 같이 수정
esc키 누름
:wq


> git commit -am '5'
'5'라는 메세지와 함께 commit


> git checkout master
'master' branch로 이동


> cat funtion.txt
 ch

> git merge contest
같은 분기점에서 시작한 내용중에서 같은 영역의 내용이 다르게 바뀌었을때 아래와 같이 충돌이 일어나게됨.
이때는 사용자가 일일이 하나씩 보고 맞는것을 수정&저장해야만 merge가 제대로 끝남.


> cat function.txt
아래와 같이 합쳐져있음.
이것을 올바로 수정한다음 저장후, commit해야함.
======== 을 기준으로 현재 브랜치내용(<<<<<아래), contest브랜치내용(>>>>>>위)가 상이한 부분임.


> vim function.txt
insert키 누름
아래와 같이 고침
esc키 누름
:wq


> git commit -am 'complete'


정상적으로 conflict를 해결하고 commit하게됨.





명령어&실습13_stash(숨겨두기)
작업중인 branch에서 commit을 할수없는데, 다른 branch로 이동해서 작업을 해야할때, 쓰는 기능
잠깐 기록해서 멈춰두고, 다른 작업 가능
그러나, branch를 활발하게 사용하지 않으면 사용빈도가 낮음.
git stash는 최소한 버전관리가 되고 있는(tracked) 것에만 적용이됨.

WIP: Working In Process(작업중인게 저장되었다라는 의미)
stash의 list안에 저장된 내용은 삭제하지 않는한, 항상 살아있음.

stash 옵션명령 종류


실습
cd C:/developGit/practice3
practice3프로젝트로 이동


> git init


> vim f1.txt
f1.txt를 만들어 a입력, 저장


> git add f1.txt



> git commit -m '1'
'1' 메세지와 함께 commit


> git checkout -b stashtest
'stashtest' 라는 branch로 이동


> vim f1.txt
f1.txt에 b라는 내용을 수정기재 


> git status
파일이 변경되었으며, 아직 staged 상태가 아니라는 것을 알수있음


이때, 갑자기 다른 브랜치로 가서 일을 해야하는데, commit하기에는 확신할수 없음.
이런경우 stash를 사용함. 즉, commit전의 변경된 내용을 저장할수 있음

> git stash --help
stash관련 추가 옵션명령어들을 볼 수 있음.

> git stash
방금 수정된 내용이 stash 기록으로 남고, 수정되지 않은 상태로 바뀜.


> git status
수정이력이 없음을 확인할 수 있음


> git stash apply
stash에 저장해둔 최근 기록을 다시 적용함


> git stash list
stash 리스트를 볼수 있음.
아래 1개가 기록되어 있음을 볼수 있음.


> git reset --hard HEAD
기록은 없어지지만, stash 리스트는 남아 있음.


> git stash list
stash는 남아 있음을 볼수 있음.


> git stash drop
stash 최근 기록 삭제


> git stash apply; git stash drop;
최근 stash 를 적용하고 stash 지우기


> git stash list
stash 리스트에 남아 있는게 없음을 볼수 있음.


> git stash pop
git stash apply; git stash drop;과 같은 기능



f1.txt는 한번 commit된적이 있어서 tracked 되고 있음.
이상황에서 f1.txt를 수정하고, f2.txt는 새로 만들어보자.
> vim f1.txt
> vim f2.txt 


> git status
아래와 같이 나옮. (f1.txt는 한번이라도 git add 된적이 있지만, f2.txt는 한번도 git add된적이 없음을 확인가능)



> git stash
이때, stash를 해주기


> git status
stash에는 한번이라도 git add되었었던 f1.txt의 변경내용만 stash에 저장이되고, f2.txt내용은 stash에 기록이 안됨 확인.


+ Recent posts