Adding Git to an Existing Project

Git has been the version-control system of choice since its inception in 2005. About 87% of the developers use Git as their version-control system. Well, a version control system is a software tool that helps record changes to files by keeping a track of modifications done to the code. This has many advantages with team collaboration being one of the top reasons. But how do you add git to an existing project which you have already started before. Well, its very easy.

How to add Git to an existing project?

Follow along the below steps to add Git to your already created project.

  • Go to the terminal of your project directory
  • You need to initialize your project git using  git init
  • Create a .gitignore file and it is actually a text file that tells Git which files or folders to ignore in a project.
  • Stage your files using  git add .
  • Commit your changes with an appropriate commit message, for example: 
 git commit -m "my first commit"

How does it work really?

So you have an existing project and you want to push to a version control system, you would need to create a repository in GitHub (or any distributed version-control repository of your choice). 🙂

Create a GitHub repository

Once you have logged in to your GitHub repository, you have to navigate to your homepage and click on the + (plus) symbol and then to New repository.

New repository will create redirect to a page where you have to give a name to your repository

Once you are navigated to a different page, you would need to give a name to your repository. You would also have the option to make your repository as public (accessible to everyone) or private (only accessible to people, whom you have given permission). Once everything is confirmed, you can finally click on Create repository, which will create your repository.

After naming the repository, you can select the repository to be either public or private

Connecting your project code to GitHub repository

Once the repository is created, you would have to add the files from your local repository to the remote one on GitHub. This is essential for pushing your code to your remote repository.

You can copy your repository remote URL to link it with your local repository

You can use the below Git command to link your local repository with that of the remote:

git remote add <your-remote-name> <your-remote-url>

Hence, the command will become:

git remote add origin https://github.com/viveknaskar/new-repository.git

The git remote command allows Git to track remote repositories and connects local repositories to those remote ones. The main remote repository in Git is called origin.

Pushing your code to your GitHub repository

After you have linked your local repository to your GitHub one, you need push your local commits to your remote repository using git push command. The push command will update the remote repository if there are additional changes in your repository.

The git push requires two parameters: the name of the remote repository (origin) and the branch to push to (here master is the default branch for every repository).

git push origin master

Once you run this command, you will get the logs like below:

$ git push origin master

Counting objects: 3, done.
Writing objects: 100% (3/3), 212 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/viveknaskar/new-repository.git
 * [new branch]      master -> master

There you go! You have pushed your existing project to GitHub. 🤓

2 thoughts on “Adding Git to an Existing Project

  1. Quite helpful for all the newbies who are starting out with GIT and want to move their projects here.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close