Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Git First Commands
2.1.
git fork
2.2.
git init
2.3.
git add
2.4.
git config
3.
Frequently Asked Questions
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Git First Commands

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Among all distributed version control systems, Git is the most popular. Those who work on Git projects can install their repository copy locally. This capability allows collaboration between multiple contributors.

It's essential to set up Git when you first start using it. Once the setup is done on your computer, you can keep using it.

We will discuss various git First Commands in this article.

Click here to know more, wget command in linux

Git First Commands

Let us go through all of Git First commands that will make our open-source development lifecycle easier.

git fork

In Git, a fork refers to the process of a new owner taking over a repository and disconnecting the source code from previous developers. Developers often fork projects when they feel dissatisfied with the direction of the original project and want their work to remain independent. Git forks limit the ability of previous contributors to contribute to the forked repository, either by providing developers with the URL of the forked repository or by giving direct access through tools such as GitHub permissions.

First, search for the project repository you want to make changes to.

 

When you open that project and try to add a new file, you will get a warning that says, "You don't have write access." So here comes the fork command in the upper right corner.

 

After clicking on the fork, a loading screen showing Forking "project" will show a loading screen. 

 

Now, copy the URL by clicking on the Code option.

 

Then open the Git Bash.

 

To make changes in the file, you need to clone the repository by using the command:

$ git clone https://github.com/Luxmi26/example.git 

And here, you can see your local clone has been created.

 

Go into the new example directory, and write "$ cd example".

 

Let's see how we can sync this repository to the original repository.

Go to the original project on Github.

To know where our current fork is, use the command "$ git remote -v."

 

Next, we will add an upstream repository using

$ git remote add upstream https://github.com/Luxmi26/example.git.

Type git remote -v again to verify the upstream repository you specified for your fork. If your fork is the origin and the original repository is upstream, then the URLs for the two should be the same.

git init

To work with git on a new project, you'll need to set up a local repository. When you run the init command, you will find all of the configuration files you need to work with git in a folder named .git/. That's what git init does. A new Git repository is created and initialized with the git init command. Let's see how we can use this command. 

Suppose we have a folder named Example having a text file tutorial.txt.

 

To set a local repository on your computer, you first need to open the git Bash and get inside the directory Example using the command "$ cd directory_location."

 

So, when we check the status of the directory using "$ git status," It shows that any repository is not, by default, a git repository. Still, we can convert it into a git repository using the command git init.

 

Therefore, writing the command ", $ git init" will initialize an empty git repository.

 

Again if we check the status of the Example repository, it shows that now we are on the master branch, which means we have an empty git repository.

git add

Adding changes to the next commit is indicated by "add". By using git-add, the contents of files can be indexed for commits. Git's staging area contains changes that can be placed in a new revision using the "git commit" command.

From the above scenario of the git init command, we have created an empty git repository, and when we run the command git status, it shows that we have some untracked files.

 

So, to track the tutorial.txt file, we need to use the command "$ git add tutorial.txt".

 

Now, if we check the status of the Example directory, it will no longer show that we have untracked files.

Some extra git add functionalities are: 

  • git add - A: All changes happen at once.
  • git add: The current directory and its subdirectories are staged with new files and modifications without deletions.
  • git add -u: Deletes or modifies old files but does not create new ones.

git config

The git config command sets configuration values. This command will enable your computer to create your Git name and email before you begin working with Git. This command modifies git configuration files. Information such as your username, default editor, and email is stored in these files.

There is a file named .gitconfig. To see the content of the .gitconfig file, write the command "$ cat .gitconfig". This will show the global content of the file.

 

Now let's make a directory using "$ mkdir test" and then list the content of the test directory and move it into the .git folder. Inside the .git folder, you can see a file named config. To see the content inside the file, we have used "$ cat config," which only shows the local data to this repository.

 

Now, go back to the root directory and type "$ git config –global -l" this will list the globally present content in the computer. 

 

Let's see what else we can do with the git config command:

  • We can remove any value from the file using the command

$git config –global –unset user.name

 

  • Adding the value using the command

$git config –global user.name "value"
 

 

  • Git aliases are here to save the day if you are tired of repeating the same commands. You can use them to create shortcuts for common commands.
     

Let's write a git alias that calls the git commit command:

git config --global alias.com commit

Every time we run git com, the git commit command will be run.

 

Check out most important Git Interview Questions here.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

  1.  How do forks and clones differ in Git?
    The fork creates a separate copy of the Git repository. The difference between cloning and forking is that clones make a linked copy that continues to synchronize with the target repository.
     
  2. Is it necessary to git init before cloning?
    It is usually used when setting up a new Git repository and already has code. You do not need git init to clone a project if you are cloning it.
     
  3. What does git add and git commit mean?
    git add creates a staging area for the modified versions of files in your working directory. With git commit, you make a permanent snapshot of your repository that contains a unique identifier based on everything in the staging area.
     
  4. How does git config -- list work?
    It will display all Git config properties within the varying scopes of all Git files. Another Git config scope may have overridden the value displayed.

Conclusion

In this blog, we have seen what are git First Commands and also the implementation for each command. You can read the following git blogs which will help you learn more about git.

Github Guide

Git Activity commands

Git Change commands 

We hope that this blog has helped you enhance your knowledge about git commands and if you would like to learn more, check out our articles on the CN Library. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass