Git User Handbook
The Git cheat sheet presented in this DevOps tutorial is designed for those who have an interest in learning file management using Git as a tool. This cheat sheet will be a handy reference not only when you learn Git but when you start working with it too. Don’t worry if you are a beginner and have no idea about how Git works. Intellipaat’s Git cheat sheet will give you a quick reference to the basics that you must know.
What is Git?
Git is a DevOps tool that is used as a version-control system for tracking the changes happening in system files and coordinating the work on those files among a group of people. In the software development field, Git is used in source code management and for tracking the changes made in any file.
- Git is a distributed version control and source code management system with an emphasis on speed.
- It is a repository used to manage projects, the set of files, as they change over time.
- Using Git, every code change or commit we make gets updated in the development code of a project.
Be ready for the industry by going through these Top DevOps Interview Questions and Answers!
Watch this Git Tutorial for Beginners video:
It shows the process flow with components used to perform certain tasks:
- Plan: It can be created or deleted by itself based on the updates in the source repository.
- Code: It is the statement written to perform a task in a repository.
- Shared repository: It is the repository that is shared among several members in order to perform a task.
- Continuous integration: It helps in updating a repository with the changes made in the code and provides integration with the changes, time to time.
- Build: It is used to manually build products and dependencies to Git.
- Configuration management: It is a process of tracking and controlling the changes made in a system.
- Deploy: Deploy is the process of pushing the code to a remote server.
Version control is the management of changes made to the code, documents, programs, large sites, and other information. The changes are termed as versions.
A version control system (VCS) is used to perform the following:
- It allows developers to work simultaneously.
- VCS does not allow overwriting each other’s changes.
- It maintains a history of every version.
There are two types of version control systems:
- Centralized VCS
- Distributed VCS
Our DevOps tool, Git falls under the category of distributed VCS.
Become a master of DevOps by enrolling in this online DevOps Training in London!
Git and GitHub
Git is a VCS that supports distributed nonlinear workflows by providing data assurance for developing quality software.
Its features are as follows:
- Distributed: A distributed development of code
- Compatible: Works with existing systems and protocols
- Non-linear: Allows the non-linear development of code
- Branching: Easy to create and merge branches
- Lightweight: Lossless compression
- Speed: Faster than the remote repository
- Open-source: A free tool and hence economical
- Reliable: Not viable to any loss of data upon crashes
- Secure: Uses SHA1 and checksum
Git Operations and Commands
- For the initial configuration of username, email, and code highlighting (optional):
$git config -- global user.name”firstname lastname”
$git config -- global user.email” [email protected]”
$git config -- global color.ui true (enables code highlights)
$git config --list
Adding and Removing Files
$git add<filename> <2nd filename> <3rd filename>
- To add all the updated files:
$git add --all ( use -A instead of -all too )
$git rm -r <filename>
- To pass a message, use ‘commit’ with ‘-m’:
$git commit -m “body_of_message”
- To amend the last commit or the last message:
$git commit --amend -m “new_message”
Learn DevOps from Intellipaat’s DevOps Training in Sydney to boost your career!
Pushing and Pulling
Here, a remote repository typically represents a remote server or a Git server.
- To create a remote repository via GitHub, go to:
$git remote add origin<link>
$git push -u origin master
$git clone <clone>
Branching and Merging
||To list branches
|git branch -a
||To list all the branches
|git branch [branch name]
||To create a new branch
|git branch -d [branch name]
||To delete a branch
|git push origin –delete [branchName]
||To delete a remote branch
|git checkout -b [branch name]
||To create a new branch and switch to it
|git checkout -b [branch name] origin/[branch name]
||To clone a remote branch and switch to it
|git checkout [branch name]
||To switch to a branch
|git checkout –
||To switch to the branch last checked out
|git checkout — [file-name.txt]
||To discard the changes made to a file
|git merge [branch name]
||To merge a branch into an active branch
||To stash the changes in a dirty working directory
|git stash clear
||To remove all the stashed entries
Sharing and Updating Projects
|git push origin [branch name]
||To push a branch to a remote repository
|git push -u origin [branch name]
||To push the changes made to a remote repository (-u remembers the branch for the next use)
|git push origin –delete [branch name]
||To delete a remote branch
||To update a local repository to the newest commit
|git pull origin [branch name]
||To pull the changes from a remote repository
|git remote add origin ssh://[email protected]/[username]/[repository-name].git
||To add a remote repository
|git remote set-url origin ssh://[email protected]/[username]/[repository-name].git
||To set a repository’s origin branch to SSH
Interested in grabbing an industry-recognized DevOps certification? Intellipaat’s DevOps Course in Bangalore is just designed for you!
Inspection and Comparison
||To view the changes made
||To view changes (in detail)
|git diff [source branch] [target branch}
||To preview changes before merging
Intellipaat’s comprehensive DevOps Training would help you be a certified DevOps Engineer!