Ben Brinckerhoff
Ben Brinckerhoff

Reputation: 287

When using git, how do you push the exact working directory to a remote?

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:

Clearly my idea is quite complex and ugly. Any ideas?

Upvotes: 6

Views: 5582

Answers (4)

CB Bailey
CB Bailey

Reputation: 791481

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"

Upvotes: 12

Ben Brinckerhoff
Ben Brinckerhoff

Reputation: 287

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)?

Upvotes: 1

jscoot
jscoot

Reputation: 2109

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

Upvotes: 3

dylanfm
dylanfm

Reputation: 6345

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.

Upvotes: 0

Related Questions