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.
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.
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.
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.
Lets learn what are working tree 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
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 is used to add files from the working area to the staging area.
git add project_git
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.
git commit -m “[ Type in the what changes you made here]” git commit -a
git push will add files from your local repository to your remote (main) repository.
The following command will send the committed changes of the master branch to your remote repository
git push [variable] master
The following command will send branch commits to your remote repository
git push [name][branch]
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.
pull the command gets the files from the remote repository to your working space.
git pull [Repository]
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.
git fetch [branch]
git merge is used if you have to merge files from the local repository to your working space.
git merge [branch name]
Lets see other basic git commands that you should be aware of.
git config is used to set the author name and email address respectively to be used with your commits.
git config –global user.name “[name]” git config –global user.email “[email address]”
git init is used to start a new repository.
git init [repository name]
git clone [url] helps to obtain and copy a repository from existing URL.
git diffshows the file differences which are not yet staged.
git diff -staggedshows 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 [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 command lists all the files that have to be committed i.e staged, unstaged, and untracked.
git rm [file] command deletes the file from your working directory and stages the deletion.
git logdisplays the entire version history for the current branch.
git log –follow[file]displays version history for specific file
git log --statInclude which files were altered and the relative number of lines that were added or deleted from each of them.
git branchcommand 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 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.