What is GIT?
Git is a distributed version control tool that is for maintaining historical and current versions of source code, web pages, etc Version Control System (VCS). It is most widely used by companies and programmers to collaborate on developing software and applications.
A GIT project mainly comprises three major sections:
- the working directory,
- the staging area,
- and the git directory.
The working directory allows adding, deleting, and editing the files. The changes are staged (indexed) in the staging area. After changes have been done, the snapshot of the changes is saved into the git directory.
Basic GIT Commands
Here are some basic GIT commands you need to know:
♠ git init cerate’s a new local GIT repository. The following Git command is used to create a repository in the current directory:
git init
♠ To create a repository within a new directory by specifying the project name:
git init [project name]
♠ git clone creates a copied repository. If the repository lies on a remote server, use:
git clone username@host:/path/to/repository
♠ To copy a local repository copy the following command:
git clone /path/to/repository
♠ git add command adds files to the staging area. For instance, the basic Git following command will index the temp.txt file:
git add <temp.txt>
♠ git commit creates a snapshot of the changes and saves it to the git directory.
git commit –m “Message to go with the commit here”
♠ git config is used to set user-specific configuration values such as email, username, file format, etc. The command for setting up an email will look like this:
git config --global user.email youremail@example.com
♠ The –global flag describes GIT about using the particular email for all local repositories. To use different emails for different repositories, use the command below:
git config --local user.email youremail@example.com
♠ git status shows the list of changed files together with the files that are to be staged or committed.
git status
♠ git push sends local commits to the master branch of the remote repository. The code structure is:
git push origin <master>
♠ git checkout generates branches and allows to navigate between them. For instance, let’s use a basic command to create a new branch and automatically switches to it:
command git checkout -b <branch-name>
♠ To switch from one branch to another, use:
git checkout <branch-name>
♠ git remote provides a view of all remote repositories. The following command lists all connections along with their URLs:
git remote –v
♠ To connect the local repository to a remote server, use the following command below:
git remote add origin <host-or-remoteURL>
♠ To delete a connection to a specified remote repository:
git remote rm <name-of-the-repository>
♠ git branch includes listing, creating, or deleting branches. For instance, to list all the branches present in the repository, the command should look like this:
git branch
♠ To delete a branch, use:
git branch –d <branch-name>
♠ git pull is used to merge all the changes present in the remote repository to the local working directory.
git pull
♠ git merge a branch into the active one.
git merge <branch-name>
♠ git diff lists down conflicts. To view conflicts against the base file, use
git diff --base <file-name>
♠ The following basic command is used to view the conflicts between branches before merging them:
git diff <source-branch> <target-branch>
♠ To list down all the present conflicts, use:
git diff
♠ git tag marks specific commits. Developers usually use it to mark release points like v1.0 and v2.0.
git tag <insert-commitID-here>
♠ git reset command resets the index and the working directory to the last git commit’s state.
git reset --hard HEAD
♠ git rm is used to remove files from the index and the working directory.
git rm filename.txt
♠ git stash command temporarily saves the changes that are not ready to be committed. It enables you to go back to that project later on.
git stash
♠ git show is used to view information about any git object.
git show
♠ git fetch enables users to fetch all objects from the remote repository that does not reside in the local working directory.
git fetch origin
♠ git ls-tree enables you to view a tree object along with the name, the mode of each item, and the blob’s SHA-1 value. Let’s say you want to see the HEAD, use:
git ls-tree HEAD
♠ git cat-file provides the type and the size information of a repository object. The -p option along with the objects is used SHA-1 value to view the information of a specific object, for example:
git cat-file –p d670460b4b4aece5915caf5c68d12f560a9fe3e4
♠ git grep allows the users to search through committed trees, working directory, and staging area for specific phrases and words. To search for www.hostinger.com in all files, use:
git grep "www.domain.com"
♠ gitk displays the graphical interface for a local repository. Simply run:
gitk
♠ git instaweb allows you to browse your local repository in the git-web interface. For instance:
git instaweb –httpd=webrick
♠ git gc cleans the unnecessary files and optimizes the local repository.
git gc
♠ git archive helps users to create a zip or a tar file containing the constituents of a single repository tree. For instance:
git archive --format=tar master
♠ git prune deletes objects that don’t consist of any incoming pointers.
git prune
♠ git fsck testifies an integrity check of the git file system and identifies any corrupted objects.
git fsck
♠ git rebase applies certain changes from one branch to another. For instance:
git rebase master
Learning basic GIT commands is efficient for developers as these commands provide the ease to control the projects’ source code.