git

Git 필수 명령어 요약

MasterOfAI 2021. 2. 16. 09:55

Clone : clone은 서버에 있는 거의 모든 데이터를 local에 복사하는 것이다. 

git clone 명령을 사영하면, 자동으로 orign/master 가 생긴다. 

git clone github.com/vdpa4me/pydev.git             //Git 이름의 directory 명을 만들고 그안에 복사한다. 

git clone github.com/vdpa4me/pydev.git myPythonDev //특정 directory 명을 만들고 그안에 복사한다. 

git clone -o booyah //origin 대신 booyah 라는 이름을 붙인다. 

 

Status check : 해당 Git 의 상태를 체크한다. 

git status 

git status -s[-short]  //좀더 간단한 형태로 보여 준다 

 

add : Working directory 의 untracked 상태의 파일을 staging area로 이동하며 tracked 상태 로 바꾼다. 

git add README 

 

파일 비교 : 각기 다른 영역에 있는 동일 파일의 내용을 비교 

git diff test.txt   //Commit 한것과, working directory 에 있는 것을 비교한다. 

git diff --staged test.txt  //Commit 한것과, Staging Area에 있는 것을 비교

git difftool test.txt   //differ tool 을 호출하여 비교 

 

Commit: staging area 에 있는 모든 file들을의 스냅샷을 기록한다. (locaal .git directory 에 쓴다) 

git commit                    //message 입력 tool이 호출되어, 추가적으로 message를 기록 해야 commit 이 실행된다. 

git commit -m "additional message" //message 까지 inline에 기록

git commit -a                           //gid add 명령까지 포함하여 commit 실행

git commit --amend               //(완료한 commit 을 수정하기)

 

파일제거: 

아래와 같이 rm / commit 2단계로 처리

git rm test.txt [-f]  //working directory 와 staging area 에서 file이 제거 되었지만,  git directory 에는 살아 있다.

git commit //working directory / staging area / git directory 에서 file이 제거됨 

git rm --cached README  //Staging Area에서만 제거하고 Working directory에 있는 파일은 지우지 않고 남겨 둔다시말해서 HDD에 있는 파일은 그대로 두고 Git만 추적하지 않게 한다. 

git rm log/\*.log //이 명령은 log/ 디렉토리에 있는 *.log 파일들을 모두 삭제한다. 

git rm \*~  // ~로 끝나는 파일을 모두 삭제 한다. 

 

Logging : Commit 히스토리를 시간순으로 보여준다. 

git log

git log --stat //각 commit 의 통계 정보를 조회 할 수 있다. 

git log --pretty=oneline  //oneline은 각 commit을 하나의 라인으로 보여준다. 

git log --pretty=format"%h - %an, %ar : %s" // 짧은 길이 해쉬 커밋, 저자 이름, 저자 상대적 시각, 요약

git log --pretty=format"%h %s" --graph  //브린치 머지 히스토리 보여줌

git log --since=2.weeks    //2주 이내로 commit 한 것들만 

git log --author="Ben Chang" //저자가 "Ben Chang" 인 것들 만 

git log --author="Ben Chang" --grep="delete" --all-match  //저자가 Ben Chang 이고 delet 라는 것을 포함한 것

 

되돌리기

git reset HEAD my3rdtxt.txt //Staging area에 있는 File을 Unstage 로 변경하기

git checkout -- myoldtxt.txt //commit 되어 있는 파일이 working directoy에서 Modified 되었다면, commit 된것으로 되돌리기 

 

리모트 저장소 

git remote -v  //현재 프로젝트에 등록된 리모트 저장소를 확인할 수 있다. 

git fetch origin //리모트 저장소의 데이터를 로컬로 가져오지만, 자동으로 Merge 하지 않는다.

git pull origin //리모트 저장소 브랜치에서 데이터를 가져올 뿐만 아니라 자동으로 로컬 브랜치와 Merge 시킬 수 있다.

git pull origin master //git 뿐 아니라, branch 까지 명시적으로 밝힘 

    //만일 "You asked to pull from the remote 'origin', but did not specify a branch" 라는 error 가 나오는 경우 branch 명시 

