reddit / emacs

Magit, How Can I Import Specific Lines From Another Branch?

_analysis230_ · 2022-09-07 18:43
I have a project with two different independent branches. I want to open the same file in both and then pick a few lines that I want to "import" from one branch to another. I don't want to merge them I just want a way to pick the lines of code I want. Edit: I was literally struggling with this for two days, I opened up VSCode and did it there and it took all of 5 mins. There has to be a way you can do it in magit too, right? Please show me
lebensterben · 2022-09-07 19:21
just checkout the specific file and then select the chunks you want (and discard the rest)
aisamu · 2022-09-07 19:21
IIUC, `magit-find-file` is what you're looking for. From the man himself: https://emacs.stackexchange.com/a/7683
_analysis230_ · 2022-09-07 19:23
Okay. Yes, I can find the file. How do I ediff it with the file on my current branch now?
arthurno1 · 2022-09-07 20:13
I don't know how you would do it in magit, but in plain gityou can create another [worktree](https://git-scm.com/docs/git-worktree) and checkout the other branch. You can then diff two files, copy what you want etc. [Worktrees](https://git-scm.com/docs/git-worktree) let you have different branches checked out at the same time.
_analysis230_ · 2022-09-07 20:54
This might come in handy. I'd have to learn some git
arthurno1 · 2022-09-07 21:11
Worktrees are super handy :). Probably the most handy feature since Git itself :). Branches are good, but worktrees are the way to work with branches. If you need to create a fix, or wish to add a new feature, or just look at another branch, just create a new worktree and checkout whichever branch you like, or let it create a new one automatically for you. I like to create it outside the git repo itself 'git worktree add ../some-new-path' so I can play in the new worktree, create new files, remove stuff, do what I want, and I just delete the entire work tree if I don't want it without worrying about extra files and stuff in main repo. No need to stash och commit if I need to check out other branch either. Handy for fast hacks, fixes, tests, etc :). Redhat have a few nice tutorials on worktrees, they are all short and easy reads, I can recommend them if you are new to worktrees. [Here is one to start with.](https://opensource.com/article/21/4/git-worktree)
nv-elisp · 2022-09-07 21:50
You probably want `magit-file-checkout` which will prompt you for the branch/file. The diff will be applied and you can manipulate (unstage hunks) from there. From the command line you could also do something like `magit checkout -p branch file` which will similarly ask which hunks of the diff you'd like to apply. There's also `magit-ediff` which you could use with the range option to select the same file on another branch.
7890yuiop · 2022-09-08 02:19
`M-x ediff-buffers`
7890yuiop · 2022-09-08 02:22
If the changes to be applied are from some commit in another branch (i.e. you want to use *part* of a commit), then just open that commit in Magit (e.g. `l o` to view the log for another branch and view the relevant commit), and then `a` to apply the desired changes (file, hunk, region) as usual. If you just want some arbitrary lines, I would start with `magit-find-file` as suggested in other comments (or from a commit view you can use `RET` on some addition or removal text to open the relevant revision of that file).
_analysis230_ · 2022-09-08 03:32
I was looking for this. Magit ediff did it
aisamu · 2022-09-08 17:04
Ooh, thanks for the `magit-ediff` pointer!
CulturMultur · 2022-09-09 19:56
I usually do it from CLI: git checkout <other-branch> -- path/to/file.txt Then using magit keep/remove desired chunks from the index and commit.