Git에서 로컬 분기를 원격 분기로 완전히 바꾸는 방법은 무엇입니까?
두 가지 지점이 있습니다.
- 지역 지점 (내가 함께 일하는 지점)
- 원격 분기 (공개, 잘 테스트 된 커밋 만 해당 위치에 있음)
최근에 나는 지역 지부를 심각하게 엉망으로 만들었습니다.
로컬 브랜치를 완전히 원격 브랜치로 대체하려면 어떻게해야 원격 브랜치가있는 곳에서 작업을 계속할 수 있습니까?
나는 이미 SO를 검색했으며 로컬에서 원격 지점으로 체크 아웃해도 효과가 없습니다.
- (Zoltán의 의견에서 ) 교체하려는 분기를 확인했는지 확인하십시오 .
마스터가 교체 할 로컬 브랜치이고 "origin / master"가 재설정하려는 원격 브랜치라고 가정합니다.
git reset --hard origin/master
이렇게하면 로컬 HEAD 분기가 오리진 / 마스터와 동일한 개정으로 업데이트되고이 --hard
변경 사항이 인덱스 및 작업 공간에도 동기화됩니다.
세 단계만큼 쉽습니다.
- 로컬 브랜치를 삭제합니다.
git branch -d local_branch
- 최신 원격 브랜치를 가져옵니다.
git fetch origin remote_branch
- 원격 분기를 기반으로 로컬 분기를 다시 빌드하십시오.
git checkout -b local_branch origin/remote_branch
git branch -D <branch-name>
git fetch <remote> <branch-name>
git checkout -b <branch-name> --track <remote>/<branch-name>
모든 것을 원격 분기로 교체하십시오. 그러나 동일한 커밋에서만 로컬 브랜치가 있습니다.
git reset --hard origin/some-branch
또는 원격 지점에서 최신 정보 를 가져 와서 모든 것을 교체하십시오.
git fetch origin some-branch
git reset --hard FETCH_HEAD
따로, 필요한 경우 아직 커밋하지 않은 추적되지 않은 파일 및 디렉터리를 지울 수 있습니다.
git clean -fd
현재 로컬 분기를 원격으로 대체하는 가장 안전하고 완전한 방법 :
git stash
git merge --abort
git rebase --abort
git branch -M yourBranch replaced_yourBranch
git fetch origin yourBranch:yourBranch
git checkout yourBranch
이 stash
행은 커밋하지 않은 변경 사항을 저장합니다. 이 branch
줄은 분기를 다른 이름으로 이동하여 원래 이름을 해제합니다. fetch
라인은 원격의 최신 복사본을 검색합니다. 이 checkout
선은 원래 분기를 추적 분기로 다시 만듭니다.
또는 bash 함수로 :
replaceWithRemote() {
yourBranch=${1:-`git rev-parse --abbrev-ref HEAD`}
git stash
git merge --abort
git rebase --abort
git branch -M ${yourBranch} replaced_${yourBranch}_`git rev-parse --short HEAD`
git fetch origin ${yourBranch}:${yourBranch}
git checkout ${yourBranch}
}
현재 분기의 이름을 replace_master_98d258f와 같은 이름으로 바꿉니다.
@Laurent의 @Hugo가 말한 것처럼 할 수 있습니다. 또는 git rebase
어떤 것을 알고 있다면 제거하려는 커밋을 삭제하는 데 사용할 수 있습니다. 저는 git rebase -i head~N
이런 종류의 작업 에 사용하는 경향이 있습니다 (여기서 N은 숫자이므로 마지막 N 커밋을 조작 할 수 있음).
선택한 대답은 절대적으로 올바른 그러나 그것은 / 커밋 최신으로 밀어 나를 떠나지 않았다, ...
그래서 나를 위해 :
git reset --hard dev/jobmanager-tools
git pull ( did not work as git was not sure what branch i wanted)
Since I know I want to temporarily set my upstream branch for a few weeks to a specific branch ( same as the one i switched to / checked out earlier and did a hard reset on )
So AFTER reset
git branch --set-upstream-to=origin/dev/jobmanager-tools
git pull
git status ( says--> on branch dev/jobmanager-tools
If you want to update branch that is not currently checked out you can do
git fetch -f origin rbranch:lbranch
It can be done multiple ways, continuing to edit this answer for spreading better knowledge perspective.
1) Reset hard
If you are working from remote develop branch, you can reset HEAD to the last commit on remote branch as below:
git reset --hard origin/develop
2) Delete current branch, and checkout again from the remote repository
Considering, you are working on develop branch in local repo, that syncs with remote/develop branch, you can do as below:
git branch -D develop
git checkout -b develop origin/develop
3) Abort Merge
If you are in-between a bad merge (mistakenly done with wrong branch), and wanted to avoid the merge to go back to the branch latest as below:
git merge --abort
4) Abort Rebase
If you are in-between a bad rebase, you can abort the rebase request as below:
git rebase --abort
As provided in chosen explanation, git reset is good. But nowadays we often use sub-modules: repositories inside repositories. For example, you if you use ZF3 and jQuery in your project you most probably want them to be cloned from their original repositories. In such case git reset is not enough. We need to update submodules to that exact version that are defined in our repository:
git checkout master
git fetch origin master
git reset --hard origin/master
git pull
git submodule foreach git submodule update
git status
it is the same as you will come (cd) recursively to the working directory of each sub-module and will run:
git submodule update
And it's very different from
git checkout master
git pull
because sub-modules point not to branch but to the commit.
In that cases when you manually checkout some branch for 1 or more submodules you can run
git submodule foreach git pull
git reset --hard
git clean -fd
This worked for me - clean showed all the files it deleted too. If it tells you you'll lose changes, you need to stash.
The ugly but simpler way: delete your local folder, and clone the remote repository again.
'code' 카테고리의 다른 글
Python 2.X에서 범위와 xrange 함수의 차이점은 무엇입니까? (0) | 2020.09.30 |
---|---|
IEnumerable의 foreach에 해당하는 LINQ (0) | 2020.09.30 |
jQuery를 사용하여 Bootstrap 모달 창을 여는 방법은 무엇입니까? (0) | 2020.09.30 |
Git의 특정 개정판에서 단일 파일을 검색하는 방법 (0) | 2020.09.29 |
선택한 요소의 외부 HTML 가져 오기 (0) | 2020.09.29 |