IT박스

로컬 git 커밋 중 일부만 어떻게 푸시합니까?

itboxs 2020. 6. 21. 20:28
반응형

로컬 git 커밋 중 일부만 어떻게 푸시합니까?


5 개의 로컬 커밋이 있다고 가정합니다. SVN 스타일 워크 플로를 사용하여 두 개만 중앙 저장소에 푸시하고 싶습니다. 어떻게해야합니까?

이것은 작동하지 않았다 :

git checkout HEAD~3  #set head to three commits ago
git push #attempt push from that head

결과적으로 5 개의 로컬 커밋이 모두 완료됩니다.

실제로 커밋을 취소하기 위해 git reset을 수행하고 git stash와 git push를 수행 할 수 있다고 생각합니다. 그러나 커밋 메시지를 작성하고 파일을 정리했으며 다시 실행하고 싶지 않습니다.

내 느낌은 밀거나 재설정하기 위해 전달 된 일부 플래그가 작동한다는 것입니다.

도움이된다면 여기 내 자식 설정이 있습니다.

[ramanujan:~/myrepo/.git]$cat config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://server/git/myrepo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

커밋이 마스터 브랜치에 있고 원격 마스터 브랜치로 푸시한다고 가정합니다.

$ git push origin master~3:master

git-svn을 사용하는 경우 :

$ git svn dcommit master~3

git-svn의 경우 커밋을 기대하고 있기 때문에 HEAD ~ 3을 사용할 수도 있습니다. 스트레이트 git의 경우 HEAD가 참조 사양에서 올바르게 평가되지 않으므로 분기 이름을 사용해야합니다.

더 긴 접근 방식을 취할 수도 있습니다.

$ git checkout -b tocommit HEAD~3
$ git push origin tocommit:master

이러한 유형의 작업 흐름을 습관화하는 경우 별도의 지점에서 작업을 수행하는 것을 고려해야합니다. 그런 다음 다음과 같은 작업을 수행 할 수 있습니다.

$ git checkout master
$ git merge working~3
$ git push origin master:master

"origin master : master"부분은 설정에서 선택 사항 일 수 있습니다.


내가하는 일은 "work"라는 로컬 지점에서 일하는 것입니다. 이 분기에는 업스트림 리포지토리로 푸시하지 않을 모든 임시 커밋 (해결 방법 또는 개인 빌드 옵션 등)이 포함됩니다. 나는 내가하는 적절한 커밋 체리 - 선택, 마스터 분기로 전환 커밋 할 때 그때, 그 지점에 떨어져 작동 다음 주인을 밀어 커밋합니다.

업스트림에서 마스터 브랜치로 변경 사항을 가져온 후 I git checkout workgit rebase master. 그러면 내 로컬 변경 사항이 기록이 끝날 때까지 다시 작성됩니다.

실제로이 git svn워크 플로와 함께 사용 하고 있으므로 "푸시"작업에 관련이 git svn dcommit있습니다. 또한 tig훌륭한 텍스트 모드 GUI 저장소 뷰어 인 마스터를 사용하여 적절한 커밋을 선택합니다.


기본적으로 git-push는 모든 분기를 푸시합니다. 이렇게하면 :

 git checkout HEAD~3  #set head to three commits ago
 git push #attempt push from that head

분리 된 HEAD (지점에 있지 않음)로 이동 한 다음 로컬 마스터 (아직 있던 위치)를 포함하여 모든 분기를 원격 마스터로 푸시합니다.

수동 솔루션은 다음과 같습니다.

 git push origin HEAD:master

혼란스럽고 위험한 모든 브랜치를 푸시하는 기본 동작을 찾으면 ~ / .gitconfig에 추가하십시오.

 [remote.origin]
    push = HEAD

그런 다음 현재 지점 만 푸시됩니다. 귀하의 예 (분리 된 헤드)에서 실수로 잘못된 커밋을 푸시하는 대신이 오류 메시지가 나타납니다.

 error: unable to push to unqualified destination: HEAD

짧은 답변:

git push <latest commit SHA1 until you want commits to be pushed>

예 :

git push fc47b2

git push HEAD~2

긴 대답 :

Commits are linked together as a chain with a parent/child mechanism. Thus, pushing a commit actually also pushes all parent commits to this commit that where not known to the remote. This is implicitly done when you git push the current commit: all the previous commits are also pushed because this command is equivalent to git push HEAD.

So the question might be rewritten into How to push a specific commit and this specific commit might be HEAD~2, for example.

If the commits you want to push are non-consecutive, simply re-order them with a git rebase -i before the specific push.


1) Use "git rebase" to reorder your commits, if you want to.

git rebase -i

This command will display something like this in your editor ( I am using vim )

pick 4791291 commitA
pick a2bdfbd commitB
pick c3d4961 commitC
pick aa1cefc commitD
pick 9781434 commitE

# Rebase ..............
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out




^G Get Help         ^O WriteOut         ^R Read File        ^Y Prev Page                ^K Cut Text         ^C Cur Pos
^X Exit             ^J Justify          ^W Where Is         ^V Next Page            ^U UnCut Text       ^T To Spell

2) Reorder your your commits according to your choice by simple cut paste. Suppose the new order is

pick 9781434 commitE

pick c3d4961 commitC

pick 4791291 commitA

pick aa1cefc commitD

pick a2bdfbd commitB

Make these changes in your editor and press ctrl+ O (writeOut)

Or you can also use

git rebase -i HEAD~<commitNumber>

You can check the new sequence with

git log

3) Now use

git push <remoteName> <commit SHA>:<remoteBranchName>

If only one branch at remote(origin) and one at local(master), just use

git push <commit SHA>
git push aa1cefc

This will push commitB and commitD.

참고URL : https://stackoverflow.com/questions/604399/how-do-you-push-only-some-of-your-local-git-commits

반응형