© 2020-present Sungjin Cho.
All Rights Reserved
Git의 명령어는 자세하게 알수록 과거 이력을 추적하는데에 도움을 준다.
추가적으로 Github 인증방식 그리고 Slack 연동등에 대해서 정리해보았다.
GPG로는 사용자 서명을 남긴다. SSH는 Github에 접속하기 위함이다. (push, pull을 하기 위함)
git-user는 사용자를 스위치하기 위함이다. git-user는 서명과 user.name, user.email은 변경해주지만
SSH등록은 git-user와 별개로 처리해줘야한다.
여러계정으로 push, pull을 하기 위해서는 SSH 등록도 계정개수만큼 해줘야한다.
SSH로 등록된 계정은 각각의 remote 주소를 갖는다. git@github.com:<owner>/<repo>.git
,
git@github-devstefancho.com:<owner>/<repo>.git
이런식으로 해당 ssh 키에 맞는 remote 주소를
등록해주어야한다.
git message는 message의 일관성을 위해서 등록해둔다.
vim ~/.gitmessage.txt # add git message config
git config --global commit.template ~/.gitmessage.txt # set git message as a template
-p
는 변경된 내용을 보여줌--follow
는 파일의 경로가 변경되어도 변경 이력을 보여줌git log --follow -p -- [file] # 파일의 변경 이력 확인 (경로 변경은 무시)
git log -p [file] # 파일의 변경 이력 확인
git log -n 500 --oneline # 최근 500개의 commit을 한 줄로 보여줌
git log origin/[BRANCH_NAME]..[BRANCH_NAME] # 로컬에는 있고 리모트에는 없는 커밋들 확인
git log [BRANCH_NAME]..origin/[BRANCH_NAME] # 리모트에는 있고 로컬에는 없는 커밋들 확인
git log [COMMIT_HASH]..[COMMIT_HASH] # commit 끼리 비교
git log [COMMIT_HASH]..[COMMIT_HASH] --author="John" # commit 끼리 비교, author 필터(일부만 들어가도 됨)
git show [commit] # commit의 변경 내용 확인
git show --name-only # filename only
git reset --hard origin/main # ~/.config main ⇣2⇡1 이런식으로, local과 remote의 차이가 있는 경우 remote로 동기화하기
git reset HEAD~1 # 바로 직전 commit을 리셋한다, git merge 이후에 바로 직전에 merge 한것을 취소할때도 사용할 수 있다.
git reset {commit-hash} # 해당 commit 까지는 유지하고, 이후 커밋들을 리셋한다.
git config --global user.name "조성진"
# 아래 이메일주소 패턴 관련 (https://github.com/orgs/community/discussions/41101)
git config --global user.email 1012345678+devstefancho-mydomain@users.noreply.github.com
# 저장소 설정 제거
git config --unset user.name
git config --unset user.email
# config 값 확인하기
git config --global user.email
# config 내용확인하기
cat ~/.gitconfig
# 전체 설정확인하기
git config --local -l
# upstream을 current로 지정하기
git config --global push.default current
# global editor 변경
git config --global core.editor "nvim"
git describe --tags --abbrev=0
git tag | grep -v '_rc' | sort -V | tail -n 1
git tag v1.0.0 # tag 생성하기
git push origin v1.0.0 # tag push
git stash apply $stash_hash # drop 처리한 stash commit을 알고 있다면, stash로 다시 적용할 수 있다.
git branch recovered $stash_hash # 또는 이 stash commit으로부터 새로운 branch로 분기시킬 수도 있다.
git worktree add {경로} -b {새 브랜치 이름}
git branch -vv # local branch name and remote branch name
git fetch origin develop
git update-ref refs/heads/develop origin/develop # develop branch를 origin/develop로 업데이트
## Reordering 참고 https://stackoverflow.com/questions/2740537/reordering-of-commits
git rebase -i HEAD~3 # 최근 3개의 commit을 rebase
git rebase origin/main # Rebase를 통해 로컬 커밋을 원격 커밋 위로 옮기기. 예를들어, origin/main이 1개 앞서있고, local에 신규로 2개 커밋이 있을때
만약 vim buffer에서 rebase를 중단하고 싶으면 :cq
를 입력하면 된다.
이후에 git rebase --abort
진행
만약 rebase를 완료했는데, 되돌리고 싶으면 git reflog
사용하면 된다.
reflog시에 아래와 같이 나온다면, git reset --hard HEAD@{14}
를 하면 1cbf1f30f 커밋으로 돌아간다.
31a1ceb78 HEAD@{10}: rebase (squash): test commit 2
a150a4983 HEAD@{11}: rebase (start): test commit 1
d8a3eb2b4 (HEAD -> feat/my-branch) HEAD@{12}: reset: moving to HEAD
d8a3eb2b4 (HEAD -> feat/my-branch) HEAD@{13}: commit: fix test
1cbf1f30f HEAD@{14}: commit (amend): chore: fix test
25858e345 HEAD@{15}: commit: chore: fix test
/github signin # login 하기
/github subscribe list features # 구독중인 리스트들
/github subscribe owner/repo comments reviews # comment와 review에 대해서 구독하기
/github unsubscribe owner/repo commits # commit은 구독하지 않기
/github unsubscribe owner/repo issues pulls commits releases deployments # default로 들어가는 것들 구독하지 않기
GPG 인증은 commit이나 tag에 서명하여 해당 commit, tag가 특정개인에 의해 생성되었음을 증명해준다. github에서 commit옆에 초록색 태그가 있는 경우가 GPG 인증이 되었음을 의미하는 것이다.
먼저 현재 등록되어 있는 key들에 대해서 확인해보자 현재 등록된 키가 있다면 아래명령어의 결과값으로 GPG Key값을 확인할 수 있다. 만약없다면 인증키를 생성하면 된다. 나는 gh cli를 사용해서 등록하였다. 등록을 마쳤으면 Settings > SSH and GPG keys에서 등록된 키를 확인할 수 있다.
gpg --list-secret-keys --keyid-format=long # gpg key 확인하기
gpg --armor --output mykey.asc --export {GPG Key} # GPG Key로 asc 파일 생성하기
gh auth refresh -s write:gpg_key # gh-cli를 사용하여 gpg key 등록할수 있도록 권한설정
gh gpg-key add mykey.asc # github에 gpg키 등록하기(https://cli.github.com/manual/gh_gpg-key_add)
list-secret-keys로 출력되는 값은 아래와 같고 각 항목에 대해 간략히 설명해보았다.
sec rsa4096/{GPG Key} {등록일 YYYY-MM-DD} [SC]
{Finger Print}
uid [ultimate] {이름} <{메일주소}>
ssb rsa4096/{Sub Key} {등록일 YYYY-MM-DD} [E]
{}
부분은 변수값gpg --full-generate-key # key 생성하기
위 명령어를 입력하면 key를 생성할 수 있다. 나는 옵션을 아래와 같이 선택하였다.
gpg --armor --output mykey.asc --export 142373285+devstefancho-mydomain@users.noreply.github.com
gh auth refresh -s write:gpg_key # gh-cli를 사용하여 gpg key 등록할수 있도록 권한설정
gh gpg-key add mykey.asc # github에 gpg키 등록하기(https://cli.github.com/manual/gh_gpg-key_add)
gpg --edit-key ABC1234567890ABC # 키 수정하기
gpg --delete-secret-key ABCDEFG012345 # 키 삭제하기
git log --show-signature # commit에서 GPG 서명을 확인할 수 있다. (merge commit에는 서명되지 않는것 같음)
git config --global user.signingkey # 현재 서명값 확인하기
git config --global user.signingkey {GPG Key 값} # 서명값 다시 설정하기
git config --global commit.gpgsign # commit에 서명유무에 대해 확인
git config --global commit.gpgsign true # commit에 서명하는 것에 대해서 true로 설정
cat ~/.gitconfig # 결과 확인
source ~/.zshrc
를 한다.export GPG_TTY=$(tty)
commit할때 passphrase를 입력하라고 나오는데, 나는 깃헙 password로 설정하였다.(Mac의 Passwords에 저장해둠)
기본 cache 시간이 10분이기 때문에, 계속 passphrase를 입력해줘야 하는 불편함이 있다. 그래서 기본 시간을 변경해주도록 한다
vi ~/.gnupg/gpg-agent.conf # 파일이 없다면 생성해준다.
## 아래 내용을 입력하고 저장한다.
# default-cache-ttl 3600
# max-cache-ttl 7200
gpgconf --reload gpg-agent # reload 해준다.
git commit이 실패하였다. 그리고 에러메시지는 아래와 같다.
error: gpg failed to sign the data:
gpg: Note: database_open 134217901 waiting for lock (held by 21223)
해결방법은 pubring.db.lock 파일을 지운다.
SSH 인증은 로컬 Git 클라이언트에서 SSH를 사용하여 Github 저장소에 접근하는 방식이다. Github 서버에 안전하게 접근하고 push, pull 하기 위해서 사용된다. 인증에는 public key, private key 두개가 쌍으로 필요한데, public key는 Github에 등록하고 이후에 Github에 접속할때 사용자의 private key로 자동인증을 하게 된다.
ls -al ~/.ssh # ssh 키가 있는지 확인
ssh-keygen -t ed25519 -C "devstefancho@mydomain.co.kr" # 없다면 key 생성
touch ~/.ssh/config # 없는 경우 생성
vi ~/.ssh/config # 수정하기(내용은 link의 ssh 생성하기 링크 참고)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519 # keychain에 등록하기
pbcopy < ~/.ssh/id_ed25519.pub # public key 복사 후 https://github.com/settings/keys에서 SSH 생성하기
# git push시에 로그인 하라는 메시지가 뜨는 경우 현재 remote가 https로 되어있어서 그런것이므로 ssh로 변경필요
git remote -v # remote 주소확인
git remote set-url origin git@github.com:<owner>/<repo>.git # ssh 주소로 변경
gp # git push
git push에서 아래와 같은 error 메시지가 뜨는 경우 yes를 해주면 된다.
The authenticity of host 'github.com (20.200.245.247)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
# 이미 id_rsa 이름이 있다면 다른 파일명으로 지정해줘야함
ssh-keygen -t rsa -b 4096 -C "your-email@example.com" -f ~/.ssh/id_rsa_newaccount
ssh-add ~/.ssh/id_rsa_newaccount # ssh 키 추가 (newaccount는 본인에 맞게 바꾼다.)
# 기본 계정
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# 새 계정
Host github-newaccount
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_newaccount
# 신규로 clone 받는 경우
git clone git@github-newaccount:username/repository.git
# 이미 clone은 받아져 있어서, origin 경로만 바꾸는 경우
git remote set-url origin git@github-newaccount:{Owner}/{Repository Name}.git
# 매칭되는 이메일이 결과에 나와야한다.
ssh -T git@github.com # 명령을 실행했을 때 "Hi {id_rsa에 등록된 email주소}! You've successfully
ssh -T git@github-newaccount.com # 명령을 실행했을 때 "Hi {id_rsa에 등록된 email주소}! You've successfully
ssh-add 된것을 다 지우고 다시 .ssh/config에 추가한 순서대로 ssh-add를 해준다.
eval "$(ssh-agent -s)" # agent 초기화
ssh-add -D # ssh-add 된것들 모두삭제
ssh-add ~/.ssh/id_rsa # .ssh/config에 있는 첫번째 항목 추가
ssh-add ~/.ssh/id_rsa_mydomain # 두번째 항목추가
우선 github에 해당 ssh키의 pub키가 등록되어있는지 확인이 필요하다.
.ssh/id_rsa_mydomain.pub | pbcopy
이렇게 복사하여 이 값을
Github > Settings > SSH and GPG Keys에서 New SSH Key로 등록해본다.
이미 ssh키가 있는 경우 중복 등록은 안된다.
ssh-add list를 확인하도록 한다.
### .ssh/config에 아래와 같이 두개의 ssh가 등록되어 있는경우 ###
# ------------------------------------------
# Host github.com
# AddKeysToAgent yes
# UseKeychain yes
# IdentityFile ~/.ssh/id_ed25519
#
# Host github-dev
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_rsa_mydomain
# ------------------------------------------
ssh-add -l # list 확인
ssh-add ~/.ssh/id_rsa_mydomain # 리스트에 없다면 새로 추가
ssh -T git@github-dev # ssh 연결확인
sudo npm i -g git-user-switch # 명령어 설치하기
git-user # 유저 변경하기
git-user -d # 유저 삭제하기
git-user로 계정을 변경후에 아래와 같은 gpg 에러가 발생할 수 있다. 이 경우는 gpg 키가 빈값으로 들어간 듯 하다. 유저를 삭제하고 git-user에 다시 등록해주면 된다.
error: gpg failed to sign the data:
gpg: skipped "": Invalid user ID
[GNUPG:] INV_SGNR 0
[GNUPG:] FAILURE sign 37
gpg: signing failed: Invalid user ID
fatal: failed to write commit object
gh cli를 사용하여 user를 switch하고 ssh도 변경할 수 있다. (https://github.com/cli/cli/discussions/6889)
gh auth switch
gh auth setup-git