git init / git clone / git config
In the previous blog, we’ve seen how to install git on various operating systems. In this blog, let’s look on how to setup git repository in your system. Here I’ll walk you through initializing a Git repository for a new or existing project and cloning an existing repository. We will also look into committing and modifying changes in the cloned repository. What are you waiting for? Let’s rock!!!
In this tutorial we are going to see:
- Initializing a new git repository.
- Cloning an existing git repository.
- Committing a modified version of a repository in the file.
- Configuring a git repository for remote collaboration.
- Some commonly used Git commands.
What can you learn by the end of this article? You will be able to create your own repository, use some common Git commands. You will also know how to view your project history and configure connection to a git hosting service (Bit bucket)
So, what is a git repository? It is a place where you can store all of your source code versions and can look into it when you needed. Let’s look into the initialization process.
Initializing a new Git Repo : Git init
We’ll use git-init (one time command) to initialize new git repository. Enter the command and hit enter. This will create a new .git sub directory under the current working directory. This will also create a new main branch.
Versioning an existing project with a new git repository
Let’s consider you already have a project, and now we are going to create a repo within the project folder. At first, cd to the root folder and then initiate git init command.
cd /path/to/your/existing/code git init
Cloning an existing git repository
Clone is more or less likely to create the same code or project. We can also say it as copying the exact same code the repo has.
git clone <repo URL>
Saving Changes to the repository : git add and git commit
We’ve cloned the repository using the above command. Now, we are going to add and commit changes to it.
cd /path/to/project echo "test content for git tutorial" >> CommitTest.txt git add CommitTest.txt git commit -m "added CommitTest.txt to the repo"
After executing the above code, your repo will now have
CommitTest.txt added to the history and will track future updates to the file.
Now, we are going to configure the remote repository, and let’s set a local branch for the repositories.
git remote add <remote_name> <remote_repo_url>
This command will map the repository or we can say it as adding reference. Once we’ve done that, we can push the local branches to it.
git push -u <remote_name> <local_branch_name>
By using the above command, we can create as many local branches we want. I hope I’ve covered enough for this tutorial. I hope you like this article, make sure to share it with your friends and colleagues. If you have any doubts, leave it in the comments section and its my pleasure to say that me and my team are happy to help you anytime. If you are facing any problem while going through this article, reach us in the comments section. Happy Learning.