반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

테크매니아

Git : 가끔 쓰는 명령어 본문

카테고리 없음

Git : 가끔 쓰는 명령어

SciomageLAB 2024. 10. 3. 16:47
반응형

tag

태그는 특정 커밋에 대해 태깅하는 것이다. (? 뭐여 뭐가 도움이 된겨). 특정 시점에 커밋에 버전명을 달 수도 있고, demo시점에 쓰였던 코드면 demo라고 태깅을 할 수도 있다.

lightweight vs annotated

태그는 두가지가 있는데 하나는 딱 태그만 다는 lightweight고 git tag <태그명>을 통해서 만들면 된다. annotated는 태그도 달고 모든 해당 시점의 정보를 전부 다 저장한다. 다른 사람들도 이 정보를 이용할 수 있도록 annotated를 사용하는게 애지간하면 좋다고 생각한다. -a / -s / -u 옵션을 사용하여 만들 수 있다. 특히 -s옵션은 gpg키로 서명을 하게 해줄 수 있다.

서명이 필요한 경우

만일 악의적인 의도를 가지고 나쁜 코드를 몰래 밀어넣는 사람을 방지하기 위해 모든 기여자가 서명해야만 가능하게 강제할 수도 있고 반대로 메인테이너가 코드를 서명하여 밀어넣음으로써 신뢰성을 부여하는 용도로 사용하는 것이다. 가짜 토발즈가 토발즈 행세를 하면서 메인코드를 바꿀 수도 있으니...

Archive해서 Snapshot 배포하기

아래와 같이 annotation 태그를 달고 현재 브랜치에 annotate된 내용을 describe명령어로 확인할 수 있다.

git tag -a v1.5 -m 'my signed 1.5 tag'

# annotate 확인
dspbsd@DESKTOPJSC$ git describe master
v1.5

이를 응용하여 태그명으로 archive 파일을 만들고 배포하면 된다.

git archive master --prefix='project/' | gzip > `git describe master`.tar.gz

rebase

rebase는 base를 기준으로 다시 맞추는 명령어인데 아래와 같은 상황에서

다른

<pre class="wp-block-code">```
git rebase --onto master server client
# 명령어 포맷 : git rebase <base-branch> <topic-branch>

위와 같은 명령을 하면 --onto 옵션에 의해 rebase한 커밋은 master에 붙게 된다. base로 삼는 기준이 server이고 붙이려는 브랜치가 client이므로 server와 client의 공통 커밋지점까지의 커밋을 master에 떼다 붙이게 된다.

이것을 그림으로 나타내면 아래와 같다. base가 되는 server에 없고 topic-branch인 client만 갖고있는 C8, C9커밋이 --onto에 옵션에 지정된 master에 C6 커밋 뒤로 붙게 된다.

다른# git diff --check : 커밋하기 전에 공백문자 확인하기

커밋하기 전에 불필요한 공백문자가 붙어있진 않는지 확인하기 위해서는 linter를 쓰는 방법도 있고 git diff --check을 사용하는 것도 방법이다. pre-commit 훅에 넣어 커밋하기 전 체크하도록 하면 유용하다.

git diff --check

cherry-pick

cherry-pick은 특정 커밋만 쏙 떼와서 반영하는 명령어이다. 아래 예시 상황을 보면 master와 develop브랜치가 있고, develop 브랜치에는 admin과 login 기능이 추가되어있다.

<pre class="wp-block-code">```
* a5364c0 (HEAD -> refs/heads/develop) Admin page added
* 164f9ff login feature added
* b62b809 (refs/heads/master) contributor alice add line on index.html
* e08e7b2 Something changed
* 182b035 Change background color to light blue
* a2c5c7f (refs/remotes/origin/master, refs/remotes/origin/HEAD) Add simple HTML and stylesheet
* e90b632 Create empty index.html, site.css files

이때 master에 develop브랜치의 a5364c0 커밋만 반영하여 admin 페이지 추가만 하고 싶을 때 git cherry-pick a5364c0 을 하면된다.

dspbsd@DESKTOPJSC$ git cherry-pick a5364c0
[master 4aa66fe] Admin page added
 Date: Mon Feb 28 04:55:25 2022 +0900
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 admin.html

cherry-pick으로 반영한 후 새로운 커밋이 생기는 것을 볼 수 있다.

# 출력
dspbsd@DESKTOPJSC$ git lg
* 4aa66fe (HEAD -> refs/heads/master) Admin page added
| * a5364c0 (refs/heads/develop) Admin page added
| * 164f9ff login feature added
|/
* b62b809 contributor alice add line on index.html
* e08e7b2 Something changed
* 182b035 Change background color to light blue
* a2c5c7f (refs/remotes/origin/master, refs/remotes/origin/HEAD) Add simple HTML and stylesheet
* e90b632 Create empty index.html, site.css files

git shortlog

