Difference between revisions of "How to do git tricks"
Line 14: | Line 14: | ||
https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch | https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch | ||
− | == | + | ==Include branch from an unrelated repository== |
− | + | <pre> | |
+ | git remote add rep2-git url_to_unrelated_repository | ||
+ | git fetch rep2-git | ||
+ | git checkout remotes/rep2-git/branch2 | ||
+ | git checkout -b rep2/branch2 | ||
+ | git push -u origin rep2/branch2:rep2/branch2 | ||
+ | </pre> | ||
+ | The resulting repository has another unrelated history. | ||
+ | There is no need for the two repositories to have a common initial commit or anything. | ||
+ | The commit hashes are preserved. You can even <tt>git cherry-pick</tt> commit from the other repostory and git will do pretty good job finding files to apply the commits to, even if the files have somewhat different names and are in somewhat different locations in the file two file trees. | ||
− | Be sure to do | + | ==Using git cherry pick== |
+ | ===Across unrelated histories=== | ||
+ | After importing branches from another unrelated repository as an unrelated history, | ||
+ | <tt>git cherry</tt> pick can apply a commit from the unrelated history, but: | ||
+ | * Sometimes git cherry-pick will work across files having different paths and sometimes just creates copies on new paths, which is not helpful. Not sure why. The same cherry-pick on different computers will do different things. | ||
+ | * Be sure to do | ||
<pre> | <pre> | ||
git diff HEAD^ | git diff HEAD^ | ||
</pre> | </pre> | ||
− | to be | + | to be see what changes are being made and to minimize them. <tt>git dif</tt> is misleading. |
+ | |||
+ | ===Undesirable changes from other commits==== | ||
+ | Sometimes git cherry pick will make more changes including those from other commits. | ||
+ | [https://stackoverflow.com/questions/7802252/why-cherry-pick-pick-change-more-than-one-commit Git goes back in the history until the cherry pick source matches the target and creates the patch based on this revision. That's why more changes might appear ...] | ||
==Patch== | ==Patch== | ||
+ | ===Instead of cherry-pick=== | ||
+ | alternatively one can use patch | ||
<pre> | <pre> | ||
git log -p -1 <sha1-of-your-commit> | git log -p -1 <sha1-of-your-commit> | ||
</pre> | </pre> | ||
− | + | then | |
− | |||
− | |||
− | |||
<pre> | <pre> | ||
git log -p -1 <sha1> | git apply --3way - | git log -p -1 <sha1> | git apply --3way - | ||
</pre> | </pre> | ||
− | + | But this can pull more changes too. And without --3way it fails. | |
− | But this | + | ===[https://www.codefull.org/2018/11/specify-target-file-in-git-cherry-pick To modify only one file |
− | + | ]=== | |
− | |||
# Create a patch file for that individual file: | # Create a patch file for that individual file: | ||
Line 48: | Line 64: | ||
git apply --3way patchfile | git apply --3way patchfile | ||
</pre> | </pre> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 07:35, 21 January 2019
Find last commit where line existed
blame --reverse START
Comparing files
git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt
How to link to such comparison view on github?
Compare and copy files from other branches
https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch
git remote add rep2-git url_to_unrelated_repository git fetch rep2-git git checkout remotes/rep2-git/branch2 git checkout -b rep2/branch2 git push -u origin rep2/branch2:rep2/branch2
The resulting repository has another unrelated history. There is no need for the two repositories to have a common initial commit or anything. The commit hashes are preserved. You can even git cherry-pick commit from the other repostory and git will do pretty good job finding files to apply the commits to, even if the files have somewhat different names and are in somewhat different locations in the file two file trees.
Using git cherry pick
After importing branches from another unrelated repository as an unrelated history, git cherry pick can apply a commit from the unrelated history, but:
- Sometimes git cherry-pick will work across files having different paths and sometimes just creates copies on new paths, which is not helpful. Not sure why. The same cherry-pick on different computers will do different things.
- Be sure to do
git diff HEAD^
to be see what changes are being made and to minimize them. git dif is misleading.
Undesirable changes from other commits=
Sometimes git cherry pick will make more changes including those from other commits. Git goes back in the history until the cherry pick source matches the target and creates the patch based on this revision. That's why more changes might appear ...
Patch
Instead of cherry-pick
alternatively one can use patch
git log -p -1 <sha1-of-your-commit>
then
git log -p -1 <sha1> | git apply --3way -
But this can pull more changes too. And without --3way it fails. ===[https://www.codefull.org/2018/11/specify-target-file-in-git-cherry-pick To modify only one file ]===
- Create a patch file for that individual file:
git show [commit hash] -- path/to/old/file > patchfile
- Manually edit the newly created patchfile and replace all occurrences of the old path with the new path.
- Apply the patch via
git apply --3way patchfile