1

I have a local directory that contains the source code, minus the .git folder, so it has no git integration. I want to add the remote and pull down any changes. I tried git init && git remote add origin https://..., however it wants me to make an initial commit. As there is an existing project, I don't want to make an initial commit.

How can I add the remote repo remote and pull down changes without an initial commit?

2
  • First google result. Does it work? Commented Nov 29, 2016 at 19:02
  • 1
    I think that an easier approach would be to do it in the opposite order. First clone the remote repository to have a local copy with it's .git folder, then copy your changes into the existing repository. Commented Nov 29, 2016 at 19:03

3 Answers 3

2

Easier would be:

git clone /url/remote/repo git --work-tree=/path/to/source add . git commit -m "update repo with local source" git push 

The trick is using the git --work-tree=<path> option in order to add files which are not in the current local repo worktree.

Sign up to request clarification or add additional context in comments.

Comments

0

You are on the right path. However, to answer your question, you cannot, at least not without cloning/forking. If you just adding your code to an already existing repo, and you are a collaborator of said repo, you can branch the repo, add your code, and pull request for the owner to merge your branch into the master.

If you are the only dev on the project your init commit and subsequent push simply add the project to server, and thus start the git magic of version control.

Comments

-1

I want to add the remote and pull down any changes.

You can turn the local directory into a Git repository, add the remote, and fetch its content.

git init git remote add origin repo-url git fetch origin master 

When you say "pull down any changes", it seems to suggest that you don't have local changes, you just want to be up to date with the remote repo. If that's the case, you can hard-reset the local repo to the remote, discarding any local differences:

git reset --hard origin/master 

As a result, the local directory will be up to date with the remote repo, and it will be a proper Git repo with a .git directory.

2 Comments

You can't run git commands in a folder that is not initiated as a git repo. The OP states that his code resides in a location without a .git folder.
@Lix I understood the question that he already did git init. Maybe I misunderstood. I clarified that point now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.