git remote show origin //리모트 저장소의 구체적인 정보를 확인할 수 있다. 

git push origin master //리모트 저장소 Update

 

브랜치 : 코드를 통째로 복사하고 나서, 원래 코드와는 상관 없이 독립적으로 개발을 진행할 수 있게 하는 것 

git branch //브랜치의 목록을 보여 준다. 

git branch -v //브랜치 목록을 보여주고, 각 branch 마다 마지막 commit message도 보여 준다. 

git branch -vv //브랜치의 좀더 자세한 상황을들 보여준다. 

git fetch --aa; git branch -vv //가장 최신 정보를 fetch 한 후 보여준다. 

git branch --merged  //현재 Checkout 한 브랜치를 기준으로 merged branch 를 보여 준다. 

git branch --no-merged //현재 Checkout 한 브랜치를 기준으로 no merged branch 를 보여 준다.  (여기에서 보여지는 brance 는 git branch -d 명령으로 삭제되지 않는다. )

git branch testing  //testging 이라는 branch 를 생성한다. 

git branch -m old_name new_name //branch 이름을 변경한다. 

git checkout testing //HEAD 가 testing branch 를 바라보게 한다. 

git checkout -b iss72 //(iss72 브랜치 생성 + checkout) 

git log --oneline --decorate --graph -all //branch 관련 정보를 잘 보여 준다.

git log --branches --decorate --graph --oneline //branch 관련 정보를 잘 보여 준다. 하나의 line으로 

git branch -D hotfix  //필요없는 hotfix 브랜치를 삭제 한다. (local 에서만 삭제되는 것이다.)

git push origin --delete iss72 //필요없는 iss72 branch를 remote에서 삭제 한다. 

 

 

git branch -r  //Remote 에 존재하는 모든 branch 를 보여 준다. 

git --no-pager branch -r  //Remote 에 존재하는 모든 branch 를 보여 준다.  pager 없이 

git --no-pager branch -r | grep -iE krg //Remote 에 존재하는 모든 branch 를 보여 준다.  pager 없이 , "krg" 가 들어간 것만 

git checkout --track origin/feature/krg_chambercontrol //브랜치 추적 

또는 

git checkout feature/krg_chambercontrol  //이렇게 줄여서도 가능함 

git branch -u [--set-upstream-to] origin/feature/krg_chambercontrol  //이미 로컬에 존재하는 브랜치가 리모트의 특정 브랜치를 추적하게 하려면 

 

특정 branch 의 이전 commit 으로 돌아가기  (Commit로 checkout)

git checkout commit#

Administrator@DESKTOP-7Q66QQR MINGW64 /e/XXXXXXXXX (develop)
$ git checkout 22343a9
Filtering content: 100% (5/5), 91.56 MiB | 5.42 MiB/s, done.
Note: switching to '22343a9'.
.....
Administrator@DESKTOP-7Q66QQR MINGW64 /e/XXXXXXXXX ((22343a9...))

Tag 로 checkout

$ git checkout FX5P4_1.1.0.26

....

Administrator@DESKTOP-7Q66QQR MINGW64 /e/KIOXIA_KS-316/ft_dna_110.26 ((FX5P4_1.1.0.26))

 

머지 : 두개의 브랜치를 하나로 합치는 것

git checkout master  //base 를 master 로 

git merge hotfix       //hotfix branch 를 땡겨서 master 에 머지한다. 

git merge --no-ff andro_hynix_ben_KS74_NIPI  //no fast fowared 옵션

 

git bash에서 gitGUI 실행하기 

git gui & //bash 에서 gitGUI를 background 로 실행한다.

git help [command] //online help

 

Git Bash 에서 Copy & Paste 하기

Copy : CTRL + INSERT

Paste : SHIFT + INSERT

 

Merge rollback

git reset --merge ORIG_HEAD //Merge rollback 

 

Tag

