logo logo

10 Common Git Commands Everyone Should Know

main post image

Version control (sometimes referred to as source control) plays an important role in any development project, including test automation. It’s the practice of tracking and providing control over changes made in the source code. And since one of the most common tools used in version control is Git, let’s get to understand some of the most common Git commands.

Benefits of Git

  • It’s free (and open-source): you don’t need to purchase, and of course, since it is open-source, you are free to contribute to the code any time.
  • Performance: Git focuses on file content rather than on file names, which makes it faster and more reliable than other version control software.
  • Security: Git protects the code and the change history, through a cryptographically secure hashing algorithm called SHA1.
  • Widely-used: Git has become the preferred VCS tool in many great organizations.

Most common Git commands

Using git –help in the command prompt (on Windows), terminal (for Mac), or shell (on Linux) will give you a list of the available Git commands:

Common Git Commands

Let’s look over some of the most useful Git commands and understand them.

1. git clone

This command is used for downloading the latest version of a remote project and copying it to the selected location on the local machine. It looks like this:

git clone <repository url>

Here’s an example:

Git clone command

To clone a specific branch, you can use

git clone <repository url> -b <branch name>

2. git fetch

This Git command will get all the updates from the remote repository, including new branches.

3. git checkout

You can use the checkout command to switch the branch that you are currently working on.

git checkout <branch name>

If you want to create a new branch and switch to it, you can do it by using this command:

git checkout -b <branch name>

4. git init

This is the command you need to use if you want to start a new empty repository or to reinitialize an existing one in the project root. It will create a .git directory with its subdirectories. It should look like this:

git init <repository name>

5. git commit

This one is probably the most used Git command. After changes are done locally, you can save them by “committing” them. A commit is like local a snapshot of the current state of the branch, to which you can always come back. To create a new commit, type this command in Git Bash:

git commit -m "<commit message>"

If all goes well, you will see the changes in the commit:

Git Commit

6. git push

Git push will push the locally committed changes to the remote branch. If the branch is already remotely tracked, simply use it like this (with no parameters):

git push

If the branch is not yet tracked, and only resides on the local machine, you need to run the command like this:

git push --set-upstream <remote branch> <branch name>

Like this:

Git Push

7. git diff

You can use this command to see the unstaged changes on the current branch. Here’s an example of a branch with an edited feature file:

Git Diff

If you want to see the staged changes, run the diff command like this:

git diff --staged

Or you can compare two branches:

gif diff <branch1> <branch2>

8. git pull

Using git pull will fetch all the changes from the remote repository and merge any remote changes in the current local branch.

9. git add

This is the command you need to use to stage changed files. You can stage individual files:

git add <file path>

Or all files:

git add .

10. git branch

Using git branch will list all the branches of the repository:

Git Branch

Or you can use it to create a new branch, without checking it out:

git branch <new branch>

To delete a branch, run it like this:

git branch -d <branch name>

Conclusions

Git can be a really powerful tool in any development project and you will probably use most of these commands on a daily basis if your team uses Git for version control.

💡 Cool update: TestProject‘s free test automation platform just launched its 2.0 version, that now supports saving tests locally for complete offline executions and easy version management as part of a versioning control system such as Git  → GET YOUR FREE ACCOUNT TODAY 🚀

Avatar

About the author

Andreea Draniceanu

Andreea is a QA engineer, experienced in web and desktop applications, and always looking to improve her automation skills and knowledge. Her current focus is automation testing with C#.

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Leave a Reply

popup image

Test, Deploy & Debug in < 1 hr

Leverage a cross platform open source automation framework for web & mobile testing using any language you prefer, and benefit from built-in dashboards & reports. Free & open source.
Get Started
FacebookLinkedInTwitterEmail