DevOps

Git Command Cheatsheet

Complete guide to Git commands covering configuration, initialization, branching, merging, remote repositories, history rewriting, and advanced operations.

#git #version-control #devops #cli #repository #github
Sign In to Download

Free account required

What is Git?

Git is a distributed version control system that tracks changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

Git was created by Linus Torvalds in 2005 for development of the Linux kernel. Every Git directory on every computer is a full-fledged repository with complete history and full version-tracking abilities, independent of network access or a central server.

Configuring Git

Configure user information for all local repositories. These settings are stored globally and will be used for all your Git operations.

Set the name that will be associated with your commits

$ git config --global user.name "[name]"

Set the email that will be associated with your commits

$ git config --global user.email "[email]"

Create a shortcut for a Git command (e.g. alias.glog "log --graph --oneline")

$ git config --global alias.[alias] [command]

Set the default text editor to use for commit messages (e.g. vi)

$ git config --global core.editor [editor]

Open the global config file in a text editor for manual editing

$ git config --global --edit

Initializing and Cloning

Start a new repository or obtain one from an existing URL.

Initialize an empty Git repository in the current directory

$ git init

Create an empty Git repo in the specified directory

$ git init [directory]

Clone a remote Git repository from the url into a local directory

$ git clone [url]

Clone a remote repo into the specified local directory

$ git clone [url] [directory]

Examining Logs

View the commit history, differences between commits, and other repository information.

Show the commit history for the current branch

$ git log

Show the diffs from each commit in the commit history

$ git log -p

Show stats (files changed, insertions, deletions) for each commit

$ git log --stat

Show condensed summary of the commit history with each commit in one line

$ git log --oneline

Draw a text-based graph of commits with branch names

$ git log --graph --decorate

Show unstaged file differences compared to current index

$ git diff

Show differences between staged changes and the last commit

$ git diff --cached

Show changes between two commits

$ git diff [commit1] [commit2]

Show changes made in the specified commit

$ git show [commit]

Versioning Files

Track, stage, and commit changes to files in your repository.

Stage file changes to be committed

$ git add [file]

Commit the staged snapshot with commit message

$ git commit -m "[message]"

Remove file from staging index and working directory

$ git rm [file]

Move or rename file in Git and stage the change

$ git mv [file] [newpath]

Branching and Merging

Manage branches for parallel development and merge changes between branches.

List all the branches in the current repository

$ git branch

Create a new branch with name [branch]

$ git branch [branch]

Switch the current branch to [branch]

$ git checkout [branch]

Create a new branch and switch to it

$ git checkout -b [branch]

Merge the history of [branch] into the current branch

$ git merge [branch]

Delete the local branch [branch]

$ git branch -d [branch]

Retrieving and Updating Repositories

Synchronize your local repository with remote repositories.

Fetch branches and commits from the remote repository

$ git fetch [remote]

Fetch remote changes and directly merge into local repository

$ git pull [remote]

Fetch remote changes and rebase onto local branch

$ git pull --rebase [remote]

Push local branch to remote repository

$ git push [remote] [branch]

Push all local branches to remote

$ git push --all [remote]

Push all local tags to remote repository

$ git push --tags [remote]

Rewriting Git History

Rewrite branch history with rebase, reset, and other advanced operations. Use with caution!

Rebase current branch onto [branch]

$ git rebase [branch]

Interactively rebase current branch onto [commit]

$ git rebase -i [commit]

Show history of Git commands for current repository

$ git reflog

Clear staging area, rewrite working tree from specified commit

$ git reset --hard [commit]

Remote Repositories

Manage connections to remote repositories and collaborate with others.

Create remote connection with url and alias [name]

$ git remote add [name] [url]

Fetch all branches from remote repository

$ git fetch [remote]

Fetch remote changes and merge into local repository

$ git pull [remote]

Push local branch to remote repository

$ git push [remote] [branch]

Undoing Changes

Revert or undo changes in your repository. These operations help fix mistakes and restore previous states.

Remove file from staging index but leave unchanged locally

$ git reset [file]

Shows which files would be removed from working directory. Use -f option to execute clean.

$ git clean -n

Undo changes from specified commit by creating a new commit

$ git revert [commit]

Compare VPS Performance with Real Benchmarks

Discover the best VPS hosting providers with our comprehensive benchmark database. Compare CPU, disk I/O, and network performance across hundreds of providers to find the perfect infrastructure for your Git repositories.