log에 적힌 내용을 짧게 요약해주는 명령어로 지정된 범위내의 변경사항을 깔끔하게 출력해준다.

dspbsd@DESKTOPJSC$ git shortlog
Alice (4):
      Change background color to light blue
      Something changed
      contributor alice add line on index.html
      Admin page added

jisuchoi01@gmail.com (2):
      Create empty index.html, site.css files
      Add simple HTML and stylesheet

git reflog : 히스토리 요약보기

reflog는 HEAD가 기록했었던 히스토리를 보여준다. shell의 command history와 유사한 기능을 한다.

dspbsd@DESKTOPJSC$ git reflog master
4aa66fe (HEAD -> master, tag: v1.5) master@{0}: cherry-pick: Admin page added
b62b809 master@{1}: commit (amend): contributor alice add line on index.html
2256f17 master@{2}: commit (amend): contributor alice add line on index.html
551edd5 master@{3}: commit: contributor alice add line on index.html
e08e7b2 master@{4}: commit: Something changed
182b035 master@{5}: commit: Change background color to light blue
a2c5c7f master@{6}: clone: from /home/dspbsd/tmp/alice/../Sample/

git add -p : 파일의 일부만 인덱스에 등록하기

add를 하면 파일이 index에 등록되고 해당 파일을 계속 추적하며 변경사항을 업로드 할 수 있다. -p 옵션을 이용하면 파일 전체 중에 일부만 patch 단위로 인덱스에 등록해서 commit할 수가 있다. -p옵션을 주면 바뀌어진 부분중에 적용할 부분을 물어본다.

Stage this hunk?

이때 hunk는 diff에 의해 변경된 일련의 코드 라인들의 묶음을 의미한다.

dspbsd@DESKTOPJSC$ git add -p index.html
diff --git a/index.html b/index.html
index 2dbb06c..4583c28 100644
--- a/index.html
+++ b/index.html
@@ -11,7 +11,8 @@
     <p>New feature.</p>
     <p>J fix this line</p>
     <p>alice add a info line</p>
-    <p>alice add a info line</p>
+    <p>first patch</p>
+    <p>second patch</p>
     <hr>
   </body>
 </html>
(1/1) Stage this hunk [y,n,q,a,d,e,?]? y

patch 단위로 reset

git reset --patch

Stash에 patch만 저장

Stash에도 필요한 부분만 저장을 할 수 있다. 대화형 프롬프트가 뜨며 변경된 데이터 중 저장할 것과 저장하지 않을 것을 지정할 수 있다.

git stash save --patch

예시 화면

$ git stash --patch
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index 66d332e..8bb5674 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -16,6 +16,10 @@ class SimpleGit
         return `#{git_cmd} 2>&1`.chomp
       end
     end
+
+    def show(treeish = 'master')
+      command(git show #{treeish})
+    end

 end
 test
Stash this hunk [y,n,q,a,d,/,e,?]? y

Saved working directory and index state WIP on master: 1b65b17 added the index file

git grep, git clean

리눅스 grep과 make clean과 유사한 동작을 한다. 두 명령어는 추적하고 있는 파일들에 대해서 검색하거나 .gitignore에 명시된 파일들을 지울 때 사용된다. 특히 grep 명령어에 -p 옵션을 주면 매칭되는 함수명이 있는 라인이나 파일을 골라 찾아준다.

git grep -p <function-name>

git log -S와 -L

곡괭이 검색

-S 옵션을 주면 string이 등장한 커밋과 사라진 커밋만 볼 수 있다.

git log -S <string>

라인 검색

filename에 functioname함수가 만들어진 순간부터 패치를 모두 보여준다. 함수의 모든 변경 양상을 볼 수 있다. 정규표현식을 허용한다. 혹시나 html 태그같은게 될까 했는데 안된다. 참고로

git log -L :functioname:filename

git rerere

rerere 명령어를 사용하려면 config를 통해 활성화를 해줘야 한다. “reuse recorded resolution” 의 줄임말로 병합 충돌을 해결했을 때 솔루션을 저장해놨다가 다시 쓰게 해주는 명령어이다. 명령어 자체는 적용된 솔루션을 복원하거나 내용을 보는데만 쓰이고 유저 상호작용이 거의 없다.

git config --global rerere.enabled true

merge, checkout, stash, rebase 등 병합할 일이 있을 때 'Resolved 'hello.rb' using previous resolution.' 라는 메세지가 추가된다. 이전에 병합을 해결한 코드를 저장해놨다가 알아서 git이 알아서 적용하여 충돌을 해결했다는 메세지이다.

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: i18n one word
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging hello.rb
CONFLICT (content): Merge conflict in hello.rb
Resolved 'hello.rb' using previous resolution.
Failed to merge in the changes.
Patch failed at 0001 i18n one word

참고문헌

git-scm.com - tag

git-scm.com - rebase하기

git-scm - revision

git-scm.com stashing과 cleaning

반응형