问题
I am using sourceTree for Mac as my GUI so I would appreciate instructions on using it but obviously I would like to learn the bash commands as my first language.
We have a remote repo we call develop
, and the practice is, when you develop a feature, you create a branch from develop
for that feature, for example feature1
, pull it down, and then submit a pull request on the branch you created.
I have recently developed some analysis tools which I don't want to get put into the remote code base (yet, anyway).
How would I, locally, merge my analysis
branch with my feature1
branch for testing, and then unmerge analysis
after I'm completed?
Which branch should I have checked out at the time I do the merge, feature1
or analysis
?
NOTE: I want to retain all the file changes to feature1
I did while the branches were merged; the files and edits from analysis
are extremely unlikely to conflict with my feature branch, so essentially I'm adding in some non-conflicting code, then pulling the branch out; so any commits I have done to feature1
, I don't want to lose.
回答1:
To unmerge a branch, you can simply git revert
the merge commit like so:
git checkout feature_branch
git revert -m 1 <merge_commit_sha>
It is possible that you will end up with some conflicts that you'll have to manually unmerge, just like when merging normally.
回答2:
tl;dr
I recommend you to approach your analysis tools as a separate code under it's own source control and never merge it to your feature branches. Keep it in a folder with own git repo and under .gitignore from higher-level repo.
Detailed explanation
Do I understand it right that:
- You have separate analysis code which is used for development but doesn't become a part of feature code,
- And it is not supposed to become a part of shared codebase,
- But you want it in the project during the development time?
If this is so:
- Merging is not a way at all. By merging you bring your code into the commits that will be later pushed to develop repo. It will still exist in the remote code base in several middle commits on each feature branch. (This can be solved with
git filter-branch
, but to my opinion it's crushing a fly with a steam-roller. Not worth it.) - Most probably you will develop your analysis tool, so it needs own versioning and source control.
Solution
Make a special folder in your project and put it in .gitignore
:
/analysis/*
If you're using Maven or some other tool which implies strict structure policy, this may be /../../../../../analysis
, but that's no difference.
Have all your analysis-related code in that folder. Initiate a separate repo there and make commits as you develop your tools.
Work with your feature branches just as you did before. The only file you have changed now is your .gitignore. Before you push to remote, do:
git checkout <sha-of-branching-time-commit> -- .gitignore
git commit -m'fixed .gitignore'
This returns the master-repo .gitignore
to initial state. You may want to make a backup of modified gitignore for future featur branches.
Optionally, you can make an agreement with your team to have a designated "this is my code and I'm not gonna share it" folder. Then it can be added to common .gitignore
来源:https://stackoverflow.com/questions/30410588/git-unmerge-a-branch