Glossary
Last Updated Nov 30, 2021

Git Command

Emma Jagger

Table of Contents:

Get your free
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

What is a Git Command?

Git is a distributed revision control system. If you've ever saved a document as "my document 1", then "my document 2", and so on, you have an idea of what Git does. But what if you make a mistake on "my document 2" and want to revert to an old version? Or what if your coworker wants to work on the document at the same time, and you're worried you'll end up with two different versions?

Git works by copying a file locally from an online repository. When you're done working on it, you submit the file back to the main repository it was copied from. The repository owner looks it over, and allows it to be "pulled" into the repo, updating the version for everyone.

Git works in the command line, but its capabilities are also available as a desktop program. If you're having trouble with Git in the command line, GitHub Desktop is a great way to learn how Git works before easing yourself into the command line.

Whichever Git you choose, you will have to exchange SSH keys for authentication.

Git Commands Quickstart

This is a quick overview of the Git checkout/checkin process commands from the command line.

  • Clone your fork onto your machine. This implicitly defines a remote repository named origin.`git clone [email protected]:githubusername/filename.git`
  • Set up the upstream remote. This links origin with main on GitHub.`git remote add upstream [email protected]:githubusername/filename.git`
  • Create a new branch. Git branch creates a branch of your origin fork that you can edit locally. Branch early and often.`git checkout -b my_git_demo`
  • Create a new file. Touch creates a new, empty file. Git Add adds any new files to the staging area (you are telling Git that you intend to commit the file you are adding).`touch git_demo add git_demo`
  • Create a commit. This adds your changes to your local repo.`git commit`
  • Add your work to the local repository. Before you add your work, check to make sure you have the latest changes from upstream.`git fetch upstream main` (this will fetch upstream changes) OR`git rebase upstream/main` (this will roll back your commit, apply the fetched changes, and then reapply your commit)
  • Push changes to your fork. This adds your local changes to your GitHub repository.`git push origin my_git_demo`
  • Finally, create a pull request in GitHub. This proposes the addition of your changes to the main repository. The repo owner will double-check your work before adding it to main.

Basic Git commands

Git is a distributed revision control system for software development. It enables you to store code as separate versions akin to storing documents as v1, v2 etc. What Git also brings to the table is the capability to revert code to the previous version where a mistake has been made. This also makes it easier for programmers to collaborate on the same code base by ensuring that none of your programmers are creating their own siloed version.

Here are 20 Git commands that you’ll frequently find useful.

Git Init

This creates a new local Git repository. You’d use this command to either convert an unversioned existing project to a Git repository or to create a new Git repository.

So long as you’re in the project directory you can type ‘git init’ to convert the directory to a Git repository.

Or ‘git init <directory>’ to create a repository in the named directory.

Or ‘git init --bare <directory>’ to create a directory purely for storage rather than development.

Git Config

This configures the name and email address of the author (you) which will be used when you commit code. You’d used this command to set configuration values on global or local projects.

Type ‘git config --global user.name "<your name>"’

‘git config --global user.email [email protected]

When specifying the color.branch output slot and color.decorate output slot there are specific variations for remote servers: ‘color.branch.<remote>’ and ‘color.decorate.<remote>’

Git Clone

This creates a local copy of a local or remote Git repository. You’d use this command to create a local working copy of the Git.

Type ‘git clone ssh://[email protected]/path/to/my-project.git’

‘cd my-project’

‘# Start work on the project’

Or ‘git clone <repo> <directory>’ to clone to a specific directory.

Or ‘git clone -branch new_feature git://remoterepository.git’ to clone just a specific branch of a repository.

Git Add

This adds a change in the working directory. You’d use this command to tell Git that you want to include updates in a file to the next commit.

A commit is a save akin to saving a local file such as a document without sending it to the server.

To stage all changes in a file for the next commit type ‘git add <filename>’

To do the same thing with a directory type ‘git add <directory>’

To chose portions of a file to add to the next commit type ‘git add -p’

Git Commit

This saves the local file to the repository or commit changes to the head but not the repository. You’d use this to check in final code to a server where it then becomes available for collaboration.

To combine this with git add from the previous entry, you’d type:

‘git add .’

‘git commit’

Git Push

This sends changes to the master branch of a remote repository. You’d use this to share a series of commits at once.

