How to use git mv from magit?
Asked Answered
C

3

29

Is there a nice way to call git mv on a file from within magit? I know it's possible to run any git command with :, but this won't autocomplete filenames.

Chough answered 12/7, 2013 at 8:7 Comment(2)
Just for information: what do you think git mv does? Do you realize that git does not track moves? git mv will just rename the file, remove the old name from the index and adds the new one.Bevbevan
Yep, it's just as everything else in magit is basically one/two keypress(es), I was hoping there was something for mv.Chough
B
20

I'm not sure offhand if there's a direct way, but...

Given that a mv is the same thing as a rm plus an add, you can just rename the file normally (e.g. in dired), and then stage both of the consequent changes -- a deleted file and a new file.

Git should figure it out.

Edit:

M-x vc-rename-file ?

Birkle answered 12/7, 2013 at 9:56 Comment(4)
This works, but I was looking for something to do it automagically. : mv /path/to/file1 /path/to/file2 (from a magit buffer) is even quicker, but it doesn't autocomplete.Chough
Excellent, vc-rename-file does what I want!Chough
How do I do force the git mv? I'm getting the error: "Please update files before moving them"Dactyl
nnyby: So I'm guessing there are unmerged changes to the file you're trying to move, so you should do as it says and merge in those changes first, otherwise I expect they'll be lost. If you're 100% certain you don't want the changes, the -f option might do the trick. (Failing that, you could always just copy the file, and manually restore it after merging.)Birkle
D
23

For reference at least in current verion of magit there is magit-file-rename which can be invoked by R.

Dixiedixieland answered 23/9, 2016 at 21:35 Comment(2)
It would be nice to have magit-file-move too.Biff
since magit buffer does not list tracked files, move the cursor to the last empty line and hit R to invoke magit-file-renameGrimace
B
20

I'm not sure offhand if there's a direct way, but...

Given that a mv is the same thing as a rm plus an add, you can just rename the file normally (e.g. in dired), and then stage both of the consequent changes -- a deleted file and a new file.

Git should figure it out.

Edit:

M-x vc-rename-file ?

Birkle answered 12/7, 2013 at 9:56 Comment(4)
This works, but I was looking for something to do it automagically. : mv /path/to/file1 /path/to/file2 (from a magit buffer) is even quicker, but it doesn't autocomplete.Chough
Excellent, vc-rename-file does what I want!Chough
How do I do force the git mv? I'm getting the error: "Please update files before moving them"Dactyl
nnyby: So I'm guessing there are unmerged changes to the file you're trying to move, so you should do as it says and merge in those changes first, otherwise I expect they'll be lost. If you're 100% certain you don't want the changes, the -f option might do the trick. (Failing that, you could always just copy the file, and manually restore it after merging.)Birkle
S
8

I use dired for this. C-x d when the file is open, then press ! on the file to run git mv ? newpath command with the file as the ? argument.

Shortwave answered 4/11, 2013 at 8:42 Comment(1)
This doesn't change the directory of the emacs buffer, which I often forget to my own confusion...Biff

© 2022 - 2024 — McMap. All rights reserved.