我想在Git中重命名/移动项目子树
/project/xyz
Run Code Online (Sandbox Code Playgroud)
至
/components/xyz
Run Code Online (Sandbox Code Playgroud)
如果我使用普通的git mv project components,那么所有的提交历史xyz project都会丢失.有没有办法移动这个以保持历史?
我读过,当在git中重命名文件时,你应该提交任何更改,执行重命名,然后暂存重命名的文件.Git将从内容中识别文件,而不是将其视为新的未跟踪文件,并保留更改历史记录.
然而,今晚这样做我最终还是回归git mv.
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
Run Code Online (Sandbox Code Playgroud)
将Finder中的样式表重命名iphone.css为mobile.css
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be …Run Code Online (Sandbox Code Playgroud) 我在没有考虑的情况下手动移动了一堆文件,并且找不到让git认识到文件只是被移动而不是实际上不同的文件的方法.有没有办法做到这一点,除了删除旧的和添加新的(从而丢失历史),或使用git-mv重做所有更改?
在重构源代码时,有时您需要在文件中移动大块文本,甚至移动到新文件.您创建一个分支refactored并提交:
$git checkout master
$git branch refactored
$git checkout refactored
<move code around>
$git commit -m "refactored code"
Run Code Online (Sandbox Code Playgroud)
但是,人们可能会在旧的预重构分支之上提交,更改已移动的代码:
$git checkout master
<change code that was moved elsewhere on branch refactored>
$git commit -m "bugfix"
Run Code Online (Sandbox Code Playgroud)
在分支上refactored,您希望合并在master以下位置进行的更改:
$git checkout refactored
$git merge master
<giant merge conflict>
Run Code Online (Sandbox Code Playgroud)
这导致了大的合并冲突.如果有办法告诉git简单地移动了内容,应该可以自动合并.
更糟糕的是,即使在解决冲突并提交冲突之后,git仍然无法使用该解决方案来确定进一步的合并:
<fix conflicts>
$git commit -m "merge master into refactored"
$git checkout master
<change more code>
$git commit -m "bugfix2"
$git checkout refactored
$git merge master
<another giant …Run Code Online (Sandbox Code Playgroud) 我们使用GIT和Visual Studio 2013(Ultimate + Professional版本).解决方案和项目的代码在GIT中,只要我们在Visual Studio中重命名文件,GIT就会将它们视为
c:\temp\testCodeSnippets>git status
# On branch master
nothing to commit (working directory clean)
Run Code Online (Sandbox Code Playgroud)
c:\temp\testCodeSnippets>git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: OldName.cs
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# NewName.cs
no …Run Code Online (Sandbox Code Playgroud) 当我重命名文件时,git认为我删除了该文件并创建了另一个文件.如何重命名文件,以便git知道我正在"重命名"它?