커밋되지 않은 로컬 변경을 다른 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
git stash
git stash branch branch2
용도:
git stash branch <branchname> [<stash>]
- 를 만들고 아웃합니다.
<branchname>
하다.<stash>
창시된 작품입니다.- 는 에 합니다.
<stash>
새로운 작업 트리와 인덱스로 이동합니다.이 성공하면, 「」는 「」를 참조해당됩니다.
<stash>
입니다.stash@{<revision>}
후, 「」, 「」를 합니다<stash>
.은, 「」를 실행한 브런치인 경우에 합니다.
git stash push
git stash apply
이치노
는 stashgit stash
실행되면 충돌 없이 원래 저장된 상태로 돌아갑니다.
현재 되지 않은 추가된)합니다.
-u
: (으) :
git stash -u
저장, 임시 커밋 및 기본 재배치 모두 과잉 살상일 수 있습니다.변경된 파일을 색인에 아직 추가하지 않은 경우 다른 분기를 체크 아웃할 수 있습니다.
git checkout branch2
편집 중인 파일이 branch1과 branch2 사이에 차이가 없는 한 이 기능은 작동합니다.브런치2할 수 .-m
체크 아웃 옵션을 선택합니다.
git checkout -m branch2
인덱스에 변경 사항을 추가한 경우 먼저 다시 설정하여 변경 사항을 실행 취소해야 합니다. 이렇게 하면 작업 복사본이 보존되고 단계별 변경 사항만 제거됩니다.
git reset
수용된 답변에 대한 짧은 대안은 다음과 같습니다.
변경 내용을 일시적으로 저장소로 이동합니다.
git stash
새 분기를 만들고 전환한 다음 한 번에 분기에 저장 공간을 팝합니다.
git stash branch new_branch_name
그냥 ★★★★★★★★★★★★★★★.add
★★★★★★★★★★★★★★★★★」commit
이 새로운 분기의 변경.
경고: 신입사원에게는 해당되지 않습니다.
이것은 제 워크플로우에 충분히 나타나기 때문에 저는 거의 새로운 git 명령어를 쓰려고 했습니다.의 「 」git stash
흐름은 가는 길이지만 조금 어색합니다.저는 보통 새로운 약속을 먼저 합니다.변화를 보고 있으면 모든 정보가 머릿속에 생생하고 그냥 시작하는 것이 좋기 때문입니다.git commit
것브랜치 을 일 、 ( 을 - - ) 。
또, 이러한 상황에 자주 직면하는 경우, 현재의 디렉토리 옆에 항상 다음의 작업 디렉토리가 있는 것도 도움이 됩니다.
master
이치노
이를 실현하는 방법은 다음과 같습니다.
git commit
즉시 변경할 수 있습니다.git reset HEAD~1
현재 브랜치에서 커밋을 취소합니다.- (임의) 기능의 작업을 계속합니다.
나중에(비동기적으로) 또는 즉시 다른 터미널 창에서 다음 작업을 수행할 수 있습니다.
cd my-project-master
WD를 하는 다른 입니다..git
git reflog
가가 、 만든든만만만 정정봐 。git cherry-pick SHA1
커밋을 실시합니다.
옵션으로(비동기인 채로) 기능 브랜치를 리베이스(또는 Marge)하여 버그 수정을 취득할 수 있습니다.보통 PR을 송신하려고 하고 기능 브랜치 및 WD를 이미 청소하고 있는 경우입니다.
cd my-project
WD를 사용하다git rebase master
버그 수정이 필요합니다.
을 중단 없이 할 수 있고, 기능 할 수 , 기능 작업을 중단하지 않아도 .git stash
- 에 - WD를 .git checkout
그 후 버그 (버그 수정은 모두)로 .master
이치노
IMOgit stash
★★★★★★★★★★★★★★★★★」git checkout
PIA가 있다.
만약 그것이 커밋된 변경에 관한 것이라면, 당신은 git-rebase를 살펴봐야 하지만, VonC가 지적한 바와 같이, 당신이 로컬 변경에 대해 언급하고 있는 것처럼, git-stash는 분명히 이것을 하는 좋은 방법이 될 것이다.
다음은 제가 수행하는 단계입니다.
- git 복제 {link}
- cd {repo folder}
상태 및 현재 브랜치 상태를 확인할 수 있습니다.
- git 상태
- git 분기
- git 분기 -a
주의: 여기서 새로운 브랜치로 이동하기 전에 로컬 보고서를 변경해도 다음 절차는 계속 작동합니다.
"git branch"가 마스터를 나타내며 다른 분기로 작성+이동하려는 경우:
- git checkout - b {filename}
"git branch"를 사용하여 브랜치를 다시 확인합니다.이것으로, 새로운 브랜치에 있는 것이 표시됩니다.
이제 추가, 커밋 및 푸시:
- git add를 클릭합니다.
- git - m "새로운 브랜치 추가"
- git 푸시 오리진 {push name}
위의 절차는 새로운 지점으로 이동하기 전에 변경을 가한 경우와 새로운 지점으로 이동 후 변경을 가한 경우 모두 유효합니다.나는 그것이 비슷한 상황에 처한 사람들에게 도움이 되기를 바란다.
커밋되지 않은 변경이 추적되지 않은 변경과 추적되지 않은 변경의 혼합인 경우
추적되지 않은 변경은 무엇입니까?
VSCode를 표시합니다.예를 들어 VSCode는 VSCode를 표시합니다.U
파일 탐색기의 파일 옆에 있습니다.
추적되는 변경이란 무엇입니까?
이전(이전 커밋)에서 repo에 커밋된 파일을 변경하는 경우.
순서의 간단한 설명
여러분이 나뭇가지에 해 보세요.A
기존 만 브랜치에 .A
않음)은 브랜치에 B
·스테이킹은 몇 가지 트릭으로 사용할 수 있습니다.이치노
" "에 ".git/config
<고객명>.git
는 ★★★★★★★★★★★★★★★★★★★★★★★★.config
파일을 열면 다음과 같은 것을 볼 수 있습니다.
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/...
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
설정 파일을 다음과 같이 변경합니다.
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[alias]
stash-untracked = "!f() { \
git stash; \
git stash -u; \
git stash pop stash@{1}; \
}; f"
[remote "origin"]
url = https://github.com/...
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
이제 지점 A에 있는 동안 다음 명령을 사용할 수 있습니다.
git stash-untracked
VSCode와 같은 편집기를 사용하는 경우 새 파일이 사라졌음을 알 수 있습니다(현재 저장됨).
지점 A 스테이지에 있는 동안 기존 파일에 변경을 커밋합니다.
git add .
git commit -m "committing tracked changes to current branch"
새로 입니다.checkout -b
★★★★★★★★★★★★★★★★★★★★★)
git checkout -b newBranchName
「」를 사용하고 stash pop
저장된 변경 내용이 현재 분기에 추가됩니다.
git stash pop
남은 것은 새로운 지점 B에 변경을 스테이징하고 커밋하는 것 뿐입니다.
git add .
git commit -m "created new file"
다만, 그 스레드가 닫혀 코멘트를 할 수 없기 때문에, 그 답변에 문제가 있습니다.
★★★★★을 했을 때git checkout other_branch
했습니다.
error: pathspec 'other_branch' did not match any file(s) known to git
따라서 명령어를 적용하는 대신 문제를 해결하는 방법을 사용합니다.
git branch other_branch
git checkout other_branch
경고: 신입사원에게는 해당되지 않습니다.
Chakrit의 답변과 마찬가지로 저는 이런 상황에 자주 부딪힙니다.기능 브랜치 작업을 하다 버그를 발견하여 수정하고 싶습니다.하지만 문제는 바로는main
''가 ''my-feature
main
7 상git
가 생기기 .
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★그냥 어디에 두기만 하면 됩니다.$PATH
/usr/local/bin
★★★★★★★★★★★★★★★★★」/$HOME/.local/bin
또는 다른 것)을 사용하여 다음 작업을 수행할 수 있습니다.
사용.
# currently working on branch `my-feature`
$ git add some-file # use git add -p if you want only some changes
$ git commit-branch main --rebase -m 'Fixed some nasty bug in some-file'
그런 다음 몇 가지 진행률 메시지가 표시됩니다.
Committing your staged changes to branch 'main'.
+ git checkout --quiet HEAD~0
+ git commit --quiet -m 'Fixed some nasty bug in some-file'
++ git rev-parse HEAD
+ commit_hash=82513091473646a09d541893b8bd60a0f98b765d
+ git stash push --quiet
+ git checkout --quiet main
+ git cherry-pick --quiet 82513091473646a09d541893b8bd60a0f98b765d
[main 1c5d96e] Fixed some nasty bug in some-file
Date: Mon Feb 6 15:04:03 2023 +0100
1 file changed, 2 insertions(+)
+ git checkout --quiet my-feature
+ git rebase --quiet main
+ git stash pop --quiet
+ set +x
Success.
스크립트
가 있습니다.git-commit-branch
꼭 해 주세요.chmod +x
을 고나에 $PATH
스크립트는 github에도 있습니다.https://github.com/fritzw/git-utils개선점을 자유롭게 제안해 주십시오.
동작 방법은 다음과 같습니다.
- 분리된 HEAD 상태로 전환
- 단계적 변경으로 임시 커밋 생성
- 기타 모든 변경 내용 저장(추적되지 않은 파일 포함)
- 대상 브랜치로 전환
- 대상 브랜치의 임시 커밋을 선택합니다.
- 원래 분기로 전환
- [옵션] 원래 브랜치에 버그 수정을 포함시키기 위해 타깃 브랜치에 원래 브랜치를 기본 재배치합니다.
- 저장소에서 다른 모든 변경 사항 복원
어떤 명령어가 실패했을 경우, 그 명령어는 여기서 정지하고 상황을 회복하기 위한 정보를 출력합니다.자세한 내용은 스크립트 끝에 있는 git 명령어 순서와 코멘트를 참조하십시오.
#!/usr/bin/env bash
set -o errexit
set -o nounset
usage() {
echo "Usage: git commit-branch <target-branch> [--rebase|-r] [ <git-commit-options>... ]"
echo ""
echo "Commits your staged changes to <target-branch>, discarding them from your current branch."
echo "Use --rebase or -r to rebase your current branch on the new commit in <target-branch>,"
echo "and thus include the changes in your current branch as well."
echo ""
echo "Example usage working on branch my-feature:"
echo " git add some-file"
echo " git commit-branch main --rebase -m 'Fixed a bug in some-file'"
}
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
target_branch="$1"; shift # Remove first argument
if ! git rev-parse --verify "$target_branch" >/dev/null; then
echo "fatal: '$target_branch' is not a branch in this git repository."
usage
exit 1
fi
rebase_command=''
if [[ $# -gt 0 ]] && [[ "$1" == "-r" || "$1" == "--rebase" ]]; then
rebase_command="git rebase --quiet $target_branch"
shift # Remove -r/--rebase argument
fi
current_branch="$(git branch --show-current)"
if ! [[ "$current_branch" ]]; then
echo "fatal: Unable to determine current branch. You must be on a branch to use git commit-branch."
exit 1
fi
commit_hash='not-committed-yet'
print_error_message() {
set +x
echo
echo "Something went wrong in the last command. :-("
echo "Your unstaged changes and untracked files should be in the last stash."
echo "Your previously staged changes should be in the following commit: $commit_hash"
echo "Please check which commands were executed and try to undo them manually."
echo
}
echo "Committing your staged changes to branch '$target_branch'."
trap 'print_error_message' ERR # Print some hopefully helpful info if something fails
set -x # Print all executed commands
git checkout --quiet 'HEAD~0' # Go into 'detached HEAD' state to avoid changing current branch
git commit --quiet "$@" # Create temporary commit
commit_hash="$(git rev-parse HEAD)" # Note temporary commit ID
git stash push --include-untracked --quiet # Save all other changes from working tree
git checkout --quiet "$target_branch" # Move to target branch
git cherry-pick --quiet "$commit_hash" # Apply changes from temporary commit to target branch
git checkout --quiet "$current_branch" # Switch back to current branch
$rebase_command # Execute git rebase if --rebase flag is present
git stash pop --quiet # Re-apply untracked changes to working tree
set +x # Stop printing executed commands
echo "Success."
if ! [[ "$rebase_command" ]]; then
echo ""
echo "If you want to include those changes in your current branch, you can run:"
echo " git stash; git rebase $target_branch; git stash pop"
echo "or"
echo " git stash; git merge $target_branch; git stash pop"
echo ""
fi
언급URL : https://stackoverflow.com/questions/556923/how-do-i-merge-my-local-uncommitted-changes-into-another-git-branch
'programing' 카테고리의 다른 글
Eclipse 기본 설정으로 되돌리기 (0) | 2023.04.24 |
---|---|
모든 앱 창의 WPF 아이콘 (0) | 2023.04.24 |
퍼스널을 사용합니다.새 워크북에 xlsb 기능이 있습니까? (0) | 2023.04.19 |
C#을 사용하여 마지막으로 채워진 Excel 행을 프로그래밍 방식으로 가져오는 중 (0) | 2023.04.19 |
C#의 해시테이블보다 딕셔너리가 우선되는 이유는 무엇입니까? (0) | 2023.04.19 |