1
`GIT - 기초 1, 기본 흐름과 명령어` 편에 이어 기본 명령어 이외의 유용한 명령어들을 이곳에 정리한다.
3. Git 유용한 명령어 모음
3.1 working with submodules
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
32
33
34
# add a submodule to a repository and clone it
# 저장소에 서브모듈을 추가하고 clone
git submodule add https://domain.com/user/repository.git submodules/repository
# add "ignore = all" to .gitmodules in order to ignore all changes in submodules
# 서브모듈의 모든 변경을 무시하기 위해 .gitmodules에 ignore 설정 추가
cat <<eof >> .gitmodules
[submodule "mysubmodule"]
path = myrepo
url = git@gitlab.com:jdsalaro/myrepo.git
ignore = all
eof
# while in a repository which contains submodules, they can be recursively
# updated by issuing the following command
# 서브모듈을 포함하는 레포지토리에 있는 동안 아래 명령어를 통해 재귀적으로 업데이트 할 수 있음
git submodule init
git submodule update
# this an faster way of updating all submodules
# 모든 서브모듈을 업데이트하는 빠른 방법
git submodule update --init --recursive
# clone a repository which contains references to other repositories as submodules
# 다른 레포지토리를 참고하는 레포지토리를 서브모듈로 clone
git clone --recursive
# remove completely a submodule
# 서브모듈을 완전히 제거하는 방법
submodule='mysubmodule';\
git submodule deinit $submodule;\
rm -rf .git/modules/$submodule;\
git config --remove-section $submodule;\
git rm --cached $submodule
3.2 searching
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#list the latest tagged revision which includes a given commit
# commitid가 포함된 최신 태그 리비전 리스트
git name-rev --name-only {commitid}
# find the branch containing a given commit
# commitid가 포함된 브랜치를 찾음
git branch --contains {commitid}
# show commits which have been cherry-picked and applied to main already
# cherry-picked 되고 이미 main에 적용된 커밋을 표시
git cherry -v main
# look for a regular expression in the log of the repository
# 저장소의 로그에서 특정내용을 검색하는 데 사용
git show :/{regex}
3.3 diffing
1
2
3
4
5
6
7
8
9
10
11
12
# diff two branches
# 두 브랜치의 다른점
git diff branch1..branch2
# perform a word-diff instead of a line-diff
# line diff 대신 word diff를 수행
git diff --word-diff
git diff --name-status main..branchname
git diff --stat --color main..branchname
git diff > changes.patch
git apply -v changes.patch
3.4 git config
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# show current values for all global configuration parameters
# 모든 전역 설정 파라미터의 현재 값 표시
git config --list --global
# let git automatically correct typos such as "comit" and "pussh."
# "comit", "pushh" 와 같은 오타를 자동으로 수정
git config --global help.autocorrect 1
# set a username globally
# 사용자 이름을 전역적으로 설정
git config --global user.name {username}
# set an email address globally
# email 주소를 전역적으로 설정
git config --global user.email {email@provider.com}
# always --prune for git fetch and git pull
# git fetch, git pull할 때 --prune한 효과를 전역적으로 설정
git config --global fetch.prune true
# remove the previously set username globally
# 이전에 설정한 사용자 이름을 전역적으로 삭제
git config --global --unset user.name
# color the git console
# git 콘솔의 색 설정
git config color.ui true
# set the tool used by git for diffing globally
# git diff 툴을 설정할 때 사용할 툴을 mytool 자리에 변경하여 입력
git config --global diff.tool {mytool}
# set the tool used by git for merging globally
# git merge 툴을 설정할 때 사용할 툴을 mytool 자리에 변경하여 입력
git config --global merge.tool {mytool}
# remove the previously set configuration value globally
# 이전에 설정한 값을 전역적으로 제거
git config --global --unset {myparameter}
# allows populating the working directory sparsely, that is,
# cloning only certain directories from a repository
# 저장소에서 특정 디렉토리만 복제해야할 경우 설정
git config core.sparseCheckout true
# instruct git to retrieve only some directory in addition to
# those listed in `.git/info/sparse-checkout
# 특정 디렉토리만 가져오고 싶은경우 .git/info/sparse-checkout 에 리스트를 추가
echo {some/directory/inside/the/repository} >> .git/info/sparse-checkout
# define which whitespace problems git should recognize (any whitespace
# at the end of a line, mixed spaces or tabs)
# 줄 끝의 공백, space와 tab이 혼합된 공백 등 git 이 알아야하는 공백 문제들을 정의
git config --global core.whitespace trailing-space,space-before-tab
# tells Git to detect renames. If set to any boolean value, it will
# enable basic rename detection. If set to "copies" or "copy", it will
# detect copies, as well.
# 이름 변경을 탐지할 수 있도록 설정, 임의의 boolean값으로 설정하면 기본 탐지가 가능하고 "copies"나 "copy"로 설정하면 복사본도 탐지
git config --global diff.renames copies
# if set, git diff uses a prefix pair that is different from the standard "a/"
# and "/b" depending on what is being compared.
# git diff 시 a/와 /b를 사용하여 소스와 대상을 나타내는데 아래 옵션은 다른 접두사를 사용할 수 있도록 설정할 수 있음
git config --global diff.mnemonicprefix true
# always show a diffstat at the end of a merge
# merge 끝에 diffstat를 항상 표시
git config --global merge.stat true
# no CRLF to LF output conversion will be performed
# CRLF에서 LF로 출력 변환이 수행되지 않게 설정
git config --global core.autocrlf input
# whenever pushing, also push local tags
# 푸시할 때마다 local tag도 푸시
git config --global push.followTags true
# show also individual files in untracked directories in status queries
# 상태 쿼리에 추적되지 않은 디렉토리에 있는 개별파일도 표시
git config --global status.showUntrackedFiles all
# always decorate the output of git log
# git log의 출력을 항상 decorate
git config --global log.decorate full
# the git stash show command without an option will show the stash in patch form
# 옵션이 없는 git stash show 는 patch 형태로 stash를 표시
git config --global stash.showPatch true
# always set the upstream branch of the current branch as the branch to be
# pushed to when no refspec is given
# refspec이 제공되지 않을 때 항상 현재 분기의 업스트림 분기를 푸시할 분기로 설정
git config --global push.default tracking
# ignore the executable bit of files in the working tree
# 작업 트리의 실행가능한 bit 파일 무시
git config core.fileMode false
3.5 ls-files and ls-tree
1
2
3
4
5
6
7
8
9
10
# list files contained in the current HEAD or in the head of the
# main branch respectively
# 현재 HEAD 또는 메인 브랜치의 head에 포함된 파일을 각각 보여줌
git ls-tree --full-tree -r HEAD
git ls-tree -r main --name-only
git ls-tree -r HEAD --name-only
# list ignored files
# ignore 된 파일의 리스트를 보여줌
git ls-files -i
3.6 cleaning
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# perform a dry run and list untracked files or directories that
# would be removed without actually doing so
# 제거될 디렉토리나 추적되지 않은 파일의 리스트를 보여주고 dry run
git clean -n
#remove untracked files from the working tree
# 작업 트리에서 추적되지 않은 파일 제거
git clean -f
# removes untracked files and directories
# 추적되지 않은 파일 및 디렉토리 제거
git clean -f -d
# same as above but also removes ignored files
# 위의 기능을 포함하여 무시된 파일도 제거
git clean -f -x -d
# same as above but does so through the entire repo
# 위와 동일하지만 전체 레포지토리를 대상으로 함
git clean -fxd :/
3.7 git log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git whatchanged myfile
git log --after="MMM DD YYYY"
git log --pretty=oneline
git log --graph --oneline --decorate --all
git log --name-status
git log --pretty=oneline --max-count=2
git log --pretty=oneline --since='5 minutes ago'
git log --pretty=oneline --until='5 minutes ago'
git log --pretty=oneline --author=<name>
git log --pretty=oneline --all
git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
git log --grep regexp1 --and --grep regexp2
git log --grep regexp1 --grep regexp2
git grep -e regexp1 --or -e regexp2
3.8 leave the current commit as the only commit in the repository
1
2
3
4
5
git checkout --orphan new
git add -A
git commit -am "Initial commit"
git branch -D main
git branch -m main
참고자료: