Git Cheat Sheet – Git Commands for Easy Reference
What Is Git?
Git is a Distributed Version Control System (DVCS) for saving different versions of a file (or set of files)—wherein any version is retrievable at will.
Git makes it easy to record and compare different file versions. Consequently, details about what changed, who changed what, or who initiated an issue are reviewable anytime.
Below are some of the widely used Git commands.
Installing and Updating Git
Below are the commands for installing and updating Git on your system.
Description | Command |
---|---|
Install Git | https://git-scm.com/download |
Check Git’s installation location |
|
Check the installed Git version |
|
Update Git (Windows) |
|
Update Git (Mac) | https://git-scm.com/download/mac |
Initializing and Cloning Git
Below are the commands for initializing new local Git repositories or cloning existing Git repositories.
Description | Command |
---|---|
Initialize Git |
|
Clone a Git repository |
|
Configuring Git
Below are the commands for configuring, reviewing, and updating users’ information for all local Git repositories.
Description | Command |
---|---|
Configure your Git username |
|
Configure your Git email |
|
Check your project’s configurations |
|
Check your email configuration |
|
Check your username configuration |
|
Update your username configuration |
|
Update your email configuration |
|
Exit the Git configuration space | Press the Q keyboard key |
Tracking Changes
Below are the commands for monitoring changes to files or folders.
Description | Command |
---|---|
Check the status of your files |
|
Stage a specific file (or folder) |
|
Stage all modified and untracked files |
|
Stage the current directory’s modified and untracked files |
|
Stage all modified files that you’ve committed previously |
|
Compare the working directory and staging area’s file versions |
|
Compare the staging area’s file version with the most recently committed version |
|
Undoing the Staging of files
Below are the commands for undoing the staging of files.
Description | Command |
---|---|
Undo the staging of a specific file |
|
Undo the staging of all files |
|
Committing Files to Git
Below are the commands for saving files to the Git repository.
Description | Command |
---|---|
Commit your files | git commit -m "Write a message about the file(s) you are committing" |
Stage and commit at once | git add file-or-folder-name && git commit -m "Your commit message" |
Commit without writing a commit message |
|
Modify a previous commit message | How to modify a previous commit message |
Commit without staging first |
|
Undo a specific commit |
|
Viewing Your Commits
Below are the commands for viewing your commits.
Description | Command |
---|---|
See the changes you are about to commit |
|
View your project’s commit history |
|
View a specific number of the project’s commit history (for instance, the last three) |
|
Exit the Git console space | Press the Q keyboard key |
See all the files currently in the staging area and the Git repository |
|
Check if a specific file or folder is in the Git directory |
|
Ignoring Files
Below are the commands for telling Git to ignore specific files.
Description | Command |
---|---|
Create a .gitignore file |
|
See an example of a .gitignore file | Example of a .gitignore file |
Open Gitignore’s manual page |
|
Managing Git Branches
Below are the commands for creating, renaming, deleting, and switching between Git branches.
Description | Command |
---|---|
Check your project’s local branches |
|
Check your project’s remote branches |
|
Create a new branch |
|
Delete a specific branch |
|
Rename your project’s HEAD branch |
|
Rename a non-HEAD branch |
|
Switch from one branch to another |
|
Switch back to the previous branch |
|
Create and switch immediately to a new branch |
|
Switch to a specific commit history |
|
Merge a specific branch into the HEAD branch |
|
Compare branches |
|
Sharing Git Repos
Below are the commands for sharing content between local and remote Git repositories.
Description | Command |
---|---|
Link your project’s local and remote repositories |
|
Confirm the connection between your local and remote repositories |
|
Push your local Git repo to the remote repository |
|
Confirm the upload of your local Git repo to the remote repository | Go to your GitHub repository page and refresh the browser. |
Undo the pushing of a commit upstream |
|
Publish your app on GitHub Pages | How to publish your website with GitHub Pages |
Deleting Files and Folders
Below are the commands for deleting files and folders from the working directory and Git repository.
Description | Command |
---|---|
Delete a file from the working directory and the Git repository |
|
Forcefully delete a file from the working directory and the Git repository |
|
Delete a folder from the working directory and the Git repository |
|
Delete a file only from the staging area—not from the working directory |
|
Rename a specific file |
|
Replace a working directory’s file with its last committed version |
|