Git Tutorials – Git Commands
What is Git?
GIT is an open source Distributed Version Control System (DVCS) which records changes made in your project or a set of files laying emphasis on speed, data integrity and distributed, non-linear workflows.
Git is one of the best version control tools available in the present market. This emerging star was first developed by Linus Torvalds, the creator of the Linux kernel. There is no singular centralized code base that the code can be pulled from, and different branches are responsible for hosting different areas of the code.
Git is fast and efficient and is used by system administrators and open-source projects to power their repositories.
Goals of Git:
- Support for non-linear development
- Fully distributed
- Able to handle large projects efficiently
Each update in the code is recorded in Git’s history, which is stored in a data structure called a Git repository. This repository is the core of Git.
Feature branch workflow
Each branch function in an isolated environment while updates are made in the codebase. This way, its ensured that the master branch always has the production-quality code
- A developer calls a pull request to ask another developer to merge one of his/her branches into the other’s code repository
- Makes it easy for project leaders to monitor and track code changes
- Very popular, widely used, and accepted as standard version control system by the vast majority within the developer’s community.
- Large number of Git users make it easy to resolve issues and seek outside help using online forums.
There are three main states that your Git files can reside in:
- In your local repo (Committed)
- HEAD – last commit snapshot, next parent
- Checked-out and modified but not yet committed (working directory)
- Sandbox – contents unpacked into actual files
- Or, in-between, in a “staging” area
- Staged files are ready to be committed
- A commit saves a snapshot of the current state of staging area
- Index – proposed next commit snapshot
The remote repository (git directory) is the server where all the collaborators upload changes made to the file, it is where the work from all developers gets integrated.
Basic Git Workflow
- Modify the files in your working directory.
- Move the files to the staging area by staging them – adding snapshots of them to your staging area.
- Commit, which takes the files in the staging area and stores that snapshot permanently to your Git directory.
- Git generates a unique ID which is a 40 character string of hex digits for each commit and refers to the commits by this ID rather than the version number
Branching & Merging
- A branch in Git is lightweight and the pointer always points to one of the commits.
- The default branch name in Git is “master”.
- Each time a commit is made, the master branch pointer moves forward automatically.
Features Of Git
- Free and open source:
Git is released under GPL (General Public License) open source license. You don’t need to purchase Git. It is absolutely free. As its open source, the source code can be modified as per requirements.
Since there is no dependency on a network connection, the performance is really fast for all operations. Performance tests done by Mozilla have proved that its way faster than other version control systems. Fetching version history from a locally stored repository is much faster than fetching it from the remote server.
Git is quite scalable. Git can easily handle if the number of collaborators increase. The data stored on the client’s side is very small as Git compresses the huge data through a lossless compression technique.
Since every contributor has a local repository, if the system crashes, the lost data can be recovered from any of the local repository. There is always a backup of all files. The Git history is stored in a way that the version ID of a particular version (a commit in Git terms) depends upon the complete development history leading up to that commit.
Git includes specific tools for visualizing and navigating a non-linear development history and supports rapid branching and merging. A change will be merged more often that it is written, when its passed around various reviewers, is what Git assumes. Branches in Git are very lightweight. A branch in Git is just a reference to a particular commit. With the knowledge about the parental commits, the full branch structure can be constructed.
Branching is a very powerful feature of git. Branch management with Git is very simple. It takes just a few seconds to create, delete or merge branches. For every change to the codebase, feature branches provide an isolated environment. Whenever a developer wants to work on something, no matter what the size, he will always create a new branch. So, this way it is ensured that the master branch always contains production-quality code.
Git provides each developer a local copy of the complete development history, and changes are copied from one such repository to another. These changes are imported as additional development branches, and can be merged in the same manner as a local branch.
- Greater collaboration:
A major benefit of the Git workflow is increased collaboration. Git helps teams to talk to each other early and more frequently, enabling them to share feedback and integrate suggestions. The ease with which the Git repositories can be accessed, makes it simple for the users across the organization to collaborate and spot errors quickly, resulting in better and more stable code.
Git: A Giant Leap Toward DevOps
DevOps is about completely removing process bottlenecks, quicker feedback, shortening development cycle and improving overall software lifecycle.
A sensible way to start on the DevOps path is to learn how to use a version control system such as Git. Git is an integral part of DevOps.
Git plays an important role in managing the code that the collaborators commit to the shared repository. This code is then extracted to perform continuous integration, to create a build and test it on the test server and finally deploy it on the production.
Git enables communication between the development and operations team, commit messages play a vital role in information exchange. The deployable bits and pieces all lie in the Version Control system like Git.
Git alone might not be able to overcome the barriers between Dev and Ops but it surely provides some remarkable capabilities that make a big difference to the IT organizations, giving substantial benefits, like :
- They are about twice as likely to exceed profitability, market share and productivity goals.
- Their employees reported far higher levels of job satisfaction.
- They realize 50 percent higher market capitalization growth over a three-year period.
Git provides both the capabilities that characterize high performing IT organizations: version control of production artifacts and peer review of updates.
Let us now look at the Git Commands.
|$ git init [project-name]||Creates a new local repository with the specified argument|
|$ git status||Lists all new or modified files to be committed|
|$ git config --global user.name “[user-name]”||Defines the name you want associated with your commit transactions|
|$ git config --global user. Email "[user-email-address]"||Defines the email address you want associated with your commit transactions|
|$ git config --global color.ui auto||Turns on colorization of command line output|
|$ git add [file]||Prepares the file for commit by logically moving it to the staged area|
|$ git ls-files --stage||Lists all the files in the staged area|
|$ git commit -m "[commit message]"||Adds the staged files permanently in version history|
|$ git diff||Shows unstaged file differences|
|$ git diff --staged||Shows file differences between staging and the last file version|
|$ git branch||Creates a new branch|
|$ git checkout [branch-name]||Switches to the specified branch and updates the working directory|
|$ git merge [branch-name]||Combines the specified branch’s history into the current branch|
|$ git branch -d [branch-name]||Deletes the specified branch|
|$ git rm [file]||Deletes the file from the working directory and the staging area|
|$ git log||Lists version history for the current branch|
|$ git log --oneline||Lists version history in one line for the current branch|
|$ git log –oneline –decorate --graph||Lists version history in one line, decorated in graphical form for the current branch|
|$ git push [alias] [branch]||Uploads all local branch commits to remote repository|
|$ git pull||Downloads from remote repository and incorporates changes|
|$ git clone [repository-url]||Clones an existing repository|
|$ git rebase [branch]||Rebases your current HEAD onto [branch]|
|$ git stash||Temporarily stores all modified tracked files|
Installing Git on Windows
Step 1: Go to https://www.git-scm.com/. Click on Download [latest version] for Windows.
Step 2: You will view the below screen. Download has started and the .exe file is downloading.
Step 3: Click on Next when this popup window opens.
Step 4: Check the below options and click Next.
Step 5: The below screen comes. Click Next.
Step 6: Choose the appropriate option and click Next.
Step 7: Choose the library option and click on Next.
Step 8: Choose the checkout option and click Next.
Step 9: Choose the emulator and click Next.
Step 10: Choose configuration options and click on Install.
Step 11: Once the installation completes, the Git GUI opens.
After installing Git in your Windows system, just open your folder/directory where you want to store all your project files; right click and select ‘Git Bash here’.
We will run the commands on Windows and operations using Git Bash. Git Bash is a text-only command line interface for using Git on Windows which provides features to run automated scripts.
Now, let us open Git Bash and run some Git commands
1. $ git init “[repository-name]”
To view the git folder, you use the command ls -la as it is a hidden folder.
2. $ git config
3. $ git add . or git add [filename]
4. $ git commit
5. git status
6. git checkout -b [branchname]
7. git log
8. git ls-files –stage
9. git diff
10. git merge
Installing Git on AWS
On AWS, after login, go to EC2 instances and connect to your VM instance.
Once you have connected to the VM, run the following command to install Git.
To check the version of Git installed
The rest of the git commands for operations related to repository like $ git init etc, will remain the same as mentioned above, in the case of Windows. You can run them in similar manner.