Git is an open-source VCS project. It was established by Linux Torvalds in 2005. An immense amount of software projects depend on Git for version control.
GIT workflow is a very popularly used VCS (Version Control System) in the tech world. It aids you to keep a check on the changes carried out in a file.
Git can be easily used by everyone. You can avail of it on Windows, Linux, Solaris, and Mac. For a better understanding, let us tell you the important terms related to Git!
Version Control System: Version Control Systems basically helps various developers, team members, as well as designers to work on the same project at the same time.
Commonly known as VCS, such systems guarantee that every team member can access the latest code. The development process is a bit complex and hence more people are required to manage various versions of all the products.
Therefore, it becomes feasible for the entire team to have access to the code at the same time in order to carry out a parallel and seamless working environment.
Some popular types of VCS are:
- Helix Core
Control System: A control system is basically a system that manages or directs other systems to reach a particular result. In short, a system that usually controls or regulates other systems is known as a control system.
Distributed Version Control System: There are several repositories in Git. It has a remote repository on the server, whereas the local repository is stored on every developer’s computer system.
Therefore, a distributed version control system simply implies that the code is available or is distributed on each member’s system!
Why is Git needed?
As you have read above that the version control system helps various developers to work together at the same time. Therefore, it becomes extremely easy for the developers who work on real-life projects to work in parallel.
Moreover, in such projects, version control systems help to go to the older version of the code too. The developers avail a huge benefit from this feature.
Now, after understanding the basics, let us head towards some Git commands.
To create a local repository
In order to create a new repository in a new directory, with a specific project name, follow this command:
git init [project name]
A new project adds up to your local Git repository.
To stage a file
If you want to stage a file, use this command:
git add test.txt
For creating multiple files, simply write the following command:
git add filename1 filename2 filename3
Next up, if you want to club up all the files of your project folder to the staging area, use this command:
git add .
git clone: This command creates a copy of your current repository. Now, to access the repository that is on a remote server, follow this command:
git clone username@host:/path/to/repository
If you seek to copy the local repository, type this command:
git clone /path/to/repository
git commit: This command takes a snapshot of whatever changes you make. After that, the changes are saved in the local git directory.
git commit –m “Hello world”.
Remember that the committed changes do not store in the remote repository.
git status: This command helps to display a list of all the modifications and changes made in the files. Moreover, you also get to know which files are present in the staging area.
Use the below-stated command:
If you want to print all the commits that have taken place, then follow this command:
This command presents you with the name of the author, date, and also the commit message.
A branch is basically a pointer to the latest commit of a local git repository.
To create a new branch abc, simply follow:
git branch abc
Now, this command generates a new branch and simultaneously switches you to it. Take a look:
git checkout -b <branchname>
Now, in order to switch between branches, use:
git checkout <branch-name>
Moving on, to make a list of all the branches, use the following command:
git config: If you want to set some user-specific values, for instance, username, email, file format, etc.
Here is a command to set up an email:
git config --global user.email firstname.lastname@example.org
The global flag depicts that you can use the mentioned email for all the local repositories.
Additionally, for different repositories, you can set different emails, to do that follow this command:
git config --local user.email email@example.com
git remote: This command allows you to view all the remote repositories.
git remote -v
In order to establish a connection between the remote server and your local repository, then follow this command:
git remote add origin <host-or-remoteURL>
Further, to delete the connection with the remote repository, use the below-stated command:
git remote rm <name-of-the-repository>
git push: If you want to push the code from a local repository to the remote repository’s master branch, type the following command:
git push -u origin master
git pull: This command helps you to pull all the latest changes into the local repository from a remote repository.
As the code of the remote repository keeps on getting updated by the developers of a team, git pull becomes important.
git diff: This command helps you to jot down all the conflicts that originated against the base file. Take a look:
git diff --base <file-name>
In between the branches, if there are any conflicts, you can view them before merging. Use the below-given command:
git diff <source-branch> <target-branch>
Additionally, to view any current conflicts, use the following command:
git reset: If you want to reset your working directory or the index, follow this command:
git reset --hard HEAD
git stash: If some changes can not be committed, then use this command to temporarily save them:
git rm: If you want to delete some files from the working directory as well as the index, then use this command:
git rm filename.txt
git tag: If you want to display all the tags in alphabetical order, type:
If you are seeking some elaborate information about a tag, then follow this command:
git show v1.4
Furthermore, if you want to tag a previous commit, mention the commit ID as stated below in the command:
git tag -a v1.2 9fceb02
git show: If you want any information about a git object, simply type this command:
git grep: By using this command, you can search your tracked files, working directory, multiple items in a single file or staging area for some specific words or phrases.
For instance, if you want to search a particular email id, say, www.abc.com, then simply use this command:
git grep www.abc.com
git fetch: This command helps you to download files or objects from a remote repository to your own local repository. Below stated is the syntax:
git fetch origin
This command helps you to instantly browse the working repository in git-web.
Have a look:
git instaweb –httpd=webrick
gitk: If you are looking to view the local repository’s graphical interface, then follow this command :
git rebase: This command is used to implement any kind of changes from one branch to another. Here it is:
git rebase master
git gc: You can remove the files that are not necessary to optimise your local repository. Type the following:
Bravo! Now you have attained knowledge about all the basic commands of Git. If you are a developer, then retaining all these commands will prove to be extremely beneficial for you.
We hope that this article helped you to gain knowledge and solve your ambiguities about the Git commands. Keep practising and keep learning!