GIT tip 정리
충돌 회피
git push 충돌 회피
# git pull시 현재 commit하지 않은 것 때문에 충돌이 발생하면 아래와 같이 stash를 이용해서 임시저장 할 수 있다.
git stash
git stash list
git pull
git stash pop
submodule
가장 처음 실행 했을 때
git submodule update --init --recursive지속적인 업데이트
git submodule update --recursive상태 확인
git submodule status변경 사항 있을 때 강제로 원래대로 돌리기
git submodule update -f --init --recursiveGit stash
branch를 변경할 때 임시로 파일을 저장하는 수단.
# 임시 저장
git stash
# 임시저장 리스트 확인
git stash list
# 임시저장 적용하고 바로 stage 상태로 만듬
git stash apply --index
# 임시저장 제거, hash를 지정하지 않으면 가장 최근 것을 제거함.
git stash drop
# 적용하면서 바로 제거함
git stash pop파일 삭제
git rm
working directory의 실제 파일을 삭제하는 것이다.
그리고 commit을 해야 한다.
git rm --cached
Staging Area (git index)에서만 제거하고 Working directory에 있는 파일은 지우지 않고 남겨둘 수 있다.
다시 말해서 하드 디스크에 있는 파일은 그대로 두고 Git만 추적하지 않게 한다.
이것은 .gitignore파일에 추가하는것을 빼먹었거나 대용량 로그 파일이나 컴파일된 파일 .a파일 같은 것을 실수로 추가 했을 때 쓴다.
$ git rm --cached readme.txt
#여러게 삭제
$ git rm log/\*.log
rm.txt 파일 생성해서 하기*명령으로는 이름 매칭이 안될 수도 있다.
- git status > rm.txt형식으로 list를 만들고 파일명을 다듬은 다음
- cat rm.txt | xargs git rm --cached으로 해당 txt에 있는것을 staging area에서 제거 한다.
git log
- checkout 하고 log가 안보이면 git log --all
- git reflog
특정 파일 변화 로그 찾기
- git log -p -- ./vta/tutorials/resnet.py
 파일 이름 바뀐것 까지 참조하면서 모두 보이기
- git log --follow -p -- 
HEAD detached at
checkout을 sha1 hash코드로 하게 되면 자동으로 HEAD가 설정 되지 않는다.branch 이름으로 할 경우 이런 문제가 발생하지 않지만 그냥 할 경우 발생하게 된다.
commit message를 잃어 버릴 수도 있기 때문에 HEAD 설정을 다시 해주는 것이 좋다.
- merge를 이용한 방법  git commit -m "....." git branch my-temporary-work git checkout master git merge my-temporary-work
git diff
기본 설정 포멧
git diff [--options] <commit> <commit> [--] [<path>...]지금 커밋과 뒤로 두 단계 커밋의 main.c에 대한 비교 커멘드는 아래 3개 모두 동일한 기능을 한다.
$ git diff HEAD^^ HEAD main.c
$ git diff HEAD^^..HEAD -- main.c
$ git diff HEAD~2 HEAD -- main.cremote하고의 비교
git fetch origin #pull과 다르게 local repo.와 merge하지 않음. 이력만 받아옴.
git diff master origin/master특정 파일만 보고 싶은 경우
git ls-files | grep .java | xargs git diff간단히 파일 변화만 보기 --state
$ git diff --stat 2ed6ec *.R
 2018_03_09_NB_SVM_RF_MLP.R       | 314 +++++++++++++++++++++++++++++++
 Class Imbalance.R                |  90 +++++++++
 FeatureSelectionWithRFE.R        |  59 ++++++
 FileWriter.R                     |  93 ++++++++++
 evaluationModel.R                |  38 ++++
 graph.R                          | 188 +++++++++++++++++++
 jsonESLABCollector.R             | 116 ++++++------
 jsonToDataFrame.R                | 385 +++++++++++++++++++++++++++++++++++++++
 jsonToSingleDataFrame.R          | 208 +++++++++++++++++++++
 phone_status_anlysis.R           |  41 ++++-
 post_matchig_with_arrival_time.R | 277 ++++++++++++++++++++++++++++
 refinedAutoLabels.R              | 158 ++++++++++++++++
 12 files changed, 1905 insertions(+), 62 deletions(-)git 무식하게 완전 초기화
