I have a branch A and want to make a list of quite significant changes to it. The end result of these changes already exists in directory B, which I would also like to become the new working copy. In essense, I just want to "Make this directory the latest working copy of my branch."
There are 2 possibilities I am currently aware of to achieve this:
- Checkout A into a temporary directory C, delete everything except ".git", copy the contents of directory B into C and then add/commit/push from directory C. Finally, move the entire contents of C back into B.
- Clone the repository without checkout into temporary directory C, move the ".git" directory from C into B. Add/commit/push from within B.
The problems with both of these solutions, besides being horribly clunky, is that they require disk space for both directories B and C. Option 1 also involves a little more file shuffling. So currently my best solution to this is option 2, detailed steps given below.
I imagine there's a better way to do this using "reset" or "symbolic-ref". I have tried various options without success.
mkdir /tmp/git_output
cd /tmp/git_output
git init
git remote add origin /path_or_url_to/git/repo.git
git fetch
git checkout name_of_branch
mv .git /path/to/new/src/
cd /path/to/new/src/
git commit -a
git push
rm -rf /tmp/git_output
Thanks for any help.
I think it should be possible to delete everything from your working copy (make triple sure to NOT delete your
.gitfolder – make backups!). Then, copy over the contents of your directoryB,git add -u(depending on your versoin of Git) to remove missing files from the index and add new files to the index. Commit. Be happy.Another way I can think of is to initialize a fresh Git repository in your directory
B, commit all files, then push to A:cd B; git init; git add .; git commit -m 'complete rewrite'; git push /path/to/A HEAD:rewrite.Now, in
A, create a graft, telling Git that the tip of branchrewritehas the tip of branchmasterofAas its parent. Then rungit filter-branchto make that graft permanent.