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.
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.
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
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 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.
The master is the primary branch of all your repositories. It should include the most recent changes and commits.
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 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.
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).
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.
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.
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.
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 provide a way to keep track of important commits. Lightweight tags simply serve as pointers while annotated tags get stored as full objects.
In the context of Git, upstream refers to where you push your changes, which is typically the master branch.
To add changes from one branch to another, use the git merge command in conjunction with pull requests.
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 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.
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
Here are some basic GIT commands you need to know
With the command below you can configure your user name:
This command lets you setup the user email address you'll use in your commits.
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:
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
Clone the repository located at ＜repo＞ into the folder called ~＜directory＞! on the local machine.
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.
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.
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:
You can delete the branch using below command
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.
You can use a wildcard to add all files in your project to the staging area.
You can add all files beginning with 'str' to the staging area by using the command below.
This command displays the current repository's status, including staged, unstaged, and untracked files.
This command allows you to specify a brief summary of your commit message.
Using the -a and -m options, you can add and commit tracked files with a single command.
This command displays the commit history for the current repository:
This command displays the current repository's commit history:
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.
You can use git commit —amend to modify and add changes to the most recent commit.
Replace commit-id with the id of the commit found in the commit log.
You can specify a file as a parameter to only see changes to that file. By default, git diff displays only unstaged changes.
If you want to remove a certain file from the stage:
This command stages the changes, then it expects a commit message.
When all of your work is complete and ready to be saved on a remote repository, use the commands below to push all changes:
Push a local branch for the first time:
To push a local branch to a different remote branch, you can use:
If you have to undo your last push, you can use below command.
This command will download changes from a remote repository but will not merge them into your local branch (use git pull to do that).
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:
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.
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.