Home GIT - 기초 1, 기본 흐름과 명령어
Post
Cancel

GIT - 기초 1, 기본 흐름과 명령어

1
2
매일(?) 사용하는 GIT 을 더 잘 사용하기 위해 많이 기초부터 정리하도록 한다.
잘 정리되어있는 외국 블로그를 발견하게 되어 이를 참고하여 번역해서 정리하도록 한다.

1. Git 흐름

일단, git과 github가 무엇인지 아는 상태에서 큰 흐름은 다음과 같다.

image 출처: https://patrickzahnd.ch

각 명령어는 아래 ‘Git 명령어 모음’의 ‘2.1 git workflow’에서 사용법 및 간단한 설명이 나와있다.


2. Git 기본 명령어 모음 - git workflow을 중심으로

2.1 initialize and clone

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# initialize a git repository in the current working directory
# 현재 작업 디렉토리에서 git 저장소 초기화
git init

# clone a remote repository over https
# https를 통해 원격 레포지토리 복제
git clone https://{remote.com/repo.git}

# clone a remote repository over ssh
# ssh를 통해 원격 레포지토리 복제
git clone ssh://{git@remote.com:/repo.git}

# recursively clone a repository over https
# https를 통해 재귀적으로 레포지토리 복제
git clone --recursive https://{remote.com/repo.git}

# recursively clone a repository over ssh
# ssh를 통해 재귀적으로 레포지토리 복제
git clone --recursive ssh://{git@remote.com:/repo.git}

2.2 track, add and commit

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
# start tracking a file or add its current state to the index
# 파일을 추적하거나 현재 상태를 인덱스에 추가
########################################
# 인덱스는 바로 다음에 커밋할 것들을 일컬음
########################################
git add {file}

# add everything which is tracked and has been changed to the index
# 추적된 것이나 인덱스에 변경된 모든것을 추가
git add -u

# add everything which is untracked or has been changed to the index
# 추적되지 않거나 인덱스에 변경된 모든 것을 추가
git add .

# commit to local history with a given message
# 메세지를 local history에 커밋
git commit -m {"message"}

# add all changes to already tracked files and commit with a given 
# message, non-tracked files are excluded
# 이미 추적된 파일의 모든 변경사항을 추가하고 메세지와 함께 커밋
# 추적되지 않는 파일은 제외
git commit -am {"message"}

# modify the last commit including both new modifications and given message
# 새로운 수정들과 메세지로 마지막 커밋을 수정
git commit --amend -m {"message"}

# perform a commit with an empty message
# 빈 메세지로 커밋
git commit --allow-empty-message -m

2.3 status and diagnostics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# show status of the working directory 
# 작업 디렉토리의 상태 표시
git status			

# show short version status 
# short 버전 상태 표시
git status -s			

# show commit at the head of current branch 
# 현재 브랜치 head의 커밋을 표시
###########################
# ** HEAD는 현재 브랜치를 가리키는 포인터
###########################
git show HEAD			

# show commit with object ID mycommit 
# mycommit은 커밋 해시값, 해당 커밋 해시값에 해당하는 정보 표시
git show {mycommit}		

# show version of folder/file at HEAD
# 폴더나 파일의 HEAD 버전을 보여줌
git show HEAD:{folder/file}

2.4 checking out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# replace file-name with the latest version from the current branch
# file 이름을 현재 브랜치의 최신 버전으로 변경
git checkout -- filename

# in case fileorbranch is a file, replace fileorbranch with the latest version
# of the file on the current branch. In case fileorbranch is a branch, replace
# the working tree with the head of said branch.
# fileorbranch 가 파일인 경우 현재 브랜치의 파일의 최신 버전으로 파일을 교체하고
# fileorbranch 가 브랜치인 경우 작업 트리를 브랜치의 head로 교체
git checkout {fileorbranch}

# replace the current working tree with commit 05c5fa
# 05c5fa 의 커밋 상태로 현재 작업 트리 교체
git checkout {05c5fa}

# replace the current working tree with the head of the main branch
# 메인 브랜치의 head로 현재 작업 트리를 교체
git checkout {main}

2.5 remotes

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
# show the remote branches and their associated urls
# 원격 브랜치의 url을 표시
git remote -v 

# adds an https url as remote branch under the name origin
# origin 에 https url을 추가
git remote add -f origin https://{remote.com/repo.git}

# adds an ssh url as remote branch under the name origin
# origin 에 ssh url을 추가
git remote add -f origin ssh://{git@remote.com:/repo.git}

# remove the remote with ID origin
# origin을 원격에서 제거
git remote remove -f origin

# set an https url for the remote with ID origin
# origin 이라는 ID의 원격에 https url 설정
git remote set-url origin https://{remote.com/repo.git}

# set an ssh url for the remote with ID origin
# origin 이라는 ID의 원격에 ssh url 설정
git remote set-url origin ssh://{git@remote.com:/repo.git}

# clean up remote non-existent branches
# 존재하지 않는 원격 브랜치 정리
git remote prune origin 

# set the upstream branch, to which changes will be pushed, to origin/main
# 변경 사항을 push 할 upstream 브랜치를 세팅
##################################################
# ** upstream branch 란? : 트래킹 하는 대상 브랜치
# 쉽게 비유하자면 origin이 내가 개발한 내용이 들어있는 브랜치라면 upstream 브랜치는 내 브랜치를 반영해야할 master 브랜치 같은 느낌
# 리모트 트래킹 브랜치를 로컬 브랜치로 checkout 하면 자동으로 트래킹 브랜치 생성
##################################################
git branch --set-upstream-to=origin/main

# set foo as the tracking branch for origin/bar
# origin/{bar}의 트래킹 브랜치로 foo를 설정
git branch –track {foo} origin/{bar}

# update local tracking branches with changes from their respective remote ones
# 원격에서 변경한 내용으로 로컬 트래킹 브랜치를 업데이트
git fetch

# update local tracking branches and remove local references to
# non-existent remote branches
# 로컬 트래킹 브랜치 업데이트 및 존재하지 않는 원격 브랜치에 대한 로컬 참조 제거
git fetch -p

# delete remote tracking branch origin/branch
# 원격 트래킹 브랜치 origin/{branch} 삭제
git branch -r -d origin/{branch}

# update local tracking branches and merge changes with local working directory
# 로컬 트래킹 브랜치 업데이트 및 로컬 작업 디렉토리 변경 내용 병합
git pull

# given one or more existing commits, apply the change each one introduces,
# recording a new commit for each. This requires your working tree to be clean 
# 하나 또는 그 이상의 커밋이 있을 때, 각각의 변경을 적용하고,
# 각 새로운 커밋에 대한 기록한다. 작업트리가 깨끗해야 동작
git cherry-pick commitid

# push HEAD to the upstream url
# HEAD를 upstream url로 표시
git push

# push HEAD to the remote named origin
# HEAD를 원격 origin에 push
git push origin 

# push HEAD to the branch main on the remote origin 
# HEAD를 원격 origin 에서 브랜치 메인으로 push
git push origin main

# push and set origin main as upstream
# origin main을 upstream으로 push 및 설정
git push -u origin main

# delete previous commits and push your current one
# WARNING: never use force in repositories from which other have pulled [1]
# https://stackoverflow.com/a/16702355
# 이전 커밋을 삭제하고 현재의 것을 push
# 다른 사용자가 [1]을 ppull 하는 레포지토리에서는 사용하지 말 것
git push --force all origin/main

# a safer option to force-push that will not overwrite work on the remote
# branch if more commits were added ensuring someone else's work is not overwritten
# 강제 push 중 좀 더 안전한 이 옵션은 원격브랜치에서 덮어쓰지 않도록 하는데
# 다른 사람의 작업을 덮어쓰지 않도록 하는 커밋
git push --force-with-lease

# turn the head of a branch into a commit in the currently checked out branch and merge it
# 브랜치의 head를 현재 체크아웃한 브랜치의 커밋으로 변경하고 병합
git merge --squash {mybranch}

2.6 revert and reset

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
# figures out the changes introduced by commitid and introduces a new commit undoing them. 
# commitid의 변경사항을 파악하고 그것을 취소하는 새로운 커밋을 만든다.
git revert {commitid}

# does the same but doesn't automatically commit
# 위와 동일한 작업을 수행하지만 자동으로 커밋하지 않음
git revert -n {commitid}

# updates the index and the HEAD to match the state of commit id. 
# changes made after this commit are moved to “not yet staged for commit” 
# commitid와 일치하도록 인덱스와 HEAD를 업데이트
# 아직 스테이징되지 않은 커밋으로 이동시킬 수 있음
git reset {commitid}

# sets only the HEAD to commitid
# HEAD를 지정한 commitid로 설정, 주로 branch 이동 시 사용
git reset --soft {commitid}

# sets the HEAD, index and working directory to commitid
# HEAD, 인덱스, 작업 디렉토리를 commitid로 설정, 주로 commit을 되돌릴 때 사용
git reset --hard {commitid}

# sets the HEAD, index and working directory to origin/main 
# HEAD, 인덱스, 작업 디렉토리를 origin/main으로 설정
git reset --hard {origin/main}

2.7 the stash

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
# take all changes made to working tree and stash them in a new dangling commit, putting 
# the working tree in a clean state
# DISCLAIMER: this does not include untracked files
# 작업 트리에 모든 변경을 가져와 새로운 커밋에 숨겨 작업 트리를 깨끗하게 만든다.
# 추척되지 않은 파일은 포함되지 않음
git stash

# stash everything into a dangling commit, including untracked files
# 추적되지 않은 파일을 포함하여 커밋에 숨긴다.
git stash save --include-untracked

# push individual or multiple files to the stash 
# 개별 또는 여러 파일을 stash로 push
git stash push file 

# apply the changes which were last stashed to the current working tree
# 마지막으로 stash한 변경을 현재 작업트리에 적용, 숨겨둔걸 꺼내는 용도
git pop

# show the stash of commits
# 커밋의 stash 리스트를 표시
git stash list

# apply a particular commit in the stash
# stash에 있는 특정 커밋을 적용
git stash apply

# apply the second-to-last commit in the stash
# stash에 있는 두번째 부터 마지막 커밋 까지 적용
git stash apply stash@{2}

# drop the second-to-last commit in the stash
# stash의 두번째부터 마지막 커밋을 버림
git stash drop stash@{2}

# stash only the changes made to the working directory but keep the index unmodified
# 이미 스테이징 영역에 들어있는 파일은 stash 하지 않음, 인덱스는 수정하지 않기 때문
# 현재 작업 디렉토리의 변경 사항만 stash
git stash --keep-index

# clear the stash
# stash 를 삭제
git stash clear


참고자료:

Git Cheatsheet: Commands, Tips and Tricks

This post is licensed under CC BY 4.0 by the author.