博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rename files and folders with git
阅读量:4285 次
发布时间:2019-05-27

本文共 3159 字,大约阅读时间需要 10 分钟。

轉載自

 

Rename files and folders with git

As easy as it sounds, it turned out renaming files and folders with git can sometimes be pretty painful. So it happened to me that I was working on a branch of a project and had to rename a subfolder.

Renaming with git mv

For renaming files or folders use nothing but the git mv command. git mv takes at least two arguments, a source and a destination. If you want to move several files to a single path you may specify n sources but the last argument is the destination.
Here’s what ‘git mv’ actually does:

mv oldfolder newfoldergit add newfoldergit remove oldfolder

 

 

It performs a file move, adds the new file to the index and removes the old file from it. We can see there’s no commit so we have to add the updates and commit the changes afterwards.

 

How to use

Assuming you’d like to change a folder’s name from oldfolder to newfolder

git mv oldfolder newfolder
 

If there’s already a newfolder in your repo and you’d like to overwrite it use –force

git mv -f oldfolder newfolder
 

Do not forget: you have to add the changes to the index and commit them after renaming with git mv.

git add -u newfoldergit commit -m "changed the foldername whaddup"
 

The -u option at the add command is important here, it’ll update already tracked files/folders.
This is a pretty simple usecase and should work almost every time. Except, and here’s the part where it gets interesting:

 

Renaming foldername to folderName on case insensitive file systems

Why is this more interesting? Because simple renaming with a normal mv command(not git mv) won’t get recognized as a filechange from git. If you try it with the ‘git mv’ command like in the following line

git mv foldername folderName
 

If you’re using a case insensitive filesystem, e.g. you’re on a Mac and you didn’t configure it to be case sensitive, you’ll experience an error message like this one:
fatal: renaming ‘foldername’ failed: Invalid argument

And here is what you can do in order to make it work:

git mv foldername tempname && git mv tempname folderName

This splits up the renaming process by renaming the folder at first to a completely different foldername. After renaming it to the different foldername the folder can finally be renamed to the new folderName. After those ‘git mv’s, again, do not forget to add and commit the changes. Though this is probably not a beautiful technique, it works perfectly fine. The filesystem will still not recognize a change of the letter cases, but git does due to renaming it to a new foldername, and that’s all we wanted :)

 

Other interesting observations

git mv has a command option called ‘–dry-run’ (short: ‘-n’) which is supposed to show what changes will be made if you run the command, but it won’t execute the command. Funny thing is that if you rename a folder/file with the -n option:

git mv -n foldername folderName
 

it will show the changes that should happen (even on a case insensitive filesystem) so you think everything should work fine but running it without -n will then fail on case insensitive filesystems.

Okay, honestly.. it was not that painful, but for a simple rename it was a lot of debugging time I’ve spent on it.

转载地址:http://qqsgi.baihongyu.com/

你可能感兴趣的文章
C# 常见Url操作实例(二)
查看>>
C# Url操作类封装、仿Node.Js中的Url模块
查看>>
AngularJS ng-checked指令
查看>>
AngularJs ng-change事件/指令
查看>>
c#必须使用适当的属性或方法修改此标头解决办法
查看>>
C#文件下载、文件分块下载实例(一)
查看>>
C#分块下载文件实例(二)
查看>>
C#下载实例(三)-断点下载
查看>>
AngularJs2.0正式发布
查看>>
CodeFirst int类型主键问题 column does not allow nulls. INSERT fails.
查看>>
微信公众号开发,消息创建时间(整形)C#处理
查看>>
数据库中字段类型对应的C#中的数据类型
查看>>
微信网页授权操作逻辑封装-C#实例
查看>>
微信6.0之后,分享接口使用
查看>>
微信js-sdk,分享接口常用逻辑分装
查看>>
SingalR自托管(self-host)实例
查看>>
SingalR自托管(self-host)实例2-实现Wpf客户端和Web客户端矩形同步
查看>>
微信js-sdk 预览图片接口&从拍照或手机相册中选图接口
查看>>
微信js-sdk 上传图片、下载图片接口
查看>>
微信js-sdk 地理位置接口实例
查看>>