-
Star
(269)
You must be signed in to star a gist -
Fork
(52)
You must be signed in to fork a gist
-
-
Save katylava/564416 to your computer and use it in GitHub Desktop.
Example: You have a branch 'refactor' that is quite different from master. You can't merge all of the commits, or even every hunk in any single commit or master will break, but you have made a lot of improvements there that you would like to bring over to master. | |
# on master | |
> git co -b temp | |
# on temp | |
> git merge --no-commit --no-ff refactor | |
# … which stages everything, so... | |
> git reset head | |
> git add --interactive | |
The following is from an actual merge. | |
staged unstaged path | |
1: unchanged +1/-1 deploy_settings.py | |
2: unchanged +4/-3 requirements.txt | |
3: unchanged +2/-1 settings/defaults.py | |
4: unchanged +1/-1 settings/production.py.example | |
*** Commands *** | |
1: status 2: update 3: revert 4: add untracked | |
5: patch 6: diff 7: quit 8: help | |
What now> | |
# choose 'p' for patch. | |
staged unstaged path | |
1: unchanged +1/-1 deploy_settings.py | |
2: unchanged +4/-3 requirements.txt | |
3: unchanged +2/-1 settings/defaults.py | |
4: unchanged +1/-1 settings/production.py.example | |
Patch update>> | |
# enter the number next to the file you want to process first. | |
# you can keep entering numbers until you've selected all the files, | |
# or you can do them one at a time. an asterisk will appear next to | |
# the files you select. | |
# press 'enter' without entering a number (or anything) to continue | |
# to the next step. | |
# you will see a single diff hunk and it will ask you whether to stage | |
# it or not | |
diff --git a/deploy_settings.py b/deploy_settings.py | |
index 9b110f4..c5b228e 100644 | |
--- a/deploy_settings.py | |
+++ b/deploy_settings.py | |
@@ -4,7 +4,7 @@ This file holds the Fabric deployment settings for this project | |
from fabric.state import env | |
#env.project = 'my_project' #The name of this project | |
-#env.repo_base = 'git@thomasjefferson.katylavallee.com:%s.git' % env.project | |
+#env.repo_base = 'git@git.farstarserver.com:%s.git' % env.project #The base svn repository for this project (ie, parent of trunk) | |
## Directory aliases ## | |
# These are used in the apache vhost config file to defined directory aliases. | |
Stage this hunk [y,n,q,a,d,/,e,?]? | |
# y for yes, n for no | |
# … this will go on for every diff hunk in the selected files | |
# … until you get back to … | |
*** Commands *** | |
1: status 2: update 3: revert 4: add untracked | |
5: patch 6: diff 7: quit 8: help | |
What now> | |
# you can enter 's' to see what you changed | |
staged unstaged path | |
1: unchanged +1/-1 deploy_settings.py | |
2: +4/-3 nothing requirements.txt | |
3: +2/-1 nothing settings/defaults.py | |
4: unchanged +1/-1 settings/production.py.example | |
*** Commands *** | |
1: status 2: update 3: revert 4: add untracked | |
5: patch 6: diff 7: quit 8: help | |
What now> | |
# you can quit now, so enter 'q' | |
# then do some status and diff commands to explore the staged vs unstaged | |
# changes and make sure it looks like you expected. | |
# don't forget to git add or just rm any untracked files as appropriate | |
# finally… | |
> git commit -m "merged selected patches from refactor branch" | |
# don't do commit -a here… you only want to commit the staged changes | |
# revert unstaged changes | |
> git co . | |
# and merge to master | |
> git co master | |
> git merge temp | |
> git br -D temp |
%s/git reset head/git reset HEAD/
Thanks a lot! This was what I was looking for!
Nice thanks.
👍👍
Lifesaver!
Excellent, thanks!
This is so great, thanks a lot!
Thanks this helped a lot!
Awesome workflow!
Note, if we deleting merged branch better to use:
git branch -d test
Instead of -D
option.
REALLY helpful today! Thanks for writing this up!! Learned a ton.
Thanks so much! That was a very helpful article. Minor typo: head -> HEAD.
huge help!
git merge
! Here's the reason:
https://github.com/bimlas/learning-by-testing-git-partial-merge/tree/doc
Thanks! Exactly what I was looking for!
Thanks this is super helpful!
This was super helpful! A modification I found to be helpful: If you create two 'dummy branches' (so to speak), one at master
and one at refactor
, then merge these using the --no-commit --no-ff command, your original refactor
branch will remain with a record of your commit history. It's not pretty because you have an unresolved branch, but it retains the info if you need it.
Thank you so much!
Thanks for this! mega handy
Thank you for this. It helped me a lot!
Hi guys:
Can merging partially lead to lost commits or files in any cases?
NO! Don't do any kind of partial merge! Chekout the files or hunks from another branch, but don't do it as
git merge
! Here's the reason:
https://github.com/bimlas/learning-by-testing-git-partial-merge/tree/doc
Hi, the link is dead. PLEASE update it.
Any way I lost several commits after partial merge... I'm looking and thinking why...
@nlpsuge - were you able to find why you lost those commits from partial merge
Step 1: Switch to the branch you want to merge with, for example, dev_target_branch
Step 2: Merge a branch with dev_target_branch, for example, dev_another_branch
git merge --no-commit --no-ff dev_another_branch
Step 3:
In IDEA, go to Project -> Git, click 'Commit Directory...'. Only choose those files/directories you want to commit, then Commit.
Step 4:Override those files/directories you don't want to commit.
git checkout -- files_or_directories_you_do_not_want_to_commit
Step 5:Commit and push partial commits
Step 6:
In IDEA, merge origin/master with dev_target_branch, then commit and push.
After a few days, I found some codes in files_or_directories_you_do_not_want_to_commit no in the branch dev_another_branch.
Unfortunately, I can't reproduce this issue according above steps. I don't know exactly why this happens.
NO! Don't do any kind of partial merge! Chekout the files or hunks from another branch, but don't do it as
git merge
! Here's the reason:
https://github.com/bimlas/learning-by-testing-git-partial-merge/tree/docHi, the link is dead. PLEASE update it.
Any way I lost several commits after partial merge... I'm looking and thinking why...
Not sure but this may be the same link in gitlab
https://gitlab.com/bimlas/learning-by-testing-git-partial-merge/-/tree/doc
I love that this is still dead useful 8 years later
Thanks very much!
Please fix this small error:
git reset head
to
git reset HEAD
Thanks a lot, very helpful guide to clean up messy branches!
wooooo thanks a lot
Anyone know, if say you have a branch main and a branch dev with more changes, and you do a partial merge of changes from dev into main. If in the future, you want to merge dev into main, will there be merge conflicts, or does it know some changes have already been merged in? Would be pretty easy to test I guess. :)
Nice! When you want to move some files to another branch there is an alternative way though:
At this point files are staged and can be committed simply to mergeTo branch