본문 바로가기
Language & Framework & GIT/GIT

[GIT] Git & Git 활용법

by veganwithbacon 2023. 11. 9.
반응형

  ▶Git & Git 활용법


  Git Program 설치

Git 다운받아 설치한다.

  Git 명령어

  • Help : git help config
  • git 설정하기
    • 사용자설정 : git config —global user.name “babo”
    • 메일설정 : git config —global user.mail “babo@babo.com”
    • config확인
      • git config —list
      • git config user.name
      • 최초 설정 확인 : git config —show-origin user.name
    • End of Line
      • 시스템설정대로 : git config —global core.eof native
      • 시스템설정대로 : git config —global core.autcrlf false
    • Diff 결과 Highlighting 하기 : git config -global pager.diff ‘diff-highlight | less’
  • 로컬저장소만들기
    • mkdir project
    • cd project
    • git init
    • git add *.c
    • git commit -m “My first version managing directory”
  • 저장소 내려받기
    • git clone id@원격저장소_URL
    • git clone /로컬저장소/저장소
  • File 상태 확인(Tracked : Modified/Unmodified/Staged, Untracked : Unstaged)
    • git status
    • git status -s :
      : OO filename
      : First O - Staging 상태를 보여줌
      : Second O - Working directory 상태를 보여줌
      : M - Modifyed
      : A - Added
      : ? - Unstaged
    • Staging Area file을 Unstage로 변경하기
      • git reset HEAD filename
      • git checkout — filename
  • Tag : 특정 commit에 대해 Tag를 지정 (Version 관리로 주로 사용)
    • 조회
      • git tag
      • git tag -l v1.4
      • git show v1.4
    • 만들기
      • LghtWeight : git tag v1.5
      • Annoated : git tag -a v1.5 -m “Version Updated by MC”
    • 이전 commit에 tag 달기 : git tag -a 1.5 commithash
    • 지우기 : git tag -d v1.5
    • Tag 공유하기 : git push origin v1.5
  • Branch
    • 조회하기
      • git branch -v
      • git branch —merged
      • git branch —no-merged
    • 만들기 : git branch issues
    • 변경하기 : git checkout issue
    • 만들고 변경하기 : git checkout -b issue
    • 확인하기 : git branch
    • 삭제하기 :
      • Merged branch : git branch -d issue
      • No-merged brnach : git branch -D issue
  • 파일만들기 : touch test.txt
  • 파일추적하기 : git add test.txt
  • 파일추적제외하기 : .gitignore 파일에 해당 파일명이나 filter를 적는다.
    : *.[oa] - 확장자가 .o 이거나 .s는 무시한다.
  • Commit
    • 파일변경완료 : git commit -m “add test.txt”
    • Unstaged file를 바로 commit하기 : git commit -a -m “Version update”
    • diff결과를 commit message로 사용하기 git commit -v
    • 이전 commit 지우고 새로 등록하기
      1. git commit -m ‘initial commit’
      2. git add forgotten_file
      3. git commit —amend
    • Commit hash 확인하기 : git log —pretty=oneline
  • 조회하기
    • git log
    • git log —pretty=format:”%h %s” —graph
  • 원격저장소
    • 원격저장소 확인하기
      • git remote -v
      • git remote show origin
    • 원격저장소 내려받기 : git clone 원격저장소_URL
    • 원격저장소 추가하기 : git add remote origin 저장소이름 원격저장소_URL
  • Fetch
    • git fetch origin
  • Merge
    1. git checkout master
    2. git diff orign master
    3. git merge issue
    4. git branch -d issue
  • 원복하기
    • 파일 원복 : git checkout — filename
    • Commit 원복
      1. git fetch origin
      2. git reset —hard origin/master
  • Pull(Fetch + Merge) : git pull
  • Push : git push origin issue
    • master로 merge 하고 Pull request 보내기 : 비추천 방식
      • master branch로 이동 : git checkout master
      • Merge : git merge issue
      • Push : git push origin/master master
    • issue로 pull request 보내고 master로 Pull 하기 : 추천 방식
      • push : git push origin issue
      • git request-pull origin issue
      • git checkout master
      • git fetch
      • git merge
  • Pull Request 보내기 : git request-pull origin/master issue

  Push 이전에 꼭 확인하기

  1. diff
  2. diff —check : 공백으로 인한 변경사항 방지를 위해
  3. commit
  4. fetch
  5. merge
  6. push
