Git 쓰면서 겪었던 상황과 이를 해결한 명령어 모음입니다.
터미널 환경에서 GIT을 사용할 경우 명령어로 쉽게 git command 정보를 알 수 있다. 바로 --help
옵션을 사용하는 것이다.
Git add 명령어 정보 보기
git add --help
Git checkout 명령어 정보 보기
git checkout --help
git branch -m <변경 전 branch name> <변경 후 branch name>
git branch -D <local branch name>
Remote branch를 수정하지 않고 단지 읽고만 싶은 경우
git checkout <remote branch 이름>
위 명령어를 수행하면 해당 branch는 detached HEAD 상태가 되며, 로컬 환경에서 해당 branch의 내용을 변경할 수는 있지만, commit과 push와 같은 명령어는 사용할 수 없다.
다른 branch로 checkout하면 해당 branch는 사라진다.
Local git remote 정보 최신화하기
git remote update
Remote Branch 이름 그대로 가져오기
git checkout -t origin/feature/issue-1
git branch -r
또는 git branch -a
목록 중 origin/feature/issue-1 원격 브랜치를 가져온다.
Remote Branch 이름 변경한 후 가져오기
git checkout -b <변경할 branch 이름> origin/feature/issue-1
-b
: branch를 만든 후 가져오기
Remote repository 목록보기
git remote -v
Local branch 이름과 remote branch 이름이 같은 경우
git push origin <local branch 이름>
Local branch 이름과 remote branch 이름이 다른 경우
git push origin <local branch 이름>:<remote branch 이름>
Remote branch가 존재하지 않은 경우 이를 만들면서 push하는 경우
git push -u origin <local branch 이름>
Remote repository와 local repository에 있는 파일을 삭제한다.
git rm <file 이름>
Remote repository에 있는 파일을 삭제한다. (local repository는 삭제하지 않는다.)
git rm --cached <file 이름>
git add 전체 취소하기
git reset HEAD
git add 특정 파일 취소하기
git reset HEAD <file 이름>
마지막 commit에 대한 정보를 변경할 때는 -amend
옵션을 사용한다.
git commit --amend
위 명령어를 사용하면 마지막 commit의 정보와 함께 자동으로 텍스트 편집기가 열린다. 여기서 수정할 수 있는 것은 commit 메시지 뿐이다. commit의 정보를 변경하여 저장하여도 반영되지 않는다.
commit 날짜 변경 임의의 날짜로 변경
git commit --amend --no-edit --date="Mon 20 Aug 2020 19:19:19 KST"
git commit --amend --no-edit --date="Mon 26 Apr 2021 20:17:19 KST"
git commit --amend --no-edit --date="Mon 26 Apr 2021 21:08:34 KST"
git commit --amend --no-edit --date="Mon 26 Apr 2021 21:33:02 KST"
현재 날짜로 변경
git commit —amend —no-edit —date="$(date)"
작성자 변경
git commit --amend --author="Author Name <email@address.com>" --no-edit
https://stackoverflow.com/questions/3042437/how-to-change-the-commit-author-for-one-specific-commit
rebase 명령어를 사용하면 여러 commit 정보를 한꺼번에 변경할 수 있다.
How to change the commit author for one specific commit?
# 최초 커밋부터 변경하려면
git rebase -i --root
git checkout main
git merge --squash sub
git commit
git commit
명령어를 수행하면 커밋 메시지를 수정하는 에디터가 실행되며, 머지하려는 브랜치의 커밋 이력이 담겨있다. 자유롭게 수정하여 커밋하자.
git checkout main
git merge --squash sub
git commit -m "commit message"
git reset --hard ORIG_HEAD
git reset --merge ORIG_HEAD
~/.gitconfig에 아래 내용을 입력하여 저장한다.
# for github remotes
[alias]
pr = "!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f"
pr-clean = "!git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done"
# for bitbucket/stash remotes
spr = "!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull-requests/$1/from:pr/$1 && git checkout pr/$1; }; f"
그 후 아래 명령어로 해당 PR 번호에 해당하는 remote branch를 checkout하여 local 환경에 가져올 수 있다.
git pr <PR 번호> <remote repository 이름>
git remote add upstream
git fetch upstream 자신의 github_id
git reset --hard upstream/자신의 github_id
git push --force [remote repository]
https://planbs.tistory.com/entry/Git-Pull-request에서-발생하는-충돌-해결하기
Git Rebase 제대로 알고 쓰기 (feat. cherry-pick)
현재 작업을 임시로 저장하고 비우기(stash)
git stash
git stash save
stash 목록
git stash list
가장 최근 stash된 목록 가져오기
git stash apply
stash 목록 중 선택하여 가져오기(stash 목록 형식: stash@{<번호>})
git stash apply <가져올 stash 목록>
가장 최근 stash된 목록 삭제하기
git stash drop
stash 목록 중 선택하여 삭제하기
git stash drop <삭제할 stash 목록>
가장 최근 stash된 목록을 적용하고 삭제하기
git stash pop
병합 충돌을 일으키는 git stash pop 실행 취소
사용자 이름 확인하기
git config user.name
사용자 이메일 확인하기
git config user.email
사용자 이름 변경하기
git config --global user.name <변경할 사용자 이름>
사용자 이메일 변경하기
git config --global user.email <변경할 이메일>
gitconfig 파일에서 변경하기
vi ~/.gitconfig
위 명령어로 편집기를 열면 user 정보를 볼 수 있다. 여기서 변경을 원하는 내용을 삽입하고 저장하면 된다.
~/.gitconfig 파일은 git config --global
로 읽을 수 있는 정보들이 저장되어 있는 파일이다.
.gitignore 파일에 git이 추적하고 싶지 않은 파일을 추가한 후 아래 명령어를 수행하면 바로 적용할 수 있다.
git add .
수행)git rm -r --cached .
git add .
git commit -m "<commit message>"
위 명령어는 remote branch로 이미 올라간 repository에 .gitignore을 적용하고 싶은 경우에도 사용할 수 있다.
Github의 비밀번호를 변경한 후 Github Repository에 push와 같은 명령어로 호출을 할 때 사용자 정보가 일치하지 않는다는 에러가 발생한다. 이러한 경우 다음 과정을 수행한다.
Local 환경의 Github 인증 초기화하기
git config --unset credential.helper
초기화한 후 git push 명령어 수행하기
git push <remote repository 이름> <local branch 이름>:<remote branch 이름>
인증 정보를 초기화한 후 git push를 수행하면 Github username과 password를 재입력받는다. 이 때 변경한 비밀번호를 입력하면 그 후에는 이전과 같이 별도의 정보 입력없이 push가 가능하다.