Git 명령어 정리

eunsol Jo·2021년 8월 24일
post-thumbnail

IntelliJ와같은 IDE나 fork와 같은 GUI에서 git을 다루기만 하다보니 명령어 익숙해지고자 cli로 git을 다루는 연습을 하며 명령어를 정리해 보았다. :)

1. 계정 초기화

$ git config --system --unset credential.helper

- 동작시 계정 셋팅 다시  ex. git fetch

2. Reset

$ git reset --option <돌아가길 원하는 커밋>       → 해당 커밋까지 살아있음.

$ git reset HEAD~n                             → n개 이전의 커밋으로 되돌리기

hard - 이전 변경사항 제거
soft - 이전 변경사항이 스테이지에 존재
mixed - 이전 변경사항이 인덱스에만 존재
 * 원격에 push후 reset사용시, force push만 가능

3. Revert

$ git revert <되돌릴커밋 or 구간>		→ 해당 커밋이 없어짐. 하나 이전꺼 까지 살아있음.

- 구간 : <되돌릴커밋>...<현재커밋-1>
- hard옵션을 뺀 reset과 유사 but 이력이 남는다.

4. Rebase

(1) 로그확인후
$ git log 

(2)  HEAD(포함)부터 n개 까지 squash
$ git rebase -i @~n

(3) 시간순으로 커밋 나열됨. 맨 () 나중꺼 빼고 pick → squash로 변경. 저장하고 나옴(:x)

(4) 커밋메세지 수정

(5) 포스푸시
$ git push origin feature/branch -f

5. stash

$ git stash : 스테시에 변경분 저장

$ git stash list : 스테시 목록 확인

$ git stash apply {stash명}

5.1 삭제된 stash 되살리기 (git bash)

1. 삭제된 stash 목록 확인
$ git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk

2. stash되살리기
$ git update-ref refs/stash <커밋아이디> -m "메세지"

3. stash목록에 보이게 (위에서 되살리면 커밋아이디 바뀜. 재조회 필요)
$ git update-ref refs/stash <커밋아이디> --create-reflog -m "메세지"

6. branch

$ git checkout -t origin/브랜치명 : 원격 브랜치 가져오기

$ git branch -d 브랜치명 : 로컬 브랜치 삭제

$ git branch : 현재브랜치명

8. 이슈

git 한글 깨질때

윈도우 + 인텔리제이 환경에서 터미널 사용시 종종 한글이 깨지는 경우가 있다.

$ set LC_ALL=ko_KR.UTF-8

git lock fail (eclipse)

대소문자를 무시한 동명의 브랜치가 있을경우, 이클립스에서 fetch시 발생
remote의 유효하지 않은 참조를 제거하는 아래 명령어를 통해 해결

$ git remote prune origin
$ git remote update --prune

.gitignore 적용

이미 커밋된 파일들에 대한 .gitignore적용

$ git rm -r --cached .
$ git add .
$ git commit -m "Apply .gitignore"
$ git push

⚙️ .gitignore

### Java template
*.class
 
# Package Files #
*.jar
*.war
*.ear
 
### macOS template
*.DS_Store
.AppleDouble
.LSOverride
 
# IntelliJ project files
.idea
.idea/*.xml
*.iml
out
gen
build
rebel.xml
 
# Compliled files
/target/
**/target
 
/example/
 
# Gradle
.gradle
/build/
.gradletasknamecache

출처
https://cjh5414.github.io/gitignore-update/

profile
Later never comes 👩🏻‍💻

0개의 댓글