Skip to content

git pro tips

Centell edited this page Dec 14, 2020 · 53 revisions

매번 push/pull 할 때마다 계정 정보 입력하기 귀찮다

git config credential.helper store

ref: How can I perform a git pull without re-entering my SSH password?

현재 작업물을 다른 브런치에 커밋하기

git stash
git checkout other-branch (브런치를 생성해야하면 git checkout -b other-branch)
git stash pop

Private repo 를 서버에 clone 하기

$ ssh-keygen

으로 rsa 키를 생성한 뒤(참고), public 키를 github > settings > SSH keys 에 등록해준다.

다른 방법: 참고 링크

가장 최신 commit의 author 수정하기

∙ 주의사항

commit을 수정하면 해당 commit의 key 값이 변경된다. history가 일치하지 않는 문제가 생기므로 remote에 push하려면 $ git push --force 혹은 $ git push --force-with-lease를 사용해야 한다. 공동작업자가 있는 경우 기존 작업물을 삭제하고 새로 clone해야 하는 등의 여러 불편함이 생기므로 commit의 변경은 되도록이면 하지 않는 편이 좋다.

∙ 방법

$ git commit --amend --author="username <eamil@example.com>"

이 때 이메일을 < >로 감싸지 않으면 다음과 같은 에러 메세지가 뜬다.

fatal: --author 'username email@example.com' is not 'Name <email>' and matches no existing author

--amend 명령어가 실행되면 commit 메세지를 수정할 수 있는 화면이 표시된다. :wq로 저장 후 종료하면 commit의 author와 메세지가 정상적으로 변경되었음을 확인할 수 있다.

참고: author 와 committer를 모두 바꾸려면

$ git log --pretty='[%cn]%ce: %cd' // commiter 확인 (cn: committer name, ce: committer email, cd: commit date)
$ git -c user.name="username" -c user.email=email@example.com commit --amend --reset-author

특정 commit의 author 수정하기

rebase를 사용한다. A -> B -> C -> D(Head) 순서로 commit이 작성되어 있을 때, C commit의 author를 수정하고 싶다면 A 혹은 B commit의 key를 알아야한다. 즉 수정하려는 commit보다 이전에 작성된 commit을 base로 다시(re) 설정, rebase 하는 것이다.

∙ 주의사항

rebase가 종료되면 수정한 commit 이후의 모든 commit들의 key 값 또한 변경된다. history가 일치하지 않는 문제가 생기므로 remote에 push하려면 $ git push --force 혹은 $ git push --force-with-lease를 사용해야 한다. 공동작업자가 있는 경우 기존 작업물을 삭제하고 새로 clone해야 하는 등의 여러 불편함이 생기므로 commit의 변경은 되도록이면 하지 않는 편이 좋다.

∙ 방법

$ git log // 바꾸려는 commit 이전에 작성된 commit의 key를 확인
$ git rebase --interactive [key] // git rebase -i [key]도 가능

rebase 명령어를 실행하면 key로 사용한 commit 이후의 모든 commit(merge commit 제외)들이 차례로 표시된다. 그 중 수정을 원하는 commit의 pickedit으로 변경한다.

edit 6ca128b commit message1
pick 210dd87 commit message2
pick 730fcc1 commit message3
edit 9de4e16 commit message4

...

:wq로 저장 후 종료하고 아래 명령어를 통해 해당 commit의 author를 변경한다.

$ git commit --amend --author="username <eamil@example.com>"

이 때 이메일을 < >로 감싸지 않으면 다음과 같은 에러 메세지가 뜬다.

fatal: --author 'username email@example.com' is not 'Name <email>' and matches no existing author

--amend 명령어가 실행되면 commit 메세지를 수정할 수 있는 화면이 표시된다. :wq로 저장 후 종료하면 commit의 author와 메세지가 정상적으로 변경되었음을 확인할 수 있다.

$ git rebase --continue

이후 --continue 명령어를 실행하면 edit으로 설정한 다음 commit으로 이동하고, 위와 동일한 방법으로 author를 수정하면 된다. 모든 변경을 끝마친 뒤 --continue 명령어를 실행하면 rebase가 종료된다.

참고: initial commit의 author를 수정하고 싶다면

$ git rebase --interactive --root

참고: rebase 사용 시 merge commit을 유지하고 싶다면

$ git rebase --interactive --rebase-merges [key]

참고: author 와 committer를 모두 바꾸려면

$ git log --pretty='[%cn]%ce: %cd' // commiter 확인 (cn: committer name, ce: committer email, cd: commit date)
$ git -c user.name="username" -c user.email=email@example.com commit --amend --reset-author

모든 commit의 author와 committer 변경하기

shell script 로 해결하자. 프로젝트 루트에 ch.sh라고 파일을 만들겠다.

#!/bin/sh
git filter-branch -f --env-filter '

OLD_EMAIL="your@old_email.com"
CORRECT_NAME="your name"
CORRECT_EMAIL="your@new_email.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

실행은

$ chomod +x ch.sh
$ ./ch.sh

or

$ sh ./ch.sh 

모든 commit의 author와 committer를 변경하고 backup repo에 푸시까지 하자

remote backup repo 는 미리 등록한 뒤 실행할 것.

#!/bin/sh
git branch backup
git checkout backup
git filter-branch -f --env-filter '

OLD_EMAIL="your_old@email.com"
CORRECT_NAME="your name"
CORRECT_EMAIL="your_new@email.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

git push -f backup backup:master
git checkout master
git branch -D backup

제외하고 싶은 파일이 있는데 .gitignore를 건드리고 싶지 않다면?

.git/info/exclude파일을 수정한다.

Clone this wiki locally