Search code examples
gitgithubgit-commit

how to git move folder and files to new folder and keep history?


I want to a move folder and files in it to a new folder. Both folder name and file names have changed.

Dir1
  file11
  file12

 TO

Dir2
  file21
  file22

I also want to keep the history.

Can I do this in one commit? Or I need the following 2 steps?

git mv Dir1 Dir2
commit and push

git mv Dir2/file11 Dir2/file21
git mv Dir2/file11 Dir2/file22
commit and push

Solution

  • Yes you can do this in only one commit with:

    git mv Dir1 Dir2
    git mv Dir2/file11 Dir2/file21
    git mv Dir2/file11 Dir2/file22
    commit and push
    

    You can also do this in two commits and squash them using git rebase -i HEAD~3 if you prefer.