tags:

views:

553

answers:

4

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?

A: 

Sorry if I have misunderstood you, but I think you just want to do the standard pull and push? Have a read of this. Git will handle any merges for you, and if there are conflicts you can sort them out easily.

dylanfm
No, unfortunately I don't think standard push and pull will work for me. Let's say file 'foo.txt' exists in the remote repo, but not in my local directory (which, remember, is not under source control yet). If I do 'git init', add the remote, and pull - it will create foo.txt!
Ben Brinckerhoff
Sorry, I must've misunderstand you. Yes, it will create foo.txt. Git repositories are all copies of each other.
dylanfm
Yes, I guess I just want a way to tell Git "Take the diff between the current working dir and the remote and apply it to the remote!"
Ben Brinckerhoff
+3  A: 

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
jscoot
A: 

Here is my current solution (let's say I'm already in the directory that contains the current files, but is not get a git repo)

git clone -n [git_url] tmp_git_dir
mv tmp_git_dir/.git .
rm -rf tmp_git_dir
git add .
git commit -a -m "commit message"

This seems more simple and will automatically add and remove files as necessary. I think it's more efficient as well.

I wonder - is there a way to clone the git repo into the current dir (basically I just want the .git dir in place in the current dir so I don't have to do the mv and rm)?

Ben Brinckerhoff
+1  A: 

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"
Charles Bailey
That's perfect! Thank you very much!
Ben Brinckerhoff