Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert existing non-empty directory into a Git working directory and push files to a remote repository

Tags:

git

  1. I have a non-empty directory (eg /etc/something) with files that cannot be renamed, moved, or deleted.

  2. I want to check this directory into git in place.

  3. I want to be able to push the state of this repository to a remote repository (on another machine) using "git push" or something similar.

This is trivial using Subversion (currently we do it using Subversion) using:

svn mkdir <url> -m <msg> cd <localdir> svn co <url> . svn add <files etc> svn commit -m <msg> 

What is the git equivalent?

Can I "git clone" into an empty directory and simply move the .git directory and have everything work?

like image 441
HMW Avatar asked Jul 22 '10 17:07

HMW


People also ask

How do I convert a directory to a git repository?

git init Existing Folder For an existing project to become a Git repository, navigate into the targeted root directory. Then, run git init . Or, you can create a new repository in a directory in your current path. Use git init <directory> and specify which directory to turn into a Git repository.


2 Answers

Given you've set up a git daemon on <url> and an empty repository:

cd <localdir> git init git add . git commit -m 'message' git remote add origin <url> git push -u origin master 
like image 81
abyx Avatar answered Sep 25 '22 08:09

abyx


This is how I do. I have added an explanation to understand what the heck is going on.

Initialize Local Repository

  • first, initialize Git with

    git init

  • Add all Files for version control with

    git add .

  • Create a commit with a message of your choice

git commit -m 'AddingBaseCode'

Initialize Remote Repository

  • Create a project on GitHub and copy the URL of your project. as shown below:

enter image description here

Link Remote repo with Local repo

  • Now use copied URL to link your local repo with the remote GitHub repo. When you clone a repository with git clone, it automatically creates a remote connection called origin pointing back to the cloned repository. The command remote is used to manage a set of tracked repositories.

    git remote add origin https://github.com/hiteshsahu/Hassium-Word.git

Synchronize

  • Now we need to merge local code with remote code. This step is critical otherwise we won't be able to push code on GitHub. You must call 'git pull' before pushing your code.

    git pull origin master --allow-unrelated-histories

Commit your code

  • Finally, push all changes on GitHub

    git push -u origin master

Note: Now Github uses "main" as the default branch. If your project use "main" instead of "master simply replace "master" with "main" from the above commands

like image 34
Hitesh Sahu Avatar answered Sep 25 '22 08:09

Hitesh Sahu