git tag | grep -iE FX5P4 | sort
FX5P4_1.0.2
FX5P4_1.0.3
FX5P4_1.1.0.16
FX5P4_1.1.0.18
FX5P4_1.1.0.20
FX5P4_1.1.0.23
FX5P4_1.1.0.25
FX5P4_101
FX5P4_Server_1.1.0.17

 

Example of making tracking branch.)

git clione [remote URL]

git --no-pager branch -r //Remote  에 존재하는 모든 branch를 보여준다. pager 없이 

git branch  //현재 local 에서 tracking 가능 한 branch 만 보여준다. 
* master    //Clone 했기 때문에 master branch 만 자동으로 traking 되도록 되어 있다. 

git --no-pager branch -r | grep -Ei krg  //Remote 에 존재하는 모든 branch 를 보여 준다. "krg" 가 들어간 것만
  origin/feature/krg_TCSErrors
  origin/feature/krg_chambercontrol
  origin/feature/krg_fx6_100
  origin/feature/krg_fx6_101
  origin/feature/krg_swig

git checkout --track feature/krg_chambercontrol 

git branch
* feature/krg_chambercontrol
  master
  release/ph_102

 

git blame 

프로젝트를 디버깅하기 위해 사용하기 좋은 명령어

어떤 파일의 특정 line 들의 저자가 누가인지 확인해 준다. 아래와 같은 경우 develop branch에서 HWMonitor.cpp 의 line 340 ~ 345 의 저자가 누구인지 확인하는 명령 
Administrator@...../hwmonitor (develop)

git blame -L 340,345 HWMonitor.cpp


결과는 아래와 같이 나옴:

commit   :   저자     : commit 날짜 
4f864e82 (eenginerd 2020-08-27 09:40:13 -0500 344) break;
4f864e82 (eenginerd 2020-08-27 09:40:13 -0500 345) }

 

 

gitlab 에 이미 push 된 commit 들을 삭제 시키기.

사실은 삭제 시키는 것이 아니고, 아래와 같이 commit 이 있다고 할때 , 4bc7c5 commit으로 돌아가고, 그 이후로 commit 된것들을 모두 날려 버리고 싶은 것이다. 

 $ git log --pretty=oneline
cbad85ae7d46a244a13260142aea396ef1071bb0 (HEAD -> pnbdev, origin/pnbdev, origin/HEAD)  
b08138ad16a86815f643d4486dcee0decddf209d 
e0511199f1a904fe4cfe7998e18cb2ab72a3fd44 
b6a66809961cf468e9f8b13f749d200e2b4bc7c5 
5999025a71a1ba804243bac8aeeaf69aaca3d3ab 
a74edb0bee1f302cb759fbf71fc6d69a8eb446a6 
da416a5e8468767ea3518731ed680d7a23c7919 initial commit 
 
//이렇게 자신이 돌아가고 싶은 commit 으로 rest 을 한다. $ git reset --hard b6a66809961cf468e9f8b13f749d200e2b4bc7c5

 

$ git log --pretty=oneline

b6a66809961cf468e9f8b13f749d200e2b4bc7c5 (HEAD -> pnbdev) 
5999025a71a1ba804243bac8aeeaf69aaca3d3ab 
a74edb0bee1f302cb759fbf71fc6d69a8eb446a6 
da416a5e8468767ea3518731ed680d7a23c7919a initial commit

//이렇게 정리된 내용을, server 로 push 하고 싶다면 아래와 같이 한다. 

$ git push -f origin pnbdev
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
 ! [remote rejected] .....

 

//하지만 해당 branch 가 protected 되어 있었기 때문에, gitlab에 가서, 해당 protected 를 unprotect 해야 한다. 

 

//Settings - Repository - Protected Branches 로 가서, 해당 branch를 unprotect 하면 된다. 

//Repository - Branches 에 가보면, 아래와 같이 [Protected] 마크가 빠진것을 볼 수 있다. 

 

 

// 다시한번 push를 하면, 성공


$ git push -f origin pnbdev
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To http://gitlab.o/PNB/pnb-test.git
 + cbad85a...b6a6680 pnbdev -> pnbdev (forced update)