본문으로 건너뛰기

Git 명령어

  author: Dev.ian
createdAt: 2024-08-18
updatedAt: 2024-08-19

Status

Git Status

  • 현재 상태Status를 확인
    • Branch Name
    • Remote Repo 의 상태(Fetch or Pull 해야할 내용이 있는지)
    • 현재 작업 중인 파일들의 상태 (stage 상태인지)
  $ git status

On branch feature/example
Your branch is ahead of 'origin/feature/example' by 1 commit.

Git Log

  • Git 이력 확인
    • git log -1 command 로 직전 커밋의 정보(작업자, 커밋일시, 커밋내용, Revision 등)를 확인할 수 있다.
  $ git log -1

commit 14c65656386493c2108355a6d7345259bcf3f55d (HEAD -> feature/example)
Author: dev.ian <abcd1234@email.com>
Date: Sun Aug 11 15:54:53 2024 +0900

add new wiki

Push

Git Push

  • Commit 한 내용을 Remote Repo 에 upload
    • -f 옵션을 이용해 force upload 가능
    • git push ${REMOTE_NAME} ${BRANCH_NAME}
  $ git push -f origin feature/example

Clone

  $ git clone ${SSH_URL}

Branch

List

  • To see local branches

      $ git branch

    * feature/example
    main
  • To see all local and remote branches

      $ git branch -a

    * feature/example
    main
    remotes/origin/HEAD -> origin/main
    remotes/origin/feature/example
    remotes/origin/main
  • To see remote branches

      $ git branch -r

    remotes/origin/HEAD -> origin/main
    remotes/origin/feature/example
    remotes/origin/main
  • To see branch name

      $ git branch --show-current

    feature/example
  • To see revision

      $ git rev-parse HEAD

    14c65656386493c2108355a6d7345259bcf3f55d

Create

  • create a new branch

      $ git branch ${NEW_BRANCH_NAME}
  • create a new branch and swtich to a branch

      $ git checkout -b ${NEW_BRANCH_NAME}

Switch

  • switch to a branch in your local repo

      $ git checkout ${BRANCH_NAME}

Delete

  • To delete a remote branch

      $ git push origin --delete ${BRANCH_NAME}
  • To delete a local branch

      $ git branch -d ${BRANCH_NAME}
    $ git branch -D ${BRANCH_NAME}

Remote Repo

Confirm Remote Repo

  • origin: alias of remote repo
  • git@gitlab.com ~ .git: remote repo url
  $ git remote -v

origin git@gitlab.com:projects/sample1/ABCDE-service.git (fetch)
origin git@gitlab.com:projects/sample1/ABCDE-service.git (push)
  $ git config --get remote.origin.url

git@gitlab.com:projects/sample1/ABCDE-service.git

Change Remote Repo URL

  • git@gitlab.com:projects/sample1/ABCDE-service.git -> git@gitlab.com:projects/sample2/ABCDE-service.git
  $ git remote set-url origin git@gitlab.com:projects/sample2/ABCDE-service.git