Git 기초

2019-07-25
  • git
  • init

    현재 위치를 저장소로 만든다.

    $ git init
    Initialized empty Git repository in C:/noneedinstall/PortableGit/localRepo/temp/.git/
    
    $ ls -al
    total 8
    drwxr-xr-x 1 lg 197609 0 7월   9 21:19 ./
    drwxr-xr-x 1 lg 197609 0 7월   9 21:19 ../
    drwxr-xr-x 1 lg 197609 0 7월   9 21:19 .git/
    

    config

    설정명령어이다.

    $ git config new.configkey "newconfigvalue"
    $ git config --list
    ...
    new.configkey=newconfigvalue
    

    --list

    설정목록을 보여준다.

    $ git config --list
    core.symlinks=false
    core.autocrlf=true
    color.diff=auto
    color.status=auto
    color.branch=auto
    color.interactive=true
    help.format=html
    http.sslcainfo=/ssl/certs/ca-bundle.crt
    diff.astextplain.textconv=astextplain
    rebase.autosquash=true
    filter.lfs.clean=git-lfs clean -- %f
    filter.lfs.smudge=git-lfs smudge -- %f
    filter.lfs.process=git-lfs filter-process
    filter.lfs.required=true
    credential.helper=manager
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    

    --global

    전역 설정 옵션이다. 유저명과 이메일 등을 설정한다.

    $ git config --global user.name "epu_baal_laptop"
    $ git config --global user.email "herdin86@gmail.com"
    $ git config --list
    core.symlinks=false
    core.autocrlf=true
    color.diff=auto
    color.status=auto
    color.branch=auto
    color.interactive=true
    help.format=html
    http.sslcainfo=/ssl/certs/ca-bundle.crt
    diff.astextplain.textconv=astextplain
    rebase.autosquash=true
    filter.lfs.clean=git-lfs clean -- %f
    filter.lfs.smudge=git-lfs smudge -- %f
    filter.lfs.process=git-lfs filter-process
    filter.lfs.required=true
    credential.helper=manager
    user.name=epu_baal_laptop
    user.email=herdin86@gmail.com
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    

    global 과 list 를 같이 사용하면 global config 만 보여준다.

    $ git config --global --list
    user.name=epu_baal_laptop
    user.email=herdin86@gmail.com
    

    alias.{YOUR-ALIAS}

    alias 를 설정한다.

    $ git config --global alias.logog 'log --oneline --graph'
    $ git logog
    * 0d24996 (HEAD -> master, origin/master, origin/HEAD) ??
    * 63db424 add highlighter css
    * 3530326 init
    *   2058028 Merge branch 'master' of https://github.com/herdin/herdin.github.com
    |\
    | * 434e55e Update README.md
    | * 531665e Set theme jekyll-theme-minimal
    * | 9f22b7e mod config
    |/
    * decfecb first post
    * 34383ef add first post
    
    $ git config --global alias.logg "log --graph --abbrev-commit --decorate --format=format:'%C(yellow)%h%C(reset) - %C(green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(yellow)%d%C(reset)' --all"
    * 0d24996 - (43 minutes ago) ?? - epu_baal_laptop (HEAD -> master, origin/master, origin/HEAD)
    * 63db424 - (45 minutes ago) add highlighter css - epu_baal_laptop
    

    내가 자주쓰는 alias

    $ git config --global alias.logg "log --graph --abbrev-commit --decorate --format=format:'%C(yellow)%h%C(reset) - %C(green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(yellow)%d%C(reset)' --all"
    
    $ git config --global alias.ss 'status'
    $ git config --global alias.logog 'log --oneline --graph'
    $ git config --global alias.aa 'add .'
    $ git config --global alias.cm 'commit -m'
    $ git config --global alias.pom 'push origin master'
    

    --unset

    cofing 설정을 해제한다.

    $ git config --unset new.configkey #삭제
    

    clone

    remote tracking branch 를 하나 만들고 remote tracking branchtracking branch 를 만든다.

    remote - 원격 저장소 (ex: https://github.com/herdin/SimpleJava.git) remote tracking branch - remote(원격저장소)를 추적(tracking)하는 branch. 자동으로 생성되며 사용자가 변경할 수 없고 fetch/pull 등으로 remote 의 수정사항에 따라 변경된다. clone 을 하게 되면 remote 를 추적하는 remote tracking branch 를 하나 만들고 mater branch 를 동일하게 생성 한뒤, HEAD 를 가리키게 한다.

    #SimpleJava 이름으로 저장소 생성
    $ git clone https://github.com/herdin/SimpleJava.git
    #SimpleJava2 이름으로 저장소 생
    $ git clone https://github.com/herdin/SimpleJava.git SimpleJava2
    

    –depth

    모든 commit 을 받지 않고, 정해진 깊이만큼만 받는다. 커밋 히스토리가 많을때 빠르다나?

    $ git clone --depth <DEPTH> <GIT REPOSITORY URL>
    $ git clone --depth 1 https://github.com/herdin/msa-prototype-member.git
    

    branch

    branch 목록을 보여준다.

    -d

    선택한 branch 를 삭제한다.

    -m, -M

    branch 의 이름을 변경한다.

    $ git branch <old branch name> <new branch name>
    

    -r

    remote 저장소의 브랜치도 보여준다.

    -a

    local, remote 저장소를 모두 보여준다.

    $ git branch -a
      develop
      feature-20210802-1635
    * feature-temp
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/develop
      remotes/origin/feature-20210802-1635
      remotes/origin/master
    

    -v, -vv

    branch 정보를 자세히 보여준다. -vv 를 사용하면 로컬브랜치가 추적하고 있는 리모트 브랜치도 보여준다.

    $ git branch -v
      develop      0222a9e done code01
      develop-temp 0222a9e done code01
      master       3f274eb Merge pull request #2 from herdin/develop
    * test         0222a9e done code01
    
    $ git branch -vv
      develop      0222a9e [origin/develop] done code01
      develop-temp 0222a9e [origin/develop] done code01
      master       3f274eb [origin/master] Merge pull request #2 from herdin/develop
    * test         0222a9e [origin/develop] done code01
    

    checkout

    현재 작업중인 branch 를 변경한다.

    $ git branch
      develop
      feature-02
    * feature-03
      hotfix-3
      master
    lg@HARM-LAPTOP MINGW64 /c/noneedinstall/PortableGit/localRepo/GitFlow (feature-03)
    $ git checkout develop
    Switched to branch 'develop'
    
    lg@HARM-LAPTOP MINGW64 /c/noneedinstall/PortableGit/localRepo/GitFlow (develop)
    

    -b

    branch 를 만듬과 동시에 현재 작업중인 branch 를 변경한다.

    lg@HARM-LAPTOP MINGW64 /c/noneedinstall/PortableGit/localRepo/GitFlow (develop)
    $ git checkout -b feature-04
    Switched to a new branch 'feature-04'
    
    lg@HARM-LAPTOP MINGW64 /c/noneedinstall/PortableGit/localRepo/GitFlow (feature-04)
    
    # v.1.1.0 tag 로 부터 branch 를 생성한다. 방법이 두개인데 둘다 작동하나?
    $ git checkout -b hotfix-1 v.1.1.0
    $ git checkout tags/v.1.1.0 -b hotfix-1
    

    -t

    remote 저장소의 입력한 branch 와 같은 이름의 로컬 트래킹 branch 를 만든다.

    $ git checkout -t origin/develop
    

    remote

    리모트 저장소와 관련된 명령어들.

    # 단축 이름과 함께 조회한다.
    $ git remote -v
    
    # 리모트 저장소를 추가한다.
    $ git remote add <단축 이름> <리모트 저장소 주소>
    
    # 리모트 저장소를 삭제한다.
    $ git remote remove <단축 이름>
    

    add

    작성중

    # filename 만 stage 에 넣는다.
    $ git add <filename>
    
    # 변화된 파일, 추가된 파일을 stage 에 넣는다.
    $ git add .
    
    # 변화된 파일, 삭제된 파일을 stage 에 넣는다.
    $ git add -u
    
    # 변화된 파일, 추가된 파일, 삭제된 파일을 stage 에 넣는다.
    $ git add -A
    

    commit

    작성중

    # 커밋메세지를 작성할 수 있는 편집기를 열지 않고 커맨드로 메세지를 작성한다.
    $ git commit -m "short message here"
    

    push

    remote repositorylocal repositorycommit 내역을 전송한다.

    pushcommit 은 수정하지 말라고 하지만, 아마 수정할 일이 많을 것 같긴한데 아무튼 pushcommit 메세지를 수정할 일이 생겼다. 이때는 그냥 git push -f

    # remote repository origin 에 remote branch master 에 local master branch 를 push
    $ git push origin master
    
    # remote branch origin 에 remote branch hotfix-1 에 local hotfix branch 를 push
    $ git push origin hotfix-1:hotfix-1
    
    # 위와 같은데 remote 와 local branch 가 같다면 생략할 수 있다.
    $ git push origin hotfix-1
    

    -u

    push 할때 사용한 remote branch 와 local branch 를 연결하여 다음 부터는 생략할 수 있도록 한다

    $ git push -u origin master
    # 뭔가 commit 하고, 이후로는 remote local 을 써주지 않아도 된다.
    $ git push
    

    -d, –delete

    remote repository 를 삭제한다.

    # 전체 branch 조회
    $ git branch -a
      develop
      feature-a
    * feature-test
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/develop
      remotes/origin/feature-test
      remotes/origin/master
    
    # remote feature-test branch 를 삭제한다.
    $ git push -d origin feature-test
    To https://github.com/herdin/gitStudy.git
     - [deleted]         feature-test
    
    # remote feature-test branch 가 삭제 된 것을 확인.
    $ git branch -a
      develop
      feature-a
    * feature-test
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/develop
      remotes/origin/master
    
    

    -f, –force

    나 혼자 쓰는 branch 인데, rebase 등으로 push 가 안될 떄가 있다. 그럴땐 그냥 강제로 push.

    develop-temp2 branch 를 만들고, 두번 커밋을 한 상태에서 push, local repository 에서 1개로 커밋을 rebase squash 로 합친다

    % git branch -a
      develop
      develop-temp
    * develop-temp2
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/develop
      remotes/origin/develop-temp2
        remotes/origin/master
    

    log

    커밋 이력을 보여준다.

    -n ${commit count}

    commit count 만큼만 로그를 보여준다.

    –oneline

    한줄로 로그를 보여준다.

    –graph

    커밋 그래프를 보여준다.

    tag

    특정 commit 에 꼬리표를 붙인다.
    태그에는 Lightweight 태그와 Annotated 태그가 있는데, Annotated 태그에는 작성한 사람의 이름과 이메일, 메세지 등을 저장할 수 있다고 한다.

    그래서 뭐가 다른건진 잘 모르겠다;;

    # tag 하기 - lightweight tag
    $ git tag v1.0
    
    # tag 하기 - 특정 커밋에 tag 달기
    $ git tag tagtest b55af48
    
    # tag 하기 - annotated tag 달기
    $ git tag -a vv1.0 -m "annotated tag 1.0"
    
    # 조회하기 
    $ git tag
    
    # 조회하기 - 패턴으로 라는데 잘안된다.
    $ git tag -l v* 
    
    # 조회하기 - commit 과 tag 를 함께
    $ git show v1.0
    
    # 조회하기 - parent 도 함께?
    $ git show --pretty=raw ${commit}
    
    # tag push to remote
    $ git push origin ${tag_name}
    $ git push origin v1.0
    
    # all tag push to remote
    $ git push origin --tags
    
    # tag push -d 하기
    $ git push origin :${tag_name}
    $ git push origin :v1.0
    
    
    # 삭제하기
    $ git tag -d v1.0
    

    push

    tag 는 branch 를 push 할때 자동으로 push 되지 않는다.
    따로 push 해줘야한다.

    git push origin v1.0
    

    revert

    revert 는 공부중

    reset

    git reset …

    rebase

    git rebase …

    blame

    남탓하기는 공부중