This guide includes an introduction on Git, a definition of terms, and lists of commonly used Git commands. If you're having trouble getting started with Git, or if you're an experienced tester looking for a refresher, this cheat sheet can help.
What is GIT
Git is a free, open source distributed version control system tool designed to handle everything from small to very large projects with speed and efficiency. It was created by Linus Torvalds in 2005 to develop Linux Kernel. Git has the functionality, performance, security and flexibility that most teams and individual developers and testers need. It also serves as an important distributed version-control DevOps tool.
Git Terminologies:
Repositories
Repositories can be compared to folders on your computer. A Github repository contains all of the project's files, including the documentation and all revisions. It is the most basic element of
Github.
Branch:
Branch is a version of the repository that diverges from the main working project. It is an essential feature available in most modern version control systems. A Git project can have more than one branch. We can perform many operations on Git branch-like rename, list, delete, etc
Remote:
Remote is the term used to refer to the repository hosted in Github. A company's local Git repository hosted in Github or any other Git host platform for sharing and collaboration is generally called Remote repository.
Master
The master is the primary branch of all your repositories. It should include the most recent changes and commits.
Checkout
Use the git checkout command to switch between branches. Just enter git checkout followed by the name of the branch you wish to move to, or enter git checkout master to return to the master branch.
Commit
Commit is one the most frequently used feature of Git, it’s like saving the updated file to its original folder and overwrite on already existing.A commit is a collection of content, a message about how you got there, and the commits that came before it.
Fetch
Fetching refers to getting the latest changes from an online repository (like GitHub.com) without merging them in. Once these changes are fetched you can compare them to your local branches (the code residing on your local machine).
Fork
A 'fork' is a personal copy of another user's repository on your GitHub account. Forks allow you to freely modify a project without affecting the original, providing limitless opportunities for experimentation and learning from the work of others.
Head
A HEAD is a snapshot of the most recent commit on each branch. On each branch, it is a short name or a pointer reference to a SHA-1 of a commit ID.A HEAD always points to the branch's most recent commit (code) and moves forward with each commit to point to the most recent commit ID.
Index
When you add, delete, or modify a file, it is retained in the index until you are ready to commit the changes. Think of it as Git's staging area. To view the contents of your index, use the git status command. Changes highlighted in green are ready to commit, while those highlighted in red must still be added to staging.
Stash
Git stash removes changes from your index and "stashes" them for later use. It's useful if you want to put something on hold and work on something else for a while. You can only stash one set of changes at a time.
Tags
Tags provide a way to keep track of important commits. Lightweight tags simply serve as pointers while annotated tags get stored as full objects.
Upstream
In the context of Git, upstream refers to where you push your changes, which is typically the master branch.
Merge
To add changes from one branch to another, use the git merge command in conjunction with pull requests.
Origin
The default version of a repository is referred to as the origin. Origin can also be used as a system alias to communicate with the master branch. To push local changes to the master branch, use the command git push origin master.
Pull
Pull requests represent suggestions for changes to the master branch. If you're working with a team, you can create pull requests to tell the repository maintainer to review the changes and merge them upstream. The git pull command is used to add changes to the master branch.
Push
Pushing refers to sending your committed changes to a remote repository such as GitHub.com. For instance, if you change something locally, you'd want to then push those changes so that others may access them
GIT Commands
Here are some basic GIT commands you need to know
How to setup your Git username:
With the command below you can configure your user name:
How to setup your Git user email:
This command lets you setup the user email address you'll use in your commits.
How to Initialize a Git repo:
Everything begins here. The first step is to create a new Git repo in your project's root directory. You can do so with the command below:
How to clone a Repository:
Git clone is a Git command line utility which is used to target an existing repository and create a clone, or copy of the target repository
Cloning to a specific folder
Clone the repository located at <repo> into the folder called ~<directory>! on the local machine.
How to Create a New Branch in Git:
By default, there is only one branch, the main branch. You can use this command to create a new branch. Git will not switch to it automatically; you must do so manually with the following command.
To create a new branch use:
How to List Branches in Git:
Using the git branch command, you can see all of the branches that have been created. It will display a list of all branches, with the current branch marked with an asterisk and highlighted in green.
How to Switch Branches in Git:
When you create a new branch, Git will automatically switch to it.
If you have multiple branches, you can easily switch between them by running git checkout:
How to delete a branch in Git:
You can delete the branch using below command
How to add a file to the staging area in Git:
The command below will add a file to the staging area. Simply replace filename here with the name of the file you want to include in the staging area.
How to add all files in the staging area in Git:
You can use a wildcard to add all files in your project to the staging area.
How to add only certain files to the staging area in Git:
You can add all files beginning with 'str' to the staging area by using the command below.
How to Check repository's status in Git:
This command displays the current repository's status, including staged, unstaged, and untracked files.
How to Commit changes with a message:
This command allows you to specify a brief summary of your commit message.
How to Commit changes (and skip the staging area) in Git:
Using the -a and -m options, you can add and commit tracked files with a single command.
How to View your commit history in Git:
This command displays the commit history for the current repository:
How to view commit history including changes in Git:
This command displays the current repository's commit history:
How to view log stats in Git:
This command causes the Git log to display some statistics about the changes in each commit, such as the number of lines changed and file names.
How to Amend the most recent commit in Git:
You can use git commit —amend to modify and add changes to the most recent commit.
How to view a specific commit in Git
Replace commit-id with the id of the commit found in the commit log.
How to view changes made before committing them using "diff" in Git:
You can specify a file as a parameter to only see changes to that file. By default, git diff displays only unstaged changes.
To see any staged changes, we can use diff with the —staged flag.
How to revert staged changes in Git:
If you want to remove a certain file from the stage:
Remove all staged files:
How to Rename files in Git:
This command stages the changes, then it expects a commit message.
How to push changes to a remote repo in Git:
When all of your work is complete and ready to be saved on a remote repository, use the commands below to push all changes:
First Push
Push a local branch for the first time:
After that, then you can just use:
Push local branch to different remote branch
To push a local branch to a different remote branch, you can use:
How to Undo Last Push
If you have to undo your last push, you can use below command.
How to fetch remote repo changes in Git:
This command will download changes from a remote repository but will not merge them into your local branch (use git pull to do that).
How to pull changes from a remote repo in Git:
If other team members are working on your repository, you can use the command below to retrieve the most recent changes made to the remote repository:
If you have a branch called my_feature and you want to pull that branch, you can use:
How to merge two branches in Git or Rebase:
When you run git merge, your HEAD branch will generate a new commit, preserving the ancestry of each commit history.
The rebase re-writes the changes of one branch onto another without creating a new commit.
Merge Master Branch to Feature Branch:
Or with rebase option, use below command:
Merge Feature Branch to Master Branch:
How to use GIT STASH:
Git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.
Top comments (0)