Programing

Git은 GitHub에서 특정 분기를 가져옵니다.

lottogame 2020. 10. 4. 10:14
반응형

Git은 GitHub에서 특정 분기를 가져옵니다.


여러 지점이있는 프로젝트가 있습니다. 나는 그들을 GitHub 로 밀고 있었고 , 이제 다른 누군가가 프로젝트를 진행하고 있으므로 GitHub에서 브랜치를 가져와야합니다. 마스터에서 잘 작동합니다. 그러나 누군가가 지점을 만들었다 고하자 xyz. xyzGitHub에서 브랜치 를 가져와 xyz브랜치 병합하려면 localhost어떻게해야합니까?

나는 실제로 여기에 내 대답이 있습니다 : Git에서 분기를 밀고 당기십시오 .

하지만 "! [거부 됨]"오류와 "빨리 감기가 아님"에 대한 내용이 표시됩니다.

어떤 제안?


하지만 "! [거부 됨]"오류와 "빨리 감기가 아님"에 대한 내용이 표시됩니다.

Git이 브랜치의 변경 사항을 현재 마스터로 병합 할 수 없기 때문입니다. branch를 체크 아웃 master했고 원격 branch에 병합하려고 한다고 가정 해 보겠습니다 other-branch. 이렇게하면 :

$ git pull origin other-branch

Git은 기본적으로 다음을 수행합니다.

$ git fetch origin other-branch && git merge other-branch

즉, a pull는 a fetch다음에 merge. 그러나 pull-ing을 사용하면 Git은 빨리 감기 병합을 수행 할 수있는 경우 에만 병합 other-branch 됩니다 . 빨리 감기 병합은에 병합하려고하는 지점의 머리가되는 병합입니다 직접 하위 병합 할 지점의 머리. 예를 들어,이 히스토리 트리가있는 경우 병합 하면 빨리 감기 병합이 발생합니다.other-branch

O-O-O-O-O-O
^         ^
master    other-branch

그러나 이것은 빨리 감기 병합 아닙니다 .

    v master
O-O-O
\
 \-O-O-O-O
         ^ other-branch

문제를 해결하려면 먼저 원격 분기를 가져옵니다 .

$ git fetch origin other-branch

그런 다음 현재 브랜치에 병합하고 ( master) 병합 충돌을 수정합니다.

$ git merge origin/other-branch
# Fix merge conflicts, if they occur
# Add merge conflict fixes
$ git commit    # And commit the merge!

원격 브랜치를 명시 적으로 추적하기 만하면 git pull원하는대로 간단 하게 수행 할 수 있습니다.

git branch -f remote_branch_name origin/remote_branch_name
git checkout remote_branch_name

후자는 로컬 작업입니다.

또는 분기에 대한 GitHub 문서에 더 적합합니다 .

git branch -f new_local_branch_name upstream/remote_branch_name

다음 명령을 사용하여 분기를 분기로 가져올 수 있습니다.

git pull {repo} {remotebranchname}:{localbranchname}

git pull origin xyz:xyz

마스터 브랜치에있을 때 다음과 같은 브랜치를 먼저 체크 아웃 할 수도 있습니다.

git checkout -b xyz

이렇게하면 마스터에서 새 분기 "xyz"가 생성되고 직접 체크 아웃됩니다.

그런 다음 다음을 수행합니다.

git pull origin xyz

이렇게하면 새 브랜치를 로컬 xyz브랜치 로 가져옵니다 .


가장 좋은 방법은 다음과 같습니다.

git checkout -b <new_branch> <remote repo name>/<new_branch>

git fetch will grab the latest list of branches.

Now you can git checkout MyNewBranch

Done :)


For more info see docs: git fetch


I am not sure I fully understand the problem, but pulling an existing branch is done like this (at least it works for me :)

git pull origin BRANCH

This is assuming that your local branch is created off of the origin/BRANCH.


This helped me to get remote branch before merging it into other:

git fetch repo xyz:xyz
git checkout xyz

git pull <gitreponame> <branchname>

Usually if you have only repo assigned to your code then the gitreponame would be origin.

If you are working on two repo's like one is local and another one for remote like you can check repo's list from git remote -v. this shows how many repo's are assigned to your current code.

BranchName should exists into corresponding gitreponame.

you can use following two commands to add or remove repo's

git remote add <gitreponame> <repourl>
git remote remove <gitreponame>

Simply put, If you want to pull from GitHub the branch the_branch_I_want:

git fetch origin
git branch -f the_branch_I_want origin/the_branch_I_want
git checkout the_branch_I_want

you may also do

git pull -r origin master

fix merge conflicts if any

git rebase --continue

-r is for rebase. This will make you branch structure from

        v  master       
o-o-o-o-o
     \o-o-o
          ^ other branch

to

        v  master       
o-o-o-o-o-o-o-o
              ^ other branch

This will lead to a cleaner history. Note: In case you have already pushed your other-branch to origin( or any other remote), you may have to force push your branch after rebase.

git push -f origin other-branch

I did

git branch -f new_local_branch_name origin/remote_branch_name

Instead of

git branch -f new_local_branch_name upstream/remote_branch_name

As suggested by @innaM. When I used the upstream version, it said 'fatal: Not a valid object name: 'upstream/remote_branch_name''. I did not do git fetch origin as a comment suggested, but instead simply replaced upstream with origin. I guess they are equivalent.

참고URL : https://stackoverflow.com/questions/1709177/git-pull-a-certain-branch-from-github

반응형