How can I move all git content one-level up in the folder hierarchy?

前端 未结 9 2284
北荒
北荒 2020-11-30 22:09

I have a git repository whose structure looks like:

+--repo.git
|
+----+bootstrap.py
+----+buildout.cfg
+----+.gitignore
+----+webapp
|
+---------+manage.py
         


        
9条回答
  •  半阙折子戏
    2020-11-30 22:50

    The solution you mentioned should work, as git tracks changes based on the hash of the files first and then their location.

    This wont work if as part of moving the file, you change the contents of the files.

    Bottom case, try it, and if it doesn't work, you can revert the changes, before pushing the changes to the master repo :). This is one of the reasons why I really like git.

    Edit

    I forgot to mention that to see the changes after a rename, you need to use the '--follow' parameter. Check this example

    First, I created a new git repo

    94:workspace augusto$ mkdir gittest
    94:workspace augusto$ cd gittest/
    94:gittest augusto$ git init
    Initialized empty Git repository in /Volumes/Data/dev/workspace/gittest/.git/
    

    Then created a file in folder/test

    94:gittest augusto$ mkdir folder
    94:gittest augusto$ vi folder/test
    94:gittest augusto$ git add folder/test
    94:gittest augusto$ git commit -am "added file"
    [master (root-commit) 7128f82] added file
     1 files changed, 1 insertions(+), 0 deletions(-)
     create mode 100644 folder/test
    

    Then moved the file to newfolder/test

    94:gittest augusto$ mkdir newfolder
    94:gittest augusto$ mv folder/test newfolder/
    94:gittest augusto$ git add newfolder/test 
    94:gittest augusto$ git commit -am "moved/renamed file"
    [master 4da41f5] moved/renamed file
     1 files changed, 0 insertions(+), 0 deletions(-)
     rename {folder => newfolder}/test (100%)
    

    And git log --follow newfolder/test shows the full history (I've added the parameter -p to show more information, such as the path).

    94:gittest augusto$ git log --follow -p newfolder/test 
    commit 4da41f5868ab12146e11820d9813e5a2ac29a591
    Author: Augusto Rodriguez 
    Date:   Sat Aug 20 18:20:37 2011 +0100
    
        moved/renamed file
    
    diff --git a/folder/test b/newfolder/test
    similarity index 100%
    rename from folder/test
    rename to newfolder/test
    
    commit 7128f8232be45fd76616f88d7e164a840e1917d5
    Author: Augusto Rodriguez 
    Date:   Sat Aug 20 18:19:58 2011 +0100
    
        added file
    
    diff --git a/folder/test b/folder/test
    new file mode 100644
    index 0000000..3b2aed8
    --- /dev/null
    +++ b/folder/test
    @@ -0,0 +1 @@
    +this is a new file
    

    I hope this helps!

提交回复
热议问题