by mistake I deleted files that I actually just wanted to move to another directory. I have also committed and pushed this changed, so that these files are regarded as deleted. Is there a way to move these files to a directory afterwards and make them to be seen as "moved"?
This would help to keep the git history clean.
Thanks in advance.
CodePudding user response:
Assuming:
- Your branch is not shared, which lets you rewrite its history
- You want to avoid this "mistake" staying in history, like if you did it right the first time
- The "bad" commit is the last one you committed/pushed (i.e. There are no commits to preserve after this one)
THEN you could :
# restore your files in the working tree
git restore --source=HEAD^ -- path/to/file1 path/to/file2 path/to/dir/*
# at this point, move the files where you want them to be
# then record them into index
git add .
# and edit last (bad) commit to record the move instead of the deletion
git commit --amend
# finally, push to remote for the new branch history to replace the old one
git push --force
CodePudding user response:
You can revert the changes:
git revert HEAD
Then, move your files and commit again. Take into account that git takes into account that a file was moved only if the renameLimit is high enough (https://git-scm.com/docs/merge-config).