Top 20 Git Commands with Examples

Collaboration between developers made easier with the Git software. Get the most from the Git tool by learning a few of its main commands with examples.

Digital Delivery
-
8 min
Digital Delivery
/
Top 20 Git Commands with Examples

Git is a powerful tool that has made collaboration easier for developers. It is used usually for coordinating the collaborative work among developers during software development.

In other hand, it can be challenging to determine where to start.

On this article we will be sharing 20 of the most common Git commands with examples so you can get started using Git in your work today.

1. Git merge

Git merge is a robust feature that allows you to combine work from two branches into one.

This is useful when developers work on the same code and want to integrate their changes before pushing them up in a branch.

Example case: $ git merge <the name of branch>

2. Git diff

Use git diff to show all the differences between any two commits or files within your Git repository.

It can be beneficial for identifying where there have been conflicts during branching or merging operations.

It will show which each commit/branch added lines and highlight any conflicting lines.

Example case: $ git diff <the source branch> <the target branch>

3. Git log

The git log command lists every commit that has ever happened in your project to see what has changed over time, along with some other information about how the commit was done.

Example case: $ git log

4. Git show

Use the git show command to view the contents of a specific Git object — be it a file, directory, or blob.

It helps inspect commits that have been made in your project and preview what changes were applied by each one.

Example case: $ git show <commit>

5. Git grep

Git grep searches through your codebase for any occurrences of an expression (i.e., keyword).

This can help you find where something has been referenced without manually searching through all files within your repository that may contain this reference.

Example case: $ Git grep -n

6. Git branch

Use the git branch command to create or delete a new branch from an existing one.

Example case: $ git branch

7. Git push

To share your local commits by pushing them up to a remote repository, you'll need to run git push within your Git project directory.

This command uploads all changes made locally and sends them up for everyone else on the team (or organization) working on the same codebase.

Example case: $ git push -u <name of variable> <name of branch>

8. Git stash

The git stash command is used to save your changes but not record them in the Git repository.

This can be useful when you want to stop working on a given feature or section for now and come back later.

Example case: $ git stash

9. Git rebase

To update branches with recent commits made elsewhere (for example, if someone else has pushed up new updates while you've been coding), then you'll need to run git rebase.

Example case: $ Git rebase master.

10. Git config

To set up Git with your details, you'll need to use git config. In this command, you can specify a username and email address to be used by default whenever new commits are made.

Example case: $ git config — global user.email "email address"

11. Git clone

Git clone does just what it says — it clones.

It copies your entire Git repository so you can work on a copy without impacting the original one, meaning no merge conflicts or accidental overwrites.

Example case: $ git clone <URL of your project>

12. Git init

Use git init to create an empty Git repository.

As long as this command has been run in a directory with nothing else stored inside of it, then that's all you need to do before starting your project and adding files into the new folder using commands like git add.

If there are other projects (or folders) in the same location, those will get cloned by default when running git init.

Example case: $ git init <the name of your repository>

13. Git checkout

If you want to switch your local branch, then git checkout is what you'll need.

The name of the current branch will be given as a parameter, followed by another one for which you want to set up.

Example case: $ git checkout <the name of your branch>

14. Git reset

Use git reset to "un-track" a file to no longer have any links to the Git repository.

This can be useful if you have made changes locally but want those new commits to go up on GitHub or elsewhere for others to see.

Example case: $ git reset <file>  

15. Git tag

A git tag labels an essential milestone in the project, like when it was published or how old it is. Use tags to mark releases and help you keep track of your history.

Example case: $ git tag <commitID>

16. Git archive

To create archives from Git branches, you'll use this command followed by one for which you want to generate the zip file — i.e., master or development.

It will then give you two parameters — name and type — both of which are optional, so don't worry if they're not given as arguments on your first try.

The default values for these are "Zip" (which implies compression) and "Standalone Archive," respectively.

Example case: $ git archive --format=tar HEAD

17. Git commit

The git commit command is what you'll use to take all of the changes that have been made locally and push them up to a remote repository.

It's important to note that you can't just type "git commit" by itself with no arguments — it needs at least one parameter, which will be either HEAD or any other branch name.

Example case: $ git verify-commit <commit>

18. Git status

Git status is a powerful command that can check the changes made in your local repository.

It will show you which files have been changed, added, or deleted — and any merge conflicts between branches that you need to take care of.

Example case: $ git status

19. Git rm

Use the git rm command to remove files from both the working directory and the Git repository.

This means that they'll be deleted on your computer — but also gone for good in terms of being a part of what's saved online.

Example case: $ git rm <the name of your file>

20. Git remote

Git remote establishes the connection between your local repository and a remote one.

For this to work, you'll need to know the URL for where it lives — or if you're doing so locally, then just running Git remote is all that's required.

Example case: $ git remote add <name of variable> <remote server URL>

+ Git fetch

This command takes any new commits from another branch in an existing Git repository and copies them onto yours by default.

Use this as a shortcut or when too many branches are cloned at once, making things difficult.

Example case: $ Git fetch

+ Git add

Git add tells Git which files it should track and upload the next time you commit.

It can be run with a single argument or many, depending on how much you want to change.

Example case: $ git add <file>

+ Git pull

Git pull is what you'll need to fetch the changes from another branch and merge them into yours.

Use this if you're working on a separate but related project or want to get new commits that may have been made in your repository.

Example case: $ git pull <repository URL>

Still not sure where to start? Continue expanding your knowledge and skills with further Git Tutorial.

Published on
February 2, 2021

Industry insights you won’t delete. Delivered to your inbox weekly.

Other posts