Development Tip

커밋되지 않은 로컬 변경 사항을 다른 Git 브랜치에 병합하려면 어떻게해야합니까?

yourdevel 2020. 10. 2. 23:22
반응형

커밋되지 않은 로컬 변경 사항을 다른 Git 브랜치에 병합하려면 어떻게해야합니까?


Git에서 다음을 어떻게 할 수 있습니까?

내 현재 브랜치는 branch1이고 일부 로컬 변경을 수행했습니다. 그러나 이제 실제로 이러한 변경 사항을 branch2에 적용해야한다는 것을 깨달았습니다. 이러한 변경 사항을 적용 / 병합하여 branch1에서 커밋하지 않고 branch2에서 로컬 변경 사항이되도록하는 방법이 있습니까?


파일이 아직 커밋되지 않았으므로 branch1:

git stash
git checkout branch2
git stash pop

또는

git stash
git checkout branch2
git stash list       # to check the various stash made in different branch
git stash apply x    # to select the right one

주석으로 하여 benjohn (참조 git stash매뉴얼 페이지 )

현재 추적되지 않은 (새로 추가 된) 파일도 숨기려면 인수를 추가하십시오 -u.

git stash -u

은닉, 임시 커밋 및 리베이스는 모두 과도 할 수 있습니다. 아직 변경된 파일을 색인에 추가하지 않은 경우 다른 분기 만 체크 아웃 할 수 있습니다.

git checkout branch2

편집중인 파일이 branch1과 branch2간에 다르지 않는 한 작동합니다. 작업 변경 사항이 유지 된 상태로 branch2에있게됩니다. 서로 다른 경우 -m체크 아웃 옵션으로 분기를 전환하여 도입 된 변경 사항과 로컬 변경 사항을 병합하도록 지정할 수 있습니다 .

git checkout -m branch2

색인에 변경 사항을 추가 한 경우 먼저 재설정을 통해 이러한 변경 사항을 실행 취소 할 수 있습니다. (이렇게하면 작업 복사본이 보존되고 단계적 변경 사항 만 제거됩니다.)

git reset

이전에 언급 한 숨김 접근 방식에 대한 더 짧은 대안은 다음과 같습니다.

일시적으로 변경 사항을 숨김으로 이동합니다.

  1. git stash

새 브랜치를 생성하고 전환 한 다음 한 단계 만에 해당 브랜치를 팝합니다.

  1. git stash branch new_branch_name

그럼 그냥 add하고 commit새로운 브랜치로 변경됩니다.


경고 : git 초보자에게는 적합하지 않습니다.

이것은 내 워크 플로에서 충분히 나타나서 새 git 명령을 작성하려고 거의 시도했습니다. 평소의 git stash흐름은가는 길이 지만 조금 어색합니다. 나는 일반적으로 변경 사항을 살펴보면 모든 정보가 내 마음에 새롭고git commit 내가 찾은 것을 시작하는 것이 더 낫기 때문에 먼저 새 커밋을 만듭니다 (보통 작업하는 동안 발견 한 마스터에 속하는 버그 수정 기능 브랜치) 바로.

이와 같은 상황이 많이 발생 하는 경우 항상 브랜치가 체크 아웃 된 현재 작업 디렉토리 와 함께 다른 작업 디렉토리 를 갖는 것도 도움이 master됩니다.

그래서 이것을 달성하는 방법은 다음과 같습니다.

  1. git commit 좋은 커밋 메시지로 즉시 변경됩니다.
  2. git reset HEAD~1 현재 분기에서 커밋을 취소합니다.
  3. (선택 사항) 기능에 대한 작업을 계속합니다.

때때로 나중에 (비동기 적으로) 또는 다른 터미널 창에서 즉시 :

  1. cd my-project-master 같은 것을 공유하는 또 다른 WD입니다 .git
  2. git reflog 방금 만든 버그 수정을 찾습니다.
  3. git cherry-pick SHA1 커밋의.

선택적으로 (여전히 비동기식) 기능 분기를 리베이스 (또는 병합)하여 버그 수정을 얻을 수 있습니다. 일반적으로 PR을 제출하려고하고 기능 분기와 WD를 이미 정리 한 경우 :

  1. cd my-project 제가 작업하고있는 주요 WD입니다.
  2. git rebase master 버그 수정을 얻으려면.

내가 중단 기능에 작업을 계속 할 수 없으며이 방법에 대해 걱정할 필요가 git stash아무것도 -ing 또는 전에 내 WD을 청소하는 데 git checkout에 간다 내 모든 버그 수정이 여전히 (. 후 다시 검사에게 기능 지점 철회를 가짐)와 master대신 내 기능 브랜치에 숨겨져 있습니다.

IMO git stash and git checkout is a real PIA when you are in the middle of working on some big feature.


If it were about committed changes, you should have a look at git-rebase, but as pointed out in comment by VonC, as you're talking about local changes, git-stash would certainly be the good way to do this.


The answers given so far are not ideal because they require a lot of needless work resolving merge conflicts, or they make too many assumptions which are frequently false. This is how to do it perfectly. The link is to my own site.

How to Commit to a Different Branch in git

You have uncommited changes on my_branch that you want to commit to master, without committing all the changes from my_branch.

Example

git merge master
git stash -u
git checkout master
git stash apply
git reset
git add example.js
git commit
git checkout .
git clean -f -d
git checkout my_branch
git merge master
git stash pop

Explanation

Start by merging master into your branch, since you'll have to do that eventually anyway, and now is the best time to resolve any conflicts.

The -u option (aka --include-untracked) in git stash -u prevents you from losing untracked files when you later do git clean -f -d within master.

After git checkout master it is important that you do NOT git stash pop, because you will need this stash later. If you pop the stash created in my_branch and then do git stash in master, you will cause needless merge conflicts when you later apply that stash in my_branch.

git reset unstages everything resulting from git stash apply. For example, files that have been modified in the stash but do not exist in master get staged as "deleted by us" conflicts.

git checkout . and git clean -f -d discard everything that isn't committed: all changes to tracked files, and all untracked files and directories. They are already saved in the stash and if left in master would cause needless merge conflicts when switching back to my_branch.

The last git stash pop will be based on the original my_branch, and so will not cause any merge conflicts. However, if your stash contains untracked files which you have committed to master, git will complain that it "Could not restore untracked files from stash". To resolve this conflict, delete those files from your working tree, then git stash pop, git add ., and git reset.

참고URL : https://stackoverflow.com/questions/556923/how-do-i-merge-my-local-uncommitted-changes-into-another-git-branch

반응형