Name: Dhanraj B. Ranvirkar Topic: Basics of Git
Name: Dhanraj B. Ranvirkar Topic: Basics of Git
1 – git init
The git init command is the first command that you will run on Git. The git init command
is used to create a new blank repository. It is used to make an existing project as a Git
project. Several Git commands run inside the repository, but init command can be run
outside of the repository.
The git init command creates a .git subdirectory in the current working directory. This
newly created subdirectory contains all of the necessary metadata. These metadata can
be categorized into objects, refs, and temp files. It also initializes a HEAD pointer for
the master branch of the repository.
Command: git init
2 – git add
The git add command is used to add file contents to the Index (Staging Area).This
command updates the current content of the working tree to the staging area. It also
prepares the staged content for the next commit. Every time we add or update any file
in our project, it is required to forward updates to the staging area.
The git add command is a core part of Git technology. It typically adds one file at a time,
but there some options are available that can add more than one file at once.
The "index" contains a snapshot of the working tree data. This snapshot will be
forwarded for the next commit.
The git add command can be run many times before making a commit. These all add
operations can be put under one commit. The add command adds the files that are
specified on command line.
The git add command does not add the .gitignore file by default. In fact, we can ignore
the files by this command.
Command: git add $(filename)
Page 1|7
Basics of Git
3 – git commit
It is used to record the changes in the repository. It is the next command after the git
add. Every commit contains the index data and the commit message. Every commit
forms a parent-child relationship. When we add a file in Git, it will take place in the
staging area. A commit command is used to fetch updates from the staging area to the
repository.
The staging and committing are co-related to each other. Staging allows us to continue
in making changes to the repository, and when we want to share these changes to the
version control system, committing allows us to record these changes.
Commits are the snapshots of the project. Every commit is recorded in the master branch
of the repository. We can recall the commits or revert it to the older version. Two
different commits will never overwrite because each commit has its own commit-id.
This commit-id is a cryptographic number created by SHA (Secure Hash
Algorithm) algorithm.
Command: git commit
4 – git clone
In Git, cloning is the act of making a copy of any target repository. The target repository
can be remote or local. You can clone your repository from the remote repository to
create a local copy on your system.
The git clone is a command-line utility which is used to make a local copy of a remote
repository. It accesses the repository through a remote URL.
Command: git clone $(repository link)
Page 2|7
Basics of Git
5 – git stash
Sometimes we want to switch the branches, but we are working on an incomplete part
of your current project. we don't want to make a commit of half-done work. Git stashing
allows us to do so. The git stash command enables you to switch branches without
committing the current branch.
Generally, the stash's meaning is "store something safely in a hidden place." The sense
in Git is also the same for stash; Git temporarily saves your data safely without
committing.
Stashing takes the messy state of your working directory, and temporarily save it for
further use.
Command: git stash
Page 3|7
Basics of Git
6 – git ignore
In Git, the term "ignore" is used to specify intentionally untracked files that Git should
ignore. It doesn't affect the Files that already tracked by Git.
Generally, the Ignored files are artifacts and machine-generated files. These files can be
derived from your repository source or should otherwise not be committed.
Git ignore files is a file that can be any file or a folder that contains all the files that we
want to ignore. The developers ignore files that are not necessary to execute the project.
Git itself creates many system-generated ignored files. Usually, these files are hidden
files. There are several ways to specify the ignore files. The ignored files can be tracked
on a .gitignore file that is placed on the root folder of the repository. No explicit
command is used to ignore the file.
There is no explicit git ignore command; instead, the .gitignore file must be edited and
committed by hand when you have new files that you wish to ignore. The .gitignore files
hold patterns that are matched against file names in your repository to determine
whether or not they should be ignored.
There is no command in Git to ignore files; alternatively, there are several ways to
specify the ignore files in git. One of the most common ways is the .gitignore file.
Steps to create .gitignore file in linux
Step – 1
cat > .gitignore
*.txt
/newfolder/*
Step – 2
Press Clt + D
Step – 3
git add .gitignore
git commit -m "ignored directory created."
Page 4|7
Basics of Git
Page 5|7
Basics of Git
8 – git remote
In Git, the term remote is concerned with the remote repository. It is a shared repository
that all team members use to exchange their changes. A remote repository is stored on
a code hosting service like an internal server, GitHub, Subversion, and more. In the case
of a local repository, a remote typically does not provide a file tree of the project's
current state; as an alternative, it only consists of the .git versioning data.
To check the configuration of the remote server, run the git remote command. The git
remote command allows accessing the connection between remote and local.
Command: git remote (want to see the original existence of your cloned repository)
9 – git checkout
In Git, the term checkout is used for the act of switching between different versions of
a target entity. The git checkout command is used to switch between branches in a
repository.
The git checkout command operates upon three different entities which are files,
commits, and branches. Sometimes this command can be dangerous because there is no
undo option available on this command.
It checks the branches and updates the files in the working directory to match the version
already available in that branch, and it forwards the updates to Git to save all new
commit in that branch.
Command: git checkout $(branch_name)
Page 6|7
Basics of Git
10 – git pull
The term pull is used to receive data from GitHub. It fetches and merges changes from
the remote server to your working directory. The git pull command is used to pull a
repository.
Pull request is a process for a developer to notify team members that they have
completed a feature. Once their feature branch is ready, the developer files a pull request
via their remote server account. Pull request announces all the team members that they
need to review the code and merge it into the master branch.
The pull command is used to access the changes (commits)from a remote repository to
the local repository. It updates the local branches with the remote-tracking branches.
Remote tracking branches are branches that have been set up to push and pull from the
remote repository. Generally, it is a collection of the fetch and merges command. First,
it fetches the changes from remote and combined them with the local repository.
Command: git pull (repository link)
11 – git push
The push term refers to upload local repository content to a remote repository. Pushing
is an act of transfer commits from your local repository to a remote repository. Pushing
is capable of overwriting changes; caution should be taken when pushing.
Moreover, we can say the push updates the remote refs with local refs. Every time you
push into the repository, it is updated with some interesting changes that you made. If
we do not specify the location of a repository, then it will push to default location
at origin master.
The "git push" command is used to push into the repository. The push command can be
considered as a tool to transfer commits between local and remote repositories.
Git push origin master is a special command-line utility that specifies the remote branch
and directory. When you have multiple branches and directory, then this command
assists you in determining your main branch and repository.
Generally, the term origin stands for the remote repository, and master is considered as
the main branch. So, the entire statement "git push origin master" pushed the local
content on the master branch of the remote location.
Command: git push origin master
Page 7|7