git

git 명령어 정리

danuri 2021. 1. 2. 22:07

git 동작 원리

 

github 등록

- 파일 수정할 때마다 다음 과정 반복(git checkout -- [file] : 수정사항 원상복귀)

-> git add [file] or git add . (모든 파일) <-> git reset [file]

-> git commit -m "[message]"

-> git push

 

커밋 내역 수정

- git pull : remote repository -> local repository(git fetch + git merge)

- git log : git 기록

- git reset --hard [hash값] : hash값에 대한 commit 이후의 commit들 local repository에서 제거

-> git push -f 로 강제 push해주면 remote repository에도 반영

+)git commit --amend : 메시지 수정

 

branch

- git branch : 현재 branch 확인

- git branch [branch] : branch 생성

- git checkout [branch] : branch 변경

- git merge [branch] : master branch에서만 가능, branch 병합 -> git push 하면 remote repository에 반영

+) merge할 때 두 branch 내용(커밋 상태)이 다르면 충돌

-> 해당 파일 열어보면 차이점이 명시되어 있음 -> 수정 후 merge 진행

- git branch -d [branch] : branch 제거

 

remote repository 관리

- git remote : 현재 원격저장소

- git remote show [remote] : 해당 원격저장소 표시

- git remote add [name] [주소] : 원격저장소 추가

- git remote rename [from] [to] : 원격저장소 이름 변경

- git log origin/master 처럼 특정 원격저장소에 대한 명령 수행 가능

- git remote rm [name] : 원격저장소 제거

+) 일반적으로 프로젝트 하나 당 한 개의 원격저장소 사용, 이런 방법도 있다는 것을 숙지.

 

log 다루기

- git log --stat : 각 log에 대한 통계정보(라인 추가 개수 등)

- git log -p -3 : 각 log의 세부 정보들(위에서 3개 까지)

- git log --pretty=oneline : log 한줄에 출력

- git log --pretty=format:"%h -> %an, %ar : %s" --graph : 이처럼 따로 포맷 설정 가능(graph는 branch 기록 출력)

+) 물론 github에도 log 기록이 이쁘게 잘 나와있음

 

README

- 일반적으로 github에서 직접 작성해서 git pull로 local repository에 저장하는 방법이 편리하다.

 

<기본>

줄 바꿈시 해당 라인 뒤에 space 두번

문단 구분은 Enter 두번

 

<제목>

## 깃 튜토리얼 : 일반적으로 ## 두개가 보기 좋은 크기(#은 다섯 개 까지 가능)

preview

 

<코드>

소스코드 블록은 다음과 같이 작성할 수 있습니다.

```c
#include <stdio.h>

int main(void){
    printf("Hello World!");
    return 0;
}
```

preview

 

<링크>

링크는 다음과 같이 작성할 수 있습니다.

 

[블로그 주소](https://gksdudrb922.tistory.com/)

 

<순서 없는 목록>

순서 없는 목록은 다음과 같이 작성할 수 있습니다.

* 깃 튜토리얼
  * 깃 Clone
  * 깃 Pull
  * 깃 Commit
    * 깃 Commit 1)
    * 깃 Commit 2)

preview

 

<인용구문>

인용 구문은 다음과 같이 작성할 수 있습니다.

> '공부합시다.' - 한영규 -

preview

 

<테이블>

테이블은 다음과 같이 작성할 수 있습니다.

이름|영어|정보|수학
---|---|---|---|
한영규|98점|87점|100점|
홍길동|76점|57점|99점|
이순신|100점|75점|99점|

preview

 

<강조구문>

강조는 다음과 같이 할 수 있습니다.

**치킨** 먹다가 ~~두드리기~~ 났어요. ㅠㅠ

preview

git archive

- 소스 코드만을 압축하고 싶을 때

->git archive --format=zip -o Master.zip

 

git rebase

- 특정 커밋 수정/삭제

- git rebase -i HEAD~3 : HEAD에서 3개의 commit 내역 확인

다양한 명령어 사용 가능

- 커밋 메시지 변경시 해당 커밋의 명령어 reword로 변경

- 커밋 삭제시 명령어 drop으로 변경

- 커밋 날짜 변경 시 명령어 edit으로 변경

-> GIT_COMMITER_DATE="Oct 1 10:00:00 2020 +0000" git commit --amend --no-edit --date "Oct 1 10:00:00 2020 +0000"

-> git rebase --continue

 

- git rebase -i [hash 값] : 특정 커밋에 대해 그 위쪽으로 전부 check

 

git config

- git 환경설정

- git config --list : git 설정 상태 출력

- git config --global [설정 변경] (user.name "test" 등) : global 설정 변경 -> .gitconfig 파일에 저장

- git Project 하나에 대해서도 별도의 환경설정 가능 (.git/config 파일에 저장)

 

git commit 날짜 및 커미터 변경

<날짜변경>

- 첫번째 방법은 rebase 사용 (git rebase 부분 참조)

- 두번째 방법은 filter 옵션 사용

->  

git filter-branch -f --env-filter \
>   'if [ $GIT_COMMIT = d1bfd49044e1f2af7df2a9c389e73df52761d524 ]
>     then
>       export GIT_AUTHOR_DATE="Thu Oct 1 10:00:00 2020 +0000"
>       export GIT_COMMITER_DATE="Thu Oct 1 10:00:00 2020 +0000"
>    fi'

 

<커미터 변경>

git filter-branch -f --env-filter '

OLD_EMAIL="gksdudrb922@naver.com"
CORRECT_NAME="test"
CORRECT_EMAIL="test@test.com"

if [ $GIT_COMMITER_EMAIL = $OLD_EMAIL ]
then
   export GIT_COMMITER_NAME="$CORRECT_NAME"
   export GIT_COMMITER_EMAIL="$CORRECT_EMAIL"
fi
if [ $GIT_AUTHOR_EMAIL = $OLD_EMAIL ]
then
   export GIT_AUTHOR_NAME="$CORRECT_NAME"
   export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi'