git에서 커밋 한 후 자동으로 푸시하는 방법은 무엇입니까?
로컬 리포지토리에 커밋 할 때마다 자동으로 원격 리포지토리로 푸시하도록 git을 설정하려면 (내 암호를 자동으로 제공하는 포함) 어떻게해야합니까?
먼저 비밀번호를 입력하지 않고 수동으로 푸시 할 수 있는지 확인합니다. HTTP 또는 HTTPS를 통해 푸시하는 경우 로그인 세부 정보 가 포함 된 .netrc
파일을 만들 거나 원격 .NET의 URL에 사용자 이름과 비밀번호를 추가 하는 경우가 해당됩니다 . SSH를 사용하는 경우 비공개 키에 비밀번호가없는 키 쌍을 만들거나을 사용 ssh-agent
하여 비공개 키를 캐시 할 수 있습니다 .
그런 다음 다음을 포함 하는 실행 파일 ( chmod +x
)을 만들어야합니다 .git/hooks/post-commit
.
#!/bin/sh
git push origin master
... 이외의 원격지로 푸시 origin
하거나 이외 의 분기 를 푸시하려는 경우 해당 라인을 사용자 정의합니다 master
. 해당 파일을 실행 가능하게 만드십시오.
마스터 브랜치보다 더 많이 사용하기 시작하면 현재 브랜치를 자동으로 푸시 할 수 있습니다. 내 후크 ( .git/hooks/post-commit
)는 다음과 같습니다.
#!/usr/bin/env bash
branch_name=$(git symbolic-ref --short HEAD`)
retcode=$?
non_push_suffix="_local"
# Only push if branch_name was found (my be empty if in detached head state)
if [ $retcode -eq 0 ] ; then
#Only push if branch_name does not end with the non-push suffix
if [[ $branch_name != *$non_push_suffix ]] ; then
echo
echo "**** Pushing current branch $branch_name to origin [i4h post-commit hook]"
echo
git push origin $branch_name;
fi
fi
git symbolic-ref로 브랜치 이름을 결정할 수 있으면 현재 브랜치를 푸시합니다.
" Git에서 현재 브랜치 이름을 얻는 방법? "은 현재 브랜치 이름을 가져 오는 다른 방법을 다룹니다.
소시지 만들기 가 발생할 것으로 예상되는 작업 브랜치에서 작업 할 때 모든 브랜치에 대한 자동 푸시는 방해가 될 수 있습니다 (푸시 후 쉽게 리베이스 할 수 없음). 따라서 후크는 정의 된 접미사 (예 : "_local")로 끝나는 분기를 푸시하지 않습니다.
.git / hooks 디렉토리에 "git push"라는 내용으로 "post-commit"이라는 파일을 만듭니다. 자동으로 암호를 제공하려면 수정이 필요합니다.
이 git-autopush 스크립트를 사용하면 " 자동 푸시를 구성하는 방법 "에서 권장 한 것과 유사한 사후 커밋 후크를 설정할 수 있습니다 .
그러나 암호의 경우ssh-agent
.
다음은 Linux 및 Windows (git bash) 사용자를 위해 ssh를 통해 암호를 제공하지 않고 푸시 / 풀링하는 간단한 지침입니다.
클라이언트에서 :
ssh 키가 생성되었는지 확인하십시오.
$ ls ~/.ssh/id_rsa.pub; ls ~/.ssh/id_dsa.pub /c/Users/Cermo/.ssh/id_rsa.pub <-- I have RSA key ls: cannot access '/c/Users/Cermo/.ssh/id_dsa.pub': No such file or directory
키가없는 경우 ( "ls : 액세스 할 수 없음 ..."줄 2 개) 새 키를 생성합니다. 키가있는 경우이 단계를 건너 뜁니다.
$ ssh-keygen.exe Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Cermo/.ssh/id_rsa): Enter passphrase (empty for no passphrase): <-- press Enter Enter same passphrase again: <-- press Enter
git을 사용하여 가져 오거나 푸시하려는 원격 서버에 키를 복사합니다.
$ ssh-copy-id user_name@server_name /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys user_name@server_name's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'user_name@server_name'" and check to make sure that only the key(s) you wanted were added.
참고 :이 작업 중에 암호를 제공해야합니다. 그 후에는 풀 / 푸시 작업이 암호를 요청하지 않습니다.
참고 2 :이 절차를 사용하기 전에 최소한 한 번 user_name을 사용하여 서버에 로그인해야합니다 (처음 로그인 할 때 ssh 키가 복사되는 홈 디렉터리가 생성됨).
다음은 git push
이 원격 저장소에 자동 으로 보내는 bash 스크립트입니다.
- ssh-agent 자동 확인
- Automatically send passphrase using expect script
- Usage is simply:
$ cd /path/to/your/repository
then$ push
Put this script to a file for example $HOME/.ssh/push
#!/bin/bash
# Check connection
ssh-add -l &>/dev/null
[[ "$?" == 2 ]] && eval `ssh-agent` > /dev/null
# Check if git config is configured
if [ ! $(git config user.name) ]
then
git config --global user.name <user_name>
git config --global user.email <user_email>
fi
# Check if expect is installed
if [[ ! $(dpkg -l | grep expect) ]]
then
apt-get update > /dev/null
apt-get install --assume-yes --no-install-recommends apt-utils expect > /dev/null
fi
# Check identity
ssh-add -l &>/dev/null
[[ "$?" == 1 ]] && expect $HOME/.ssh/agent > /dev/null
# Clean and push repo
REMOTE=$(git remote get-url origin)
URL=git@github.com:${REMOTE##*github.com/}
[[ $REMOTE == "http"* ]] && git remote set-url origin $URL
git add . && git commit -m "test automatically push to a remote repo"
git status && git push origin $(git rev-parse --abbrev-ref HEAD) --force
Link it to /bin
directory so it can be called by just $ push
command
$ sudo ln -s $HOME/.ssh/push /bin/push
$ chmod +x /bin/push
참고URL : https://stackoverflow.com/questions/7925850/how-to-automatically-push-after-committing-in-git
'Development Tip' 카테고리의 다른 글
파이썬에서 정수를 반올림하는 방법 (0) | 2020.11.03 |
---|---|
@ Html.DropDownList ()를 사용하여 선택할 CSS 클래스 추가 (0) | 2020.11.03 |
Angularjs 필터가 null이 아닙니다. (0) | 2020.11.03 |
npm 구성을 기본값으로 복원 / 재설정하는 방법은 무엇입니까? (0) | 2020.11.03 |
C에서 OO 스타일 다형성을 어떻게 시뮬레이션 할 수 있습니까? (0) | 2020.11.03 |