본문 바로가기
IT/Git

Git 명령어 정리

by DOSGamer 2022. 8. 26.
반응형

소스코드 관리를 위한 분산 버전 관리 시스템이다

유사 프로그램은 SVN (Subversion) 이 있다

공식문서

원격 repository 연결

## origin connected to a remote repository
git remote add origin <RemoteURL>
## returns a list of the remote repository
git remote -v
## unconnected to a remote repository
git remote remove origin
## change remote alias name
git remote rename <OldAlias> <NewAlias>

상태확인

git status
git status --short
git status -sb

변경적용

## adds the file to the stage section
git add file
## stage all changed files
git add .

변경취소

## discards changes to the file
git restore file
## removes the file from the stage section
git restore --staged file
## Unstage a particular files
git reset HEAD file
## reset all to head
git reset --hard

차이점 검사

git diff
git diff --staged
## displays changes between the working tree and dev branch
git diff dev
## displays changes between the branches master and dev
git diff master..dev

커밋

git commit -m 'Commit message'
## change commit message
git commit --amend -m 'New Commit message'
## commit the added files but keep commit message same
git commit --amend --no-edit
## un-commit
git reset --mixed HEAD~

PUSH

## git push origin repository's BranchName
git push origin <BranchName>
git push --set-upstream origin <RemoteBranchName>
git push -u origin <RemoteBranchName>

브랜치 변경

## switches to a <BranchName> branch
git switch <BranchName>
## create branch <BranchName> and switches to it
git switch -b <BranchName>
## HEAD to any commit/tag
git checkout [args]
## checkout to local branch
git checkout -b <BranchName>
## remote branch checkout
git fetch && git checkout <RemoteBranchName>

## show all Local branch
git branch
## show all Remote branch
git branch -r
## All local and remote branch
git branch -a
## creates <BranchName> branch
git branch <BranchName>
## deletes <BranchName> if already merged
git branch -d <BranchName>
## force deletion
git branch -D <BranchName>

## delete <BranchName> branch from remote
git push origin --delete <BranchName>

로그 확인

## display the changes the commit description
git log -p
## Return n latest commits from your current position
git log -2
## display log for a specific branch/revision
git log <BranchName>
## filter log for a specific files
git log *.js
## filter log by author
git log --author skyksit
## filter log by date
git log --since 2020-03-23
## filter log by date using dynamic date ranges
git log --since 3months --until 1months

Aliases 설정

.gitconfig 파일에서 alias 를 설정할 수 있다

[alias]
  br = branch
  st = status
  rt = restore
  sw = switch
  ci = commit
  d = diff
  dc = diff --cached
  l = log
  su = git stash show -p | git apply -R

Merge

## merges the <BranchName> branch into yours
git merge <BranchName>
## squash all of the changes into one commit
## you don’t want to include the commits’ history when merging the changes
git merge --squash <BranchName>

git merge origin <RemoteBranchName>

임시저장

git stash
git stash save
## show stash list
git stash list
## apply lastest stash
git stash apply
git stash apply <StashName>
## delete stash
git stash drop
git stash drop <StashName>
## stash apply and drop
git stash pop
## restore stash
git stash show -p | git apply -R
git stash show -p <StashName> | git apply -R

local Git 확인

## local user name
git config --show-origin --get user.name

Commit Message

Caching your GitHub password in Git

https://help.github.com/en/github/using-git/caching-your-github-password-in-git

개행문제해결하기

Windows 에서는 line ending 을 CR(Carriage-Return, \r)과 LF(Line Feed, \n)을 사용하고 Unix와 Mac 은 LF 만 사용한다

소스 변경 없는데 CR/LF 때문에 변경으로 착각하여 commit 을 하게 된다

OS 가 달라도 문제가 없도록 crlf 를 처리하는 방법을 결정해야 한다

  • core.autocrlf = false. 기본 설정이다. 파일에 CRLF 를 썼든 LF 를 썼든 git 은 상관하지 않고 파일 그대로 checkin, checkout 한다. 이 설정은 line ending 이 다른 OS 에서는 text file 이 변경되었다고 나오므로 위에서 언급한 여러 가지 문제가 발생할 수 있다.
  • core.autocrlf = true text file을 object database 에 넣기전에 CRLF 를 LF 로 변경한다.
  • core.autocrlf = input LF를 line ending 으로 사용한다.

해결방법

윈도우OS 는 object 에 넣기전에 자동으로 lf 로 변경하고 Linux 와 Mac 은 lf 로 사용한다

# Windows
git config --global core.autocrlf true

# Linux, Mac
git config --global core.autocrlf input

Git Push 취소 (원격 파일 삭제)

# 원격 저장소와 로컬 저장소의 파일을 둘 다 삭제
git rm <fileName>
# 원격 저장소에 있는 파일만 삭제
git rm --cached <fileName>
# 원격 저장소에 있는 폴더 삭제
git rm -rf --cached <folderName>

Git add 취소

실수로 add 한 파일들 취소하기

git reset HEAD <fileName>

Git Commit 취소

실수로 commit 한 파일들 취소하기

# commit 된 모든 파일을 취소하고 stage 상태 Rollback
git reset --soft HEAD

# commit 된 모든 파일을 취소하고 unstage 상태로 Rollback
git reset --mixed HEAD

git reset HEAD

# commit 된 모든 파일을 취소하고 특정 커밋으로 Rollback
git reset --hard <commitID>

참고포스트

git 사용법과 협업 Home
github 프로젝트의 readme 에 ci, code quality, download 횟수등 프로젝트의 상태와 현황을 파악할 수 있는 지표를 표시하는 뱃지 달기 Travis ci 프로젝트에 들어간 후에 상단의 bu ... 익히 알다시피 Windows 에서는 line ending으로 CR(Carriage-Return, \r)과 LF(Line Feed, \n)을 사용하고 Unix 나 Mac OS 는 LF 만 사용한다. 이는 상당히 골치 ...
https://www.lesstif.com/gitbook/git-home-27984628.html

하위폴더의 .git 폴더 제거 방법 (submodule 인식 제거)

하위폴더에 .git 폴더가 있으면 submodule 로 인식한다

하위폴더에 .git 폴더가 있으면 이런 메시지를 볼 수 있다

해결방법은

submoule 로 등록을 하던지

git submodule add <url> themes/icarus

index 에서 삭제하고 나머지만 올리던지

git rm --cached themes/icarus

icarus 에서 .git 폴더를 삭제해서 repository 와의 연결을 끊어버리고 올린다

rm themes/icarus/.git


Uploaded by N2T

반응형