Version Control is a widely used aspect of modern software development. It enables the developers to keep track of the changes, and previous code changes, collaborate with other developers effectively, and revert the code to the old version, when required, and so on. Git is a distributed version control system, and it is heavily adopted for its effective and flexible usage. For many developers, pushing existing code to a new repository is painful. In this article, let's quickly touch on how to get started with the git to commit the first round of code and learn how to achieve the same.
![](https://static.wixstatic.com/media/051710_66bf3d6424b247978e6a713544c35a91~mv2.png/v1/fill/w_980,h_735,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/051710_66bf3d6424b247978e6a713544c35a91~mv2.png)
There are two approaches,
Clone the git project on your computer, then copy and paste the code into the folder and commit the changes. This approach is fairly simple and quick.
Another way is to use the remote git command in the terminal and push the code. This might look like a tedious approach, but once you get the understanding of it, this can be rather a quick way.
Let us have a step-by-step guide to learn both ways.
The first step is going to be common for either of the ways. Create a brand new repository on the git. There are different version controls including but not limited to GitHub, GitLab, Bitbucket, etc. Create the repository on one of the platforms. I am going to use GitHub as an example throughout this article.
If you don't have an account on GitHub, now is a good time to create one. After you sign in, on the top right corner of the website, you can see a + icon, click on the icon and code New Repository. You will see something like,
![](https://static.wixstatic.com/media/051710_58d4a884e52148f28649f4497ec7dbe5~mv2.png/v1/fill/w_980,h_502,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/051710_58d4a884e52148f28649f4497ec7dbe5~mv2.png)
Fill in the information and click on Create Repository.
1. Clone the Project
As mentioned earlier, this is a fairly straightforward method. Find the URL for cloning the project.
![](https://static.wixstatic.com/media/051710_944c605fff9e417589a43d68dd957356~mv2.png/v1/fill/w_980,h_510,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/051710_944c605fff9e417589a43d68dd957356~mv2.png)
Copy the URL. Now open the terminal and use the following command
1. git clone https://github.com/skd09/Test.git
2. cd Test
You can use this folder while creating a new project or paste the existing code into this folder. After you add the files, run the below commands in the terminal.
1. git add .
2. git commit -m "Intial Commit"
3. git push origin main
When to avoid using this method?
If your existing project has lots of files and code, in such case avoid using this approach. A lot of times, while you copy the files, chances are the project may break, I am sure that none of us like errors in the project.
2. Remote Git Command
There are a few commands which we use to push the existing code into this newly created repository. Some the these commands you have read in method one.
1. git init
2. git add .
3. git commit -m "Intial Commit"
4. git remote add origin https://github.com/skd09/Test.git
5. git push -f origin main
Let us break down and understand these commands.
1. Initialize a Git repository
To use the git version control, it is necessary to have a hidden git folder and other files to manage the version control. Use the following to introduce all the files related to git in your directory.
git init
2. Add Files to the Staging Area
Once the repository is initialized, the next step is to add all the files to the staging area. To achieve the same use the following command,
git add .
The dot (".") indicates the current directory.
3. Committing the changes
Now that you have pushed the files to the staging area, further, we have to commit them. Commit is like a snapshot of the changes we have done. We use the command.
git commit -m "Intial Commit"
The -m represents the message that you want to add to the commit. For instance, in the message, I have added the comment initial commit, since it is the first commit. The message is a hint of what changes were made, this approach is important when you collaborate.
4. Setting up the new Remote Repository
Since your code is committed, now is the time to refer to the remote server where the code will be preserved. This remote server will hold your remote repository, in our case it is our Test project. Use the below command.
git remote add origin https://github.com/skd09/Test.git
The origin is the URL. This URL points to the GitHub repository where you want to push your changes.
5. Pushing the changes to the Remote Repository
Here we are on our final step. The command to push everything from your staging area to the remote server is,
git push -f origin main
The main is the branch name, you can have a different branch name such as dev, master, stag, and so on.
The -f means force flag. We add the flag when we want to force commit our changes. This flag will forcefully overwrite the files if there are any. Thus, use this flag with caution. In our scenario since the repository is fresh, we need to force every minute change.
Comments