-
Notifications
You must be signed in to change notification settings - Fork 0
git pro tips
Git Notes for Professionals에도 팁이 많으니 참고.
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
$ ssh-keygen
으로 rsa 키를 생성한 뒤(참고), public 키를 github > settings > SSH keys 에 등록해준다.
다른 방법: 참고 링크
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와 메세지가 정상적으로 변경되었음을 확인할 수 있다.
$ 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
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의 pick을 edit으로 변경한다.
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가 종료된다.
$ git rebase --interactive --root
$ git rebase --interactive --rebase-merges [key]
$ 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
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
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
.git/info/exclude파일을 수정한다. 작성 방법은 .gitignore 와 같다.
example:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
exclude-file-name
git update-index --assume-unchanged <파일 경로>
$ ssh-keygen -t rsa -b 4096 -C "sample@example.com"
파일명은 무엇으로 해도 되지만 각 계정마다 다르게 해줘야 한다. 편의상 id_rsa_work등으로 알아보기 쉽게 만들자.
생성한 키의 권한은 600으로 적용해준다.
chmod 600 id_rsa_yourid
chmod 600 id_rsa_yourid.pub
2. ssh public key 를 각각의 github 계정에 등록 (공식문서)
각각의 계정에서 SSH and GPG Keys메뉴에 ssh public key를 등록한다
pbcopy < id_rsa_work.pub
~/.ssh/config파일에 (없으면 생성해서) 자신의 계정을 등록해준다.
# Personal git
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# Work git
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
그리고 잘 동작하는지 테스트
$ ssh -T git@github.com-personal
$ ssh -T git@github.com-work
리턴으로 자신의 아이디가 나온다면 성공이다.
~/Workspace/Company 디랙토리 안에서는 다른 .gitconfig 파일을 사용할 수 있는 예제이다.
vi ~/.gitconfig
[user]
name = Your Name
email = personal@email.com
[includeIf "gitdir:~/Workspace/Company/"]
path = ~/Workspace/Company/.gitconfig
vi ~/Workspace/Company/.gitconfig
[user]
email = my-id@company.com
이제 필요한 repo 를 클론하자. 이때 SSH를 사용하며, origin url 이 다름에 유의한다. example:
git clone git@github.com-work:myCompany/myProject.git
기본적으로 깃 유저의 정보는 아래 명령으로 바꿀 수 있다.
git config user.email <your-email>
git config user.name <your-name>
하지만 git-user-switch 를 이용하면 편리하다.
npm i -g git-user-switch
현재 사용하고 있는 branch 로 remote branch 를 pull 해온다.
remote 에 어떤 브런치들이 있는지 확인
git remote show origin
현재 사용되고 있는 branch 확인
git branch -v
원한다면 브런치를 바꾸거나 생성한다
git checkout target-branch
git checkout -b target-branch
그리고 pull
git pull origin <branch name>