If you are a programmer and want to apply for a development job or even UI/UX designer job having knowledge about git and git commands will add an extra benefit to your resume.
Before we div deep into git commands, let’s see what is git and why is it so popular.
Introduction to GIT
Git was developed by Linus Torvalds, yes the same person who developed the amazing Linux OS. Git is a modern open-source version control system and source code management system used to easily manage the files. A version control system means basically different instances of the same project.
Take for example you developed a project for a client and sent the final project to them for review. Now the client asks you to make some changes in the layout or any other details in the project. You agreed and made changes accordingly.
Once you finish the changes you send the final project again to your client. Now your client says that this one does not work the earlier one was better. Now you as a programmer have to go back and make the changes again(except if you knew git).
Here is where version control can help you out. You basically save a collection of files with different versions of the same project. Now if the client asks you to make any changes, you can perform the same without worrying.
Let’s take another example to understand exactly what is Git and why it is useful?
Let’s consider the idea that you and your friend are collaborating on a revolutionary project.
The Monopoly game is what you’ve chosen to do instead of just a calculator.
You know that a Monopoly game consists of tokens, cards, houses, hotels, a money pack, pieces, etc.
So, you and your friend decided to develop some functions and features individually. While some by doing pair programming.
You started developing houses functions while your friend started with hotels functions. You send each other your part of the code by email or other methods.
Things worked perfectly for some time. But after a while merging both codes, you realize the code isn’t functioning for some reason. You have searched all the code and haven’t found the problem.
Hence, both of you decided to roll back to the last version, but unfortunately, neither of you controlled the version of the project. Therefore, you couldn’t do a rollback.
This is where Git could have saved you time and effort.
Using Git, you can manage your system’s version. Because Git tracks changes in the project files, you can collaborate on those files with multiple people as all changes are tracked. Git tracks every type of file. Additionally, since you keep all the history of your files, you can roll back to a specific part of your project (usually, the last part that worked).
In conclusion, Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. Git is used by many to develop open-source projects and some also use it internally in a company.
Also, a version control system helps to easily recover lost data, know who modified which code at what time, what change made this bug issue, etc.
Version control systems fall into two categories:
Centralized and Distributed.
Using a centralized system, all team members can share their changes with others and get the latest copy of the code from a central server.
CVS, Subversion (or SVN), and Perforce are examples of centralized version control systems.
With a centralized architecture, if the server goes down, we cannot collaborate or save snapshots of our projects, so we have to wait until it comes back online.
Distributed systems, however, do not pose these problems since every team member has a copy of the project with its history so that we can save snapshots of our project locally. During an outage of the central server, we can synchronize directly with others.
The most popular distributed version control systems are Git and Mercurial. Git is the most popular one.
Git is popular due to its branching capabilities. Unlike other centralized version control systems, git manages different branches for each change. So every person managing and making changes to the code has a copy of every change made.
Git helps you synchronize code between multiple developers. So, there is a Master repository that merges different codes from different developers of the same project. Here it saves you time from mailing each other different copies and updating each code one by one.
Different ways to use Git
Here are a few ways to use Git.
The first is the most common method, using Git through the Terminal or Command line. It’s the fastest and preferred method of using Git, as it gives more control over the workflow compared to other G U I or code editors.
The second method is through a Graphical user interface. Graphical User Interface clients are tools that provide alternative visualization for Git. Some of the popular Git G U I are Source Tree, GitKraken, and Github desktop. Sourcetree simplifies how you interact with your Git repositories so you can focus on coding. It’s available for Windows and macOS. While Gitkraken is a truly cross-platform app as it’s available for Windows, macOS, and Linux. Gitkraken is free for personal projects and charges a fee for commercial projects. Github Desktop is another free G U I tool that simplifies your git workflow. It’s free and available for Windows and macOS.
The third method to use Git is through code editors and I D Es. Modern code editors like VS Code, Atom have built-in support for Git. You can also install additional extensions to the code editors to bring in extra Git features.
Now that we know about different ways to use Git, let’s look at different hosting services for git repositories. Some of the popular Git repository hosting services are Github, Gitlab, and Bitbasket. Github being the most known and used among the others. While we will be looking at GitHub along with some git commands in the latter part of this video series, it is necessary that you understand what it represents.
Git Repository
A Git repository is nothing but a collection of source code. Git easily makes changes to the code and makes sure you are working on an updated original copy of the code.
Let’s make this easier. A git repository is a place where files and folders are stored.
As we know git is a version controls system(VCS), now there has to be a place where this needs to be stored. That specific place where VCS creates and stores these different versions are termed a Repository.
Now Git repository can be of two types one is a Bare repository and the other is a Non-bare repository.
Bare Repository: A bare repository is created without a working tree. They are supposed to be used on a server for sharing changes coming from different developers. Such repositories do not allow the user to modify local files and to create new versions for the repository based on these modifications.
Non-bare / Regular Repository: It is the default git repository. This allows to create new changes new changes by modification of files or to create new versions in the repository.
A working tree in a Git Repository is the collection of files which are originated from a certain version of the repository. Every Git repository has a working tree. The files in the working tree may be new or based on a certain version from the repository. The user can change and create files or delete them.
Let’s learn what is working tree is in Git. There are basic four elements in a git working tree.
- Working Area: It’s a place where you work with files and make changes to them.
- Staging Area: Here you are ready to add the changes you’ve made, but not yet committed
- Local Repository: In this, you commit a new version of the change to your local repository.
- Remote Repository: In this stage, you push the changes made to a remote server for eg Github
To add a file from the Working directory to the Staging area, we use the git command Git Add. Next, we use the git commit command used to add all files that are staged to the local repository. In the next step, git push is a command used to add all committed files in the local repository to the remote repository. As a result, anyone with access to the remote repository will be able to see all files and changes.
Now, to get files directly from a remote repository to a working area, we use the Git pull command. Alternatively, we can use Git fetch and git merge to pull files from the remote repository.
Using git fetch, files from the remote repository can be transferred to the local repository, but not into the working directory.
git merge is a command used to get the files from the local repository into the working directory.
As of now, we have an understanding of git terminology and git workflow. In the next video, we will take you through the installation process and share how to use git commands and an overview of GitHub.
Basic Git Commands
Now that we have a basic idea about what Git is let’s take a look at Git commands. First, let’s look at how we can add changes and send files to the repository.
Git Add
git add
is used to add files from the working area to the staging area.
Example:
git add project_git
Git Commit
git commit
is used to commit or add files to the local repository (.git
file in the project) from the staging area. Here -m
code adds the files you have staged for commit and -a
code will commit any files you’ve added with the git add command and also commits any files you’ve changed since then.
Example:
git commit -m “[ Type in the what changes you made here]”
git commit -a
Git Push
git push
will add files from your local repository to your remote (main) repository.
Example 1:
The following command will send the committed changes of the master branch to your remote repository
git push [variable] master
Example 2:
The following command will send branch commits to your remote repository
git push [name][branch]
Example 3:
The following command pushes all branches to your remote repository.
git push –all [name]
Above were Git commands that are used to commit your changes to the repository. Now let look at how can we pull changes from the repository.
This helps when a team works on a particular project. Everyone pushes the changes they have made to the remote repository. Now if you want to use those changes you need those modified files in your repository. Follow the below commands to do the same.
Git Pull
git
pull
the command gets the files from the remote repository to your working space.
Example:
git pull [Repository]
Git Fetch
git fetch
is the same as git pull
but works it only gets the files from the main repository to the local repository and not directly to your working space. It is useful to allow for checking before doing an actual pull, which could change files in your current branch and working copy.
Example:
git fetch [branch]
Git Merge
git merge
is used if you have to merge files from the local repository to your working space.
Example:
git merge [branch name]
Let’s see other basic git commands that you should be aware of.
Git Config
git config
is used to set the author name and email address respectively to be used with your commits.
Example:
git config –global user.name “[name]”
git config –global user.email “[email address]”
Git init
git init
is used to start a new repository.
Example:
git init [repository name]
Git clone
git clone [url]
helps to obtain and copy a repository from the existing URL.
Git Diff
git diff
shows the file differences which are not yet staged.git diff -stagged
shows the differences between the files in the staging area and the latest version present.- git diff [first branch] [second branch] shows the difference between the two branches mentioned.
Git Reset
git reset [file]
Removes from the staging area, but leaves the working directory unchanged. This upstages a file without overwriting any changes.git reset [commit]
undoes all the commits after the specified commit and preserves the changes locally.git reset –hard [commit]
command discards all history and goes back to the specified commit… Deletes uncommitted changes, and all commits after.
Git Status
git status
command lists all the files that have to be committed i.e staged, unstaged, and untracked.
Git RM
git rm [file]
the command deletes the file from your working directory and stages the deletion.
Git Log
git log
displays the entire version history for the current branch.git log –follow[file]
displays version history for specific filegit log --stat
Include which files were altered and the relative number of lines that were added or deleted from each of them.
Git Branch
git branch
command lists all the local branches in the current repository.git branch [branch name]
creates a new branch.git branch -d [branch name]
deletes the feature branch.git checkout [branch name]
is used to switch from one branch to another.git checkout -b [branch name]
creates a new branch and also switches to it.git merge [branch name]
Merges specified branch history into the current branch
Git Remote
git remote add [variable name] [URL]
is used to connect your local repository to the remote server.
Above were some basic and important git commands that will make your work with git easier.
Do let us know any other git commands that we should add to the list. Share this blog with your colleagues to help them out.