Git — Commands And Operations In Git
What is Git?
Git lets you easily keep track of every revision you and your team make during the development of your software. You all share one repository of code that is worked on independently and then merged back together. You also do not need to be connected all the time because the project is both saved locally on each machine and remotely (probably at Github).
Git is special (and pretty much mandatory) because you can revert back to any previous version of your code, branch off and develop a specific feature without affecting anything or anyone else, and maintain safety from data corruption because of its distributed nature.
Required Vocabulary
When you’re first starting to use Git, it can be intimidating. Commands and concepts may seem totally foreign if you’ve never used any form of version control in the past. You will pick them up super fast, though.
Repository:
Often referred to as a repo. A directory that contains your project work, as well as a few files. which are used to communicate with Git. Repositories can exist either locally on your computer or as a remote copy on another computer.
Github:
The most popular remote storage solution for git repos. It also allows you to set access permissions for projects, track and submit bugs, accept feature requests, subscribe to repository notifications, and utilize a graphic interface, rather than use the command line. Repos default to public, but paid accounts can have private ones.
Commit:
Git thinks of its data as a set of snapshots of a mini file system. Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.
Push:
Pushing is the bread to committing butter. Committing throws your files into the time capsule, and pushing is what launches the capsule into space. Pushing is essentially syncing your commits to the cloud (again, probably Github). You can push multiple commits at once, too. You can work offline, commit lots of work, and then push it all up to Github when you’re back in civilization with that sweet, sweet wifi.
Branch:
You can think of your git repo as a tree. The trunk of the tree, the software that goes live, is called the Master Branch. That’s the one that goes live. The branches of that tree are, well, called branches. These are separate instances of the code that offshoots from the main codebase. You might branch off for a single feature or an experimental patch. By branching, you can preserve the integrity of the software and have a way to revert if you do something totally bonkers. It also allows you to work on your task without stumbling into your team’s way (or they into yours).
Staging Area or Staging Index or Index:
A file in the Git directory that stores information about what will go into your next commit. You can think of the staging area as a prep table where Git will take the next commit. Files on the Staging Index are poised to be added to the repository.
Merge:
When a branch is polished up, free of bugs (as far as you can tell, at least), and ready to become part of the primary codebase, it will get merged into the master branch. Merging is just what it sounds like: integrating two branches together. Any new or updated code will become an official part of the codebase. Anyone who branches off from the point of merging will have this code in their branch as well.
Clone:
Cloning a repo is pretty much exactly what it sounds like. It takes the entire online repository and makes an exact copy of it on your local machine. You will need to do this for any number of reasons, not the least of which are starting in the middle of a project with a new team, swapping workstations, or starting over from a corrupted repo.
Working Directory:
The files that you see in your computer’s file system. When you open your project files up on a code editor, you’re working with files in the Working Directory. This is in contrast to the files that have been saved (in commits!) in the repository. When working with Git, the Working Directory is also different from the command line’s concept of the current working directory which is the directory that your shell is “looking at” right now
Getting Started and Required Commands:
How to install Git:
Git (probably) didn’t come installed on your computer, so we have to get it there. Luckily, installing Git is super easy, whether you’re on Linux, Mac, or Windows.
For Linux, open the terminal and type Sudo apt-get install git-all (if you’re in Ubuntu. If not, there is a list of all the Linux package installation commands for whichever other distro you’re on.)
On Windows, it’s just as simple. You download the installer and run it.
On Mac, you just need to open up the terminal and type git. If git is not installed
Git Tutorial — A few Operations & Commands
Some of the basic operations in Git are:
- Initialize
- Add
- Commit
- Pull
- Push
Some advanced Git operations are:
- Branching
- Merging
- Rebasing
Let me first give you a brief idea about how these operations work with the Git repositories. Take a look at the architecture of Git below:
If you understand the above diagram well and good, but if you don’t, you need not worry, I will be explaining these operations in this Git Tutorial one by one. Let us begin with the basic operations.
Initialize
In order to do that, we use the command git init. Please refer to the below screenshot.
$ git initgit init creates an empty Git repository or re-initializes an existing one. It basically creates a .git directory with subdirectories and template files. Running a git init in an existing repository will not overwrite things that are already there. It rather picks up the newly added templates.
Git status
The git status command lists all the modified files which are ready to be added to the local repository.
$ git statusThis shows that I have two files which are not added to the index yet. This means I cannot commit changes with these files unless I have added them explicitly in the index.
Add
This command updates the index using the current content found in the working tree and then prepares the content in the staging area for the next commit. For that, use the commands below
$ git add <directory>or
$ git add <file>To add all files or directories at the same time
$ git add -ACommit
It refers to recording snapshots of the repository at a given time. Committed snapshots will never change unless done explicitly. Let me explain how commit works with the diagram below:
You can commit by using the command below:
$ git commitThis will commit the staged snapshot and will launch a text editor for a commit message.
Get Yash sony’s stories in your inbox
Join Medium for free to get updates from this writer.
Or you can use:
$ git commit -m "<message>"Pull
The git pull command fetches changes from a remote repository to a local repository. It merges upstream changes in your local repository, which is a common task in Git-based collaborations.
$ git pullBut first, you need to set your central repository as origin using the command:
$ git remote add origin <link of your central repository>Now that my origin is set, let us extract files from the origin using pull. For that use the command:
$ git pull origin masterThis command will copy all the files from the master branch of the remote repository to your local repository.
Push
This command transfer commits from your local repository to your remote repository. It is the opposite of pull operation.
you can then push them to the central repository by using the following command:
$ git push <remote>Branching and Merging
The final two commands you’ll need to be familiar with deal with branching and merging. Working on a branch keeps the main codebase safe,
$ git branch <branch-name>And once your changes are all dandy, you can merge it all back together.
They can be a little tricky, so it may just take some prodding to get them to play nice.
- git checkout -b “branch1” — This command will both create a new branch that you title and swap you over to it as your working branch. It combines both the git checkout and git branch commands into one handy-dandy line of code.
- touch awesome.html — this is just a filler command to create a new html file that I will merge back into the master.
- And like before, you will git status, git add awesome.html, git commit -m “create awesome.html”
- git status again will let you see that everything is as it should be.
You can switch back to the master branch with,
$ git checkout <branch-name>To merge branches, use
$ git merge <branch-name>Log History
Git also maintains a history log of all your commits. You can see them by using the command,
$ git logThis command is very powerful and comes with a lot of options with Unix shell. See ‘git log — help’. It stores all the information related to committing hash, author of the commit, date, etc., and much more
This stuff happens to be the most basic and lesser of the crazier stuff that git has in store. To learn more about git, I recommend a free course offered by Udacity. Also check out the official git document reference.
— Thank you for reading my blog —






