Git Cheat Sheet
Tags: git, github, cheatsheet, cheat
Published: 2021-10-03 11:59:30

Git is an open source version control system that works locally to help developers work together on software projects that matter. This cheat sheet provides a quick reference to commands that are useful for working and collaborating in a Git repository (repo).

___
# Initializing
_Starting up Git within a project and getting it connected._

```
git init
```
Initializes (or starts) your current working directory (folder) as a Git repository (repo).

```
git clone https://www.github.com/username/repo-name
```
Copies an existing Git repo hosted remotely.

```
git remote  (or)  git remote -v
```
Shows your current Git directory's remote repo. Use the -v flag for more info.

```
git remote add upstream https://www.github.com/username/repo-name
```
Adds the Git upstream to a URL.

___
# Staging
_Creating files staged after modifying a file and marking it ready to go in the next commit._

```
git status
```
Checks the status of your Git repo, including files added that are not staged.

```
git add  .   (or)   git add my_script.js
```
Stages modified files. If you make changes that you want included in the next commit, you can run add again. Use "git add." for all files to be staged, or specify specific files by name.

```
git reset my_script.js
```
Removes a file from staging while retaining changes within your working directory.

___
# Committing
_Recording changes made to the repo._

```
git commit -m "Commit message"
```
Commits staged files with a meaningful commit message so that you and others can track commits.

```
git commit -am "Commit message"
```
Condenses all tracked files by committing them in one step.

```
git commit --amend -m "New commit message" 
```
Modifies your commit message.
 
___
# Branching
_Isolating work and managing feature development in one place._

```
git branch
```
Lists all current branches. An asterisk (*) will appear next to your currently active branch.

```
git branch new-branch
```
Creates a new branch. You will remain on your currently active branch until you switch to the new one.

```
git checkout another-branch
```
Switches to any existing branch and checks it out into your current working directory.

```
git checkout -b new-branch
```
Consolidates the creation and checkout of a new branch.

```
git branch -d branch-name
```
Deletes a branch.

___
# Collaborating and Sharing
_Downloading changes from another repository or sharing changes with the larger codebase._

```
git push origin main
```
Pushes or sends your local branch commits to the remote repo. Note: some repos use master instead of main in their commands.

```
git pull
```
Fetches and merges any commits from the tracking remote branch.

```
git merge upstream/main
```
Merges the fetched commits.

___
# Showing Changes
_See changes between commits, branches, and more._

```
git diff --staged
```
Compares modified files that are in the staging area.

```
git diff a-branch..b-branch
```
Displays the diff of what is in 'a-branch' but is not in 'b-branch'.

```
git diff 99abcde..1122gghh
```
Uses commit id to show the diff between two specific commits.

___
# Log
_The history of changes._

```
git log
```
Displays important information about our commit history, like the ID of the commits, the author, the date and where is the head.

```
git log --all --decorate --oneline --graph
```
Draws a graphical representation in one line and it's very visual.

___
# Download Git Cheatsheet
![Git Cheatsheet Single Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d29nlbojtknrgol13do8.jpg)

___
# Extra: Git Command - FlowChart
![Git Command FlowChart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zl9a4750ibaabz80l5ux.png)