To confirm which repositories you have connections to type ‘git remote’ or ‘git remote -v’ to get the URL for each repository.

Then type ‘git remote add <name> <url>’ to create a remote connection to that repository.

Then type ‘git push origin master’

Git Status

This lists the files you’ve changed and those you still need to commit.

This enables you to see which files are staged, unstaged and untracked.

Type ‘git status’

Git RM

This deletes a file. You’d use this to delete a working file and stage that deletion

Type ‘git rm <filename>’

Git Log

This presents version history of the current branch. You’d use this to understand the changes that have been made, especially with collaborative repositories.

Type ‘git log’

To get the version history of a file including renaming type ‘git log –follow<filename>’

Git Show

This shows metadata and content changes. You’d use this to help understand precisely how a file has changed ie the additional code added.

Type ‘git show <commit>’

Git Branch

This enables you see all the current branches in the repository. You’d use this to ensure that you have visibility over all the branches available which can be important given how easy branches are to create in Git.

Type ‘git branch’

Type ‘git branch <newbranch>’ to create a new branch of that name.

Type ‘git branch -d <branch>’ to delete the named branch.

Type ‘git branch -D <branch>’ to delete a specific branch even if it has unmerged changes where a line of development is no longer needed.

Type ‘git branch -m <extranewbranch>’ to rename the current branch.

Type ‘git branch -a’ to list all remote branches.

Git Merge

This enables the merging of two branches. You’d use this to merge another branch into your current working branch.

Type ‘git merge <branchname>’

Git Push

This pushes a current branch or file to a remote repository. You’d use this to push local commits to the remote repository.

Type ‘git push <remote> <branch>’

Type ‘git push <remote> --force’ to do the same as above even if it’ll create a non-fast forward merge.

Type ‘git push <remote> --all’ to push all branches to the remote repository.

Type ‘git push <remote> --tags’ to push all the tags to the remote repository as this doesn’t happen automatically.

Git Pull

This synchronizes the remote server and working directory. You can use this to both fetch and merge changes on the remote server to your working directory.

Type ‘git pull <remote>’

Git Remote

Use this to connect to a remote repository. You’d want to connect to a remote repository to be able to source and deliver code to a centralized server or repository.

Type ‘git remote’ to see which repositories you’re connected to.

Type ‘git remote -v’ to get the repository’s URL.

Type ‘git remote add <name> <url>’ to create a connection to a remote repository.

Type ‘git remote rm <name>’ to remove the connection to a repository.

Type ‘git remote rename <old-name> <new-name>’ to rename a remote connection.

Git Diff

View and manage conflicts. This enables you to view all the conflicts that would arise from a merge to prevent issues later.

Type ‘git diff’

Type ‘git diff –staged’ to view all the conflicts between files staged and those present

Type ‘git diff <first branch> <second branch>’ to see the conflicts between two named branches

Git Reset

This enables you to reset a file. You’d use this to remove all your local changes so that you can restore your code to a safe point in case something’s become fundamentally broken.

Type ‘git reset <filename>’

Type ‘git reset <commit>’ to undo all your commits after a specific commit.

Type ‘git reset –hard [commit]’ to discard all history and go back to a specific commit.

Type ‘git fetch origin’

‘git reset --hard origin/master’ to drop all local changes and commits, and restore from the original from the server.

Git Tag

Apply a tag. You might use this to mark a significant changeset or version.

Type ‘git tag <number> <commitID>’ to apply a specified number to a specific commit

Git Stash

Create a temporary stored version. Use this when you want to record the current state of your index and working directory, but revert to a clean working directory.

Type ‘git stash save’ to temporarily store all modified and track files.

Type ‘git stash pop’ to restore the most recently stashed file.

Type ‘git stash list’ to list all stashed files.

Type ‘git stash drop’ to drop the most recent stash.

Git Checkout

Undo local changes. If you discover that you’ve made a mistake, then you can undo them using this command.

Type ‘git checkout -- <filename>’ to replace changes in your file with the last content in the head while preserving changes already added to the index as well as new files.

Conclusion

There are many other useful Git commands, but an understanding of the Git process is more important at an early stage. Build yourself a git repository test environment and practice forking, cloning, git merge, and git pull. You'll eventually find you're using Git with confidence. And hey, if you make a mistake, you can always roll back your commits with the git log.

Get your free
API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required