Rename files and folders with git
来源:互联网 发布:软件产业基地1栋 编辑:程序博客网 时间:2024/05/29 03:18
轉載自http://www.patrick-wied.at/blog/rename-files-and-folders-with-git
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.
- Rename files and folders with git
- Working with iPhone files and folders
- dos xcopy files and folders
- Display hidden files or folders with CutFTP
- enumerate Files and Folders(遍历)
- Android: Loading files from the Assets and Raw folders
- delete Files and Folders(删除文件、文件夹)
- recover all files with git
- git - Restore locally deleted files with git
- Windows 64-bit: The 'Program Files (x86)' and 'SysWOW64' folders explained
- mv - move (rename) files
- git pull Checkout conflict with files:
- git pull Checkout conflict with files:
- Virtualbox and Shared Folders
- Virtualbox and Shared Folders
- Reading and writing .mat files with Python
- Base64 encoding and decoding files with POCO
- Files and Exceptions: Dealing with Errors
- ios xmpp 文件传输
- unity 给物体边缘加高光轮廓的办法,付Demo(增加一组算法)
- How can I recover a file that I deleted in my local repo from the remote repo in Git?
- sgu-249 Matrix
- wpf 的拖拽功能实现(含拖拽复制功能实现)
- Rename files and folders with git
- XCode5添加新建类模板(Cocos2dx Template Class for Scene or Layer)
- ubuntu获取版本号和内核信息的方法
- linux配置java环境变量 转过几个,这个最详细和靠谱
- ubuntu获取版本号和内核信息的方法
- Unity3D性能优化总结
- Core Plot学习笔记:绘制饼图
- Android开源框架ImageLoader的完美例子
- Unity 相关经典博客资源总结(持续更新)