반응형
소스코드 관리를 위한 분산 버전 관리 시스템이다
유사 프로그램은 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 폴더 제거 방법 (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
반응형
'IT > Git' 카테고리의 다른 글
linebreak-style 오류 해결 (1) | 2023.06.03 |
---|---|
앱 버전관리 Semantic Versioning (0) | 2022.09.20 |
Git Commit Message 정리 (0) | 2022.08.26 |
GIT commit message 규칙 (0) | 2022.08.12 |
fatal: refusing to merge unrelated histories (0) | 2020.02.18 |
git log 한글 깨짐 (windows) (1) | 2020.02.13 |
Git 사용시 실수 대처법 (0) | 2020.02.03 |
git 줄바꿈 문제 (warning: CRLF will be replaced by LF in .gitignore) (0) | 2019.10.17 |