rm -rf .git
git init
git add .
git commit - m "initial commit"
git remote add origin <github-uri>
git push -u --force origin --all단 위 방법은 submodule이 있을 경우 문제가 발생한다.
그 때는 rebase를 이용해서 정공법으로 해결해야 한다.
Branch 관련
Branch 삭제
#local delete
git branch -d <branch_name>
#remote delete
git push <remote_name> :<branch_name>Branch 이름 변경
# Local에 있는 branch 이름 변경하기
git branch -m oldbranch newbranch
# Remote에 존재하는 oldbranch 삭제하기
git push origin :oldbranch
# newbranch push 하기
git push origin newbranch파일 복원
파일 변경 사항 알아내기
특정 파일 변동 사항 검색
git log -- UNLOCKED_Analysis.R해당 로그 확인
$ git show --stat f27a8d
commit f27a8d01f6fa828d4e38919823fb1ddb462136b6
Author: Jemin Lee <leejaymin@cnu.ac.kr>
Date:   Tue Nov 8 23:50:22 2016 +0900
    change file name
 README.md                                  | 17 +++++++++++++++++
 jsonToDataFrame.R                          |  9 +++++++++
 phone_status_anlysis.R                     |  1 +
 UNLOCKED_Analysis.R => refinedAutoLabels.R |  4 +++-
 4 files changed, 30 insertions(+), 1 deletion(-)위와 같이 UNLOCKED_Analysis.R파일이 refinedAutoLabels.R로 변경된 것을 알 수 있다.
갑자기 파일이 사라져서 log를 통해서 확인해본 예제이다.
개별파일 원복
git checkout  -- <파일> : 워킹트리의 수정된 파일을 index에 있는 것으로 원복
git checkout HEAD -- <파일명> : 워킹트리의 수정된 파일을 HEAD에 있는 것으로 원복(이 경우 --는 생략가능)
git checkout FETCH_HEAD -- <파일명> : 워킹트리의 수정된 파일의 내용을 FETCH_HEAD에 있는 것으로 원복? merge?(이 경우 --는 생략가능)
index 추가 취소
git reset -- <파일명> : 해당 파일을 index에 추가한 것을 취소(unstage). 워킹트리의 변경내용은 보존됨. (--mixed 가 default)
git reset HEAD <파일명> : 위와 동일
특정 commit message에서 복원$ git checkout ec76bc~1 allData.RData
commit 취소
- git reset HEAD^ : 최종 커밋을 취소. 워킹트리는 보존됨. (커밋은 했으나 push하지 않은 경우 유용)
- git reset HEAD~2 : 마지막 2개의 커밋을 취소. 워킹트리는 보존됨.
hard, soft, mixed 차이점
- git reset --hardHEAD~2- 마지막 2개의 커밋을 취소. stage와 워킹트리 모두 원상 복구. 즉 모든 상태가 복구됨.
 
- --soft:- stage에 올라간 상태로 변경이력이 복구됨
 
- --mixed;- 워킹트리에만 변경사항이 남아있음. 커밋은 완전 초기화
 
revert 방식reset 계열은 결국 커밋을 조작 하기 때문에 혼자 사용하는 branch에서 사용하는것이 적당하다.
여럿이 사용하는 branch라면 revert를 이용해서 이력을 rollback한 내용들도 commit으로 남기는 것이 좋다.
- git revert HEAD : HEAD에서 변경한 내역을 취소하는 새로운 커밋 발행(undo commit). (커밋을 이미 push 해버린 경우 유용)- 취소 커밋 범위 설정 가: git revert [hash1]..[hashN]
 
워킹트리 전체 원복
git reset --hard HEAD : 워킹트리 전체를 마지막 커밋 상태로 되돌림. 마지막 커밋이후의 워킹트리와 index의 수정사항 모두 사라짐.
                                  (변경을 커밋하지 않았다면 유용)
