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
- Create a
.gitignorefile 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.
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.
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 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
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.
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. 🤓