Git Cheat Sheet – Git Commands You Should Know
Tags: webdev, github, git, javascript
Published: 2024-06-21 14:49:37



1. [What is a Distributed Version Control System?](#what-is-a-distributed-version-control-system)
2. [Git Commands You Should Know](#git-commands-you-should-know)
    1. [How to check your Git configuration](#how-to-check-your-git-configuration)
    2. [How to setup your Git username](#how-to-setup-your-git-username)
    3. [How to setup your Git user email](#how-to-setup-your-git-user-email)
    4. [How to cache your login credentials in Git](#how-to-cache-your-login-credentials-in-git)
    5. [How to initialize a Git repo](#how-to-initialize-a-git-repo)
    6. [How to add a file to the staging area in Git](#how-to-add-a-file-to-the-staging-area-in-git)
    7. [How to add all files in the staging area in Git](#how-to-add-all-files-in-the-staging-area-in-git)
    8. [How to add only certain files to the staging area in Git](#how-to-add-only-certain-files-to-the-staging-area-in-git)
    9. [How to check a repository’s status in Git](#how-to-check-a-repositorys-status-in-git)
    10. [How to commit changes in the editor in Git](#how-to-commit-changes-in-the-editor-in-git)
    11. [How to commit changes with a message in Git](#how-to-commit-changes-with-a-message-in-git)
    12. [How to commit changes (and skip the staging area) in Git](#how-to-commit-changes-and-skip-the-staging-area-in-git)
    13. [How to see your commit history in Git](#how-to-see-your-commit-history-in-git)
    14. [How to see your commit history including changes in Git](#how-to-see-your-commit-history-including-changes-in-git)
    15. [How to see a specific commit in Git](#how-to-see-a-specific-commit-in-git)
    16. [How to see log stats in Git](#how-to-see-log-stats-in-git)
    17. [How to see changes made before committing them using “diff” in Git](#how-to-see-changes-made-before-committing-them-using-diff-in-git)
    18. [How to see changes using “git add -p”](#how-to-see-changes-using-git-add-p)
    19. [How to remove tracked files from the current working tree in Git](#how-to-remove-tracked-files-from-the-current-working-tree-in-git)
    20. [How to rename files in Git](#how-to-rename-files-in-git)
    21. [How to ignore files in Git](#how-to-ignore-files-in-git)
    22. [How to revert unstaged changes in Git](#how-to-revert-unstaged-changes-in-git)
    23. [How to revert staged changes in Git](#how-to-revert-staged-changes-in-git)
    24. [How to amend the most recent commit in Git](#how-to-amend-the-most-recent-commit-in-git)
    25. [How to rollback the last commit in Git](#how-to-rollback-the-last-commit-in-git)
    26. [How to rollback an old commit in Git](#how-to-rollback-an-old-commit-in-git)
    27. [How to create a new branch in Git](#how-to-create-a-new-branch-in-git)
    28. [How to switch to a newly created branch in Git](#how-to-switch-to-a-newly-created-branch-in-git)
    29. [How to list branches in Git](#how-to-list-branches-in-git)
    30. [How to create a branch in Git and switch to it immediately](#how-to-create-a-branch-in-git-and-switch-to-it-immediately)
    31. [How to delete a branch in Git](#how-to-delete-a-branch-in-git)
    32. [How to merge two branches in Git](#how-to-merge-two-branches-in-git)
    33. [How to show the commit log as a graph in Git](#how-to-show-the-commit-log-as-a-graph-in-git)
    34. [How to show the commit log as a graph of all branches in Git](#how-to-show-the-commit-log-as-a-graph-of-all-branches-in-git)
    35. [How to abort a conflicting merge in Git](#how-to-abort-a-conflicting-merge-in-git)
    36. [How to add a remote repository in Git](#how-to-add-a-remote-repository-in-git)
    37. [How to see remote URLs in Git](#how-to-see-remote-urls-in-git)
    38. [How to get more info about a remote repo in Git](#how-to-get-more-info-about-a-remote-repo-in-git)
    39. [How to push changes to a remote repo in Git](#how-to-push-changes-to-a-remote-repo-in-git)
    40. [How to pull changes from a remote repo in Git](#how-to-pull-changes-from-a-remote-repo-in-git)
    41. [How to check remote branches that Git is tracking](#how-to-check-remote-branches-that-git-is-tracking)
    42. [How to fetch remote repo changes in Git](#how-to-fetch-remote-repo-changes-in-git)
    43. [How to check the current commits log of a remote repo in Git](#how-to-check-the-current-commits-log-of-a-remote-repo-in-git)
    44. [How to merge a remote repo with your local repo in Git](#how-to-merge-a-remote-repo-with-your-local-repo-in-git)
    45. [How to get the contents of remote branches in Git without automatically merging](#how-to-get-the-contents-of-remote-branches-in-git-without-automatically-merging)
    46. [How to push a new branch to a remote repo in Git](#how-to-push-a-new-branch-to-a-remote-repo-in-git)
    47. [How to remove a remote branch in Git](#how-to-remove-a-remote-branch-in-git)
    48. [How to use Git rebase](#how-to-use-git-rebase)
    49. [How to run rebase interactively in Git](#how-to-run-rebase-interactively-in-git)
    50. [How to force a push request in Git](#how-to-force-a-push-request-in-git)
3. [Conclusion](#conclusion)

---

## What is a Distributed Version Control System?

A Distributed Version Control System (DVCS) is a type of version control where the complete codebase—including its full history—is mirrored on every developer's computer. This setup allows for more flexible workflows, including offline work, easier branching, and faster operations compared to centralized version control systems like SVN.

## Git Commands You Should Know

Understanding and mastering Git commands is essential for effective version control. Here's a comprehensive guide to help you get started and become proficient with Git.

## How to check your Git configuration

Before diving into Git commands, it's crucial to check your Git configuration to ensure everything is set up correctly.

```bash
git config --list
```

## How to setup your Git username

Your Git username is used to label your commits with your identity. Set it up using:

```bash
git config --global user.name "Your Name"
```

## How to setup your Git user email

Similar to the username, your email address is attached to your commits.

```bash
git config --global user.email "you@example.com"
```

## How to cache your login credentials in Git

To avoid entering your credentials repeatedly, you can cache them.

```bash
git config --global credential.helper cache
```

## How to initialize a Git repo

Start tracking your project with Git by initializing a repository.

```bash
git init
```

## How to add a file to the staging area in Git

Prepare your changes for a commit by adding them to the staging area.

```bash
git add <file>
```

## How to add all files in the staging area in Git

Add all your changes to the staging area at once.

```bash
git add .
```

## How to add only certain files to the staging area in Git

Sometimes, you only want to stage specific files.

```bash
git add <file1> <file2>
```

## How to check a repository’s status in Git

Check the status of your working directory and staging area.

```bash
git status
```

## How to commit changes in the editor in Git

Commit your staged changes with an editor for the commit message.

```bash
git commit
```

## How to commit changes with a message in Git

Commit your changes directly from the command line with a message.

```bash
git commit -m "Your commit message"
```

## How to commit changes (and skip the staging area) in Git

You can commit changes directly without staging them first.

```bash
git commit -a -m "Your commit message"
```

## How to see your commit history in Git

Review the history of your commits.

```bash
git log
```

## How to see your commit history including changes in Git

View commit history along with the changes introduced.

```bash
git log -p
```

## How to see a specific commit in Git

Inspect a particular commit in detail.

```bash
git show <commit-hash>
```

## How to see log stats in Git

Get a concise summary of your commit history.

```bash
git log --stat
```

## How to see changes made before committing them using “diff” in Git

Compare changes in your working directory.

```bash
git diff
```

## How to see changes using “git add -p”

Interactively select changes to stage.

```bash
git add -p
```

## How to remove tracked files from the current working tree in Git

Remove files from your working directory and staging area.

```bash
git rm <file>
```

## How to rename files in Git

Rename and move files within your repository.

```bash
git mv <oldfile> <newfile>
```

## How to ignore files in Git

Use a `.gitignore` file to exclude files from tracking.

```text
# .gitignore
node_modules/
*.log
```

## How to revert unstaged changes in Git

Discard changes in your working directory.

```bash
git checkout -- <file>
```

## How to revert staged changes in Git

Unstage changes while retaining them in your working directory.

```bash
git reset <file>
```

## How to amend the most recent commit in Git

Modify the last commit with new changes.

```bash
git commit --amend
```

## How to rollback the last commit in Git

Undo the last commit while keeping changes in the working directory.

```bash
git reset --soft HEAD~1
```

## How to rollback an old commit in Git

Revert to a specific commit.

```bash
git revert <commit-hash>
```

## How to create a new branch in Git

Branching allows you to work on different features or fixes.

```bash
git branch <branch-name>
```

## How to switch to a newly created branch in Git

Move to your new branch.

```bash
git checkout <branch-name>
```

## How to list branches in Git

See all branches in your repository.

```bash
git branch
```

## How to create a branch in Git and switch to it immediately

Create and switch to a new branch in one command.

```bash
git checkout -b <branch-name>
```

## How to delete a branch in Git

Remove branches that you no longer need.

```bash
git branch -d <branch-name>
```

## How to merge two branches in Git

Combine changes from different branches.

```bash
git merge <branch-name>
```

## How to show the commit log as a graph in Git

Visualize your commit history as a graph.

```bash
git log --graph
```

## How to show the commit log as a graph of all branches in Git

See the commit history graph for all branches.

```bash
git log --graph --all
```

## How to abort a conflicting merge in Git

If a merge goes wrong, you can abort it.

```bash
git merge --abort
```

## How to add a remote repository in Git

Link your local repository to a remote one.

```bash
git remote add origin <url>
```

## How to see remote URLs in Git

List the URLs of your remote repositories.

```bash
git remote -v
```

## How to get more info about a remote repo in Git

Detailed information about your remotes.

```bash
git remote show origin
```

## How to push changes to a remote repo in Git

Upload your local changes to a remote repository.

```bash
git push origin <branch-name>
```

## How to pull changes from a remote repo in Git

Fetch and merge changes from a remote repository.

```bash
git pull
```

## How to check remote branches that Git is tracking

List the remote branches your local repository is tracking.

```bash
git branch -r
```

## How to fetch remote repo changes in Git

Download changes from a remote repository without merging.

```bash
git fetch
```

## How to check the current commits log of a remote repo in Git

View the commit log for a remote repository.

```bash
git log origin/<branch-name>
```

## How to merge a remote repo with your local repo in Git

Combine changes from a remote repository into your local one.

```bash
git merge origin/<branch-name>
```

## How to get the contents of remote branches in Git without automatically merging

Fetch remote branches without merging them.

```bash
git fetch origin
```

## How to push a new branch to a remote repo in Git

Share your new branch with the remote repository.

```bash
git push origin <branch-name>
```

## How to remove a remote branch in Git

Delete a branch from the remote repository.

```bash
git push origin --delete <branch-name>
```

## How to use Git rebase

Rebase your current branch onto another branch.

```bash
git rebase <branch-name>
```

## How to run rebase interactively in Git

Use interactive rebase for a more controlled process.

```bash
git rebase -i <commit-hash>
```

## How to force a push request in Git

Forcefully push changes to a remote repository.

```bash
git push --force
```

## Conclusion

Mastering Git commands empowers you to manage your codebase efficiently, collaborate with others, and maintain a robust history of your project's development. Practice these commands and explore more advanced features as you grow more comfortable with Git.