git checkout HEAD . : ??? 워킹트리의 모든 수정된 파일의 내용을 HEAD로 원복.
git checkout -f : 변경된 파일들을 HEAD로 모두 원복(아직 커밋하지 않은 워킹트리와 index 의 수정사항 모두 사라짐. 신규추가 파일 제외)
* 참조 : reset 옵션
--soft : index 보존, 워킹트리 보존. 즉 모두 보존.
--mixed : index 취소, 워킹트리만 보존 (기본 옵션)
--hard : index 취소, 워킹트리 취소. 즉 모두 취소.
* untracked 파일 제거
git clean -f
git clean -f -d : 디렉토리까지 제거
Git tracked file 용량 파악 하기
git count-objects -vH
100MB 이상 파일 push 하기
100MB이상의 파일을 push 할 경우 아래와 같은 error 메시지를 나타낸다.
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
remote: error: Trace: a021e64e835a3163bc978f98da55335e
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File huge_files.zip is 151.37 MB; this exceeds GitHubs file size limit of 100.00 MBlarge file system (lfs)를 설치하고 이를 이용해서 commit & push를 한다.
brew install git-lfs
git lfs install # you only need to run this once per user account
git lfs track "*.psd" # Git LFS selects the file types you would like.
git add .gitattributes # make sure .gitattributes is tracked.
git add file.psd
git commit -m "Add design file"
git push origin master트랙킹 파일 리스트 확인: git lfs ls-files
민감 파일 이력에서 자동 찾아 제거 (sensitive data remove in entire history)
.gitignore설정을 하지 않아 password와 같은 정보가 들어 있는 config 파일이 commit push 되었을 때 변경이력에서 이러한 부분을 제거하는 방법을 다룬다.
그냥 untacked시키는 방법은 git rm --cached를 사용하면 되나 저장소 history안에 해당 파일 변경 내역이 그대로 남기 때문에 문제는 계속된다.
해결방법으로는git filter-branch와 BFG Repo-Cleaner를 이용한 방법 있다.
BFG Repo-Cleaner를 이용한 방법을 다룬다.
- java 설치
- BFG jar 파일 다운 https://rtyley.github.io/bfg-repo-cleaner/
java -jar bfg-1.13.0.jar --delete-files <지울 파일 이름>
java -jar bfg-1.13.0.jar --replace-text <지울 파일 이름>
# 원격 저장소에 강제로 반영
git push origin --force --allGit commit 메시지들 정리하기
Squash all commits (Pull Request 이후에 처리)
git checkout <branch-name>
git rebase -i master # interactive mode
# vim 화면에서 pick이 선택이고 s가 squash 한다는 의미가 된다.
# 그 다음 새로운 commit log를 작성하게 된다.
git push origin branch-name --forcePR이 다수 반영된 upstream 내용을 origin과 local에 반영하기
아래는 여러 방법들 중 하나의 것이다.
git checkout master
git fetch upstream
git rebase upstream/master
git push -f origin master기존 local branch 삭제
git branch -d <branch name>참고자료
공식 홈피: https://help.github.com/en/articles/removing-sensitive-data-from-a-repository
BFG: https://rtyley.github.io/bfg-repo-cleaner/
Gitlab, Merge-request fetch 방법
vi ~/.gitconfig
# 추가
[alias]
    mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -
# fetch and checkout.
# specify mr number in the last
git mr origin 5또는git fetch origin merge-requests/58/head:relay_mr  
참고자료
- checkout locally by adding a Git alias, https://docs.gitlab.com/ee/user/project/merge_requests/reviews/index.html
참고자료
'Computer Science > Git' 카테고리의 다른 글
| GPG sign off (서명 설정) (0) | 2021.03.26 | 
|---|---|
| git difftool 사용법 (0) | 2017.02.28 | 
| 자동으로 commit 위치 찾아서 Git으로 지운 파일 복원하기 (0) | 2017.01.31 | 
| Git config 설정 (계정설정) (1) | 2016.10.02 | 
| Github에 100Mb이상 파일 올리는 방법 (오류 해결) (5) | 2016.08.09 |