더보기

  Branch : 하나의 커밋과 그 부모 커밋들을 포함하는 작업 내역


✔ git branch 브랜치명
 git checkout 브랜치명 : 현위치가 메인으로 이동하며, main branch 로 돌아간다
 git merge 브랜치명 : 체크아웃한 branch들과 합쳐짐
 git rebase 브랜치명 : ex) 분선을 타다가 main으로 합칠 때 주로 사용,
                                          만약 최신화가 된 것이 rebase된 branch 라면 git rebase main을 해주면 해결됨
                                
 git checkout 헤드명 : 최신화된 커밋에 checkout을 하여 head로 지정이 가능하다.
                                    해시를 지우는 것만으로 데이터를 없앨 수 있기에, 간편하다.
 git checkout 브랜치명^ : ^는 상대참조를 해주기에, 해당 branch 명의 부모 커밋으로 올라간다. 

                                              ^를 통해 head를 부모 커밋으로 옮겨서 확인할 수 있다.

 

  ^연산자 & ~ 연산자

^ 를 이용하면 거슬러 올라가려는 만큼 연산자를 작성해줘야 하지만, 

~(틸드) 연산자를 사용하면 ~뒤에 숫자만 써주면 ~N를 통해 뒤로 N번 옮길 수 있다.

 

EX) N번 올라간다 했을떄 

       1) git branch main^^^^^^^^^^^^^^^^^^^      2) git branch main~N

>> git branch -f main HEAD~3

         :  위와 같이 사용하면 main을 HEAD(최신커밋/내가 checkout되어있는 곳)기준 뒤로 3칸 옮기는게 된다. 

 

  -f 커맨드를 통해 브랜치를 특정 커밋에 직접 재지정 가능하다

설명이 미흡해서 이것저것 다 시도해보니까 알게 된 것
git branch -f [브랜치명(a1)] [목표 커밋(a2)] 을 지정하면 a1이 a2위치로 이동된다. -f 커맨드가 입력되어야만 사용 가능


  git을 되돌리는 방법

: reset  - 브랜치가 예전의 커밋을 가리키도록 이동시키는 방식(말 그대로 reset을 시킨다)
: revert - 로컬 branch 로 사용할 경우, reset을 사용해도 되지만 공동작업을 하는 경우에는 git revert를 많이 쓴다.

git reset HEAD~1 ?

 

   git cherry-pick <commit1> <commit2> <~~~~~~~~

: 현 위치의 아래에 있는 일련의 커밋들에 대한 복사본을 만들겠다는 것


  interactive rebase 

: rebase 명령을 사용 시, -i 옵션을 같이 사용
  i옵션을 사용하면 git은 rebase의 목적지가 되는 곳 아래에 복사될 커밋들을 보여주는 

   텍스트 편집기(EX: VIM)에서 파일을 띄운다.
  각 커밋이 구분 가능하도록 각각의 해시와 메시지들을 보여준다.

=> 적용 커밋들의 순서를 텍스트 편집기에서 바꾸기 가능 
=> 원치않는 커밋들 제거 
=> 커밋을 SQUASH 가능(커밋합치기)


 

출처 : https://git-scm.com/book/ko/v2

 

Git - Book

 

git-scm.com

https://midnightcow.tistory.com/entry/Git-Command-%EC%82%AC%EC%9A%A9%EB%B2%95-%EC%A0%95%EB%A6%AC

반응형

'Language & Framework & GIT > GIT' 카테고리의 다른 글

[GIT] GIT 많이 쓰는 명령어 모음  (118) 2023.11.02
[GIT] GIT COMMIT MSG 작성법  (70) 2023.11.02

댓글