I have a a remote git repo and a local clone. Let's say I lose my local .git directory and subsequently add and remove some files to the local working directory.
At some point, I want to re-init the local repo, connect it to the remote, and ultimately push my local working dir to the remote exactly as it is (which is to say, I want to have all the added/deleted files be the same in the remote)
How would I accomplish this?
Here is my current solution, which I don't like (and may not work in all cases).
git init
git remote add origin [some_url]
git add . # adds all the files in the working dir
git commit -m "adding files"
(At this point, my current idea is to:
make a branch,
fetch the remote into it,
'git diff master branch > my_patch'
apply that patch to the branch,
push from the branch to the remote,
pull into the master,
and kill the branch.)
Clearly my idea is quite complex and ugly. Any ideas?
Something like this (assuming you are starting from the lost .git moment)?
# Make the current directory a git repository.
# This puts you on a master branch with no commits.
git init
# Add a reference to the remote repository.
git remote add origin urlurlurl
# Fetch the remote refs.
git fetch
# Without touching the working tree, move master branch
# from nowhere onto the remote master.
git reset origin/master
# Stage all changes between the new index and the current tree.
git add -A
# Make a commit of these changes.
git commit -m "New working tree as a patch on remote master"
I don't think it is necessary to create a new branch in your case. Here is my answer: Do the following in your current local broken repo:
git init
git add .
git commit -m 'add files' #create the local master branch
git remote add myproj [some_url]
git fetch myproj
git branch -r #check the remote branches
git diff master..myproj/master #view the diffs between your local and remote repos
#now you are sure there is no conflict,
#so you merge the remote to your local master branch
git pull myproj master
git push myproj #push your local master branch to remote
#now your upstream and downstream is sync'ed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With