Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 544
Ben Brinckerhoff Avatar asked Jan 23 '09 04:01

Ben Brinckerhoff


2 Answers

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"
like image 109
CB Bailey Avatar answered Sep 21 '22 01:09

CB Bailey


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
like image 25
jscoot Avatar answered Sep 18 '22 01:09

jscoot