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.
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>
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>
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
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>
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
Use the git branch command to create or delete a new branch from an existing one.
Example case: $ git branch
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>
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
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.
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"
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>
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>
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>
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>
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>
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
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>
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
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>
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>
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 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 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 through this Git Tutorial.