Git Cheatsheet
Tags: git, beginners, programming, tutorial
Published: 2023-09-07 12:05:02

Git is a powerful distributed version control system used for tracking changes in source code during software development. Whether you're new to Git or looking for a quick reference, this cheat sheet provides essential Git commands and concepts.

## Table of Contents

1. [Configuring Git](#configuring-git)
2. [Creating and Cloning Repositories](#creating-and-cloning-repositories)
3. [Basic Git Workflow](#basic-git-workflow)
4. [Branching and Merging](#branching-and-merging)
5. [Checking Status and History](#checking-status-and-history)
6. [Staging and Committing](#staging-and-committing)
7. [Undoing Changes](#undoing-changes)
8. [Collaboration and Remote Repositories](#collaboration-and-remote-repositories)
9. [Advanced Git](#advanced-git)

## Configuring Git

### Configure User Information

```bash
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
```

### Set Default Text Editor

```bash
git config --global core.editor "nano"  # Set your preferred text editor
```

## Creating and Cloning Repositories

### Initialize a New Repository

```bash
git init
```

### Clone a Repository

```bash
git clone repository_url
```

## Basic Git Workflow

### Check Repository Status

```bash
git status
```

### Update Local Repository

```bash
git pull origin branch_name
```

### Push Changes to Remote

```bash
git push origin branch_name
```

## Branching and Merging

### Create a New Branch

```bash
git checkout -b new_branch
```

### Switch Branches

```bash
git checkout branch_name
```

### Merge Branch into Current Branch

```bash
git merge branch_name
```

### Delete a Branch

```bash
git branch -d branch_name
```

## Checking Status and History

### View Changes in Files

```bash
git diff
```

### View Commit History

```bash
git log
```

### View Remote Branches

```bash
git remote -v
```

## Staging and Committing

### Stage Changes for Commit

```bash
git add filename
```

### Commit Changes

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

## Undoing Changes

### Discard Changes in Working Directory

```bash
git checkout -- filename
```

### Unstage Changes

```bash
git reset filename
```

### Undo the Last Commit (Keep Changes)

```bash
git reset HEAD~1
```

### Undo the Last Commit (Discard Changes)

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

## Collaboration and Remote Repositories

### Add a Remote Repository

```bash
git remote add remote_name repository_url
```

### Fetch Changes from a Remote Repository

```bash
git fetch remote_name
```

### Create a Pull Request

After pushing your branch to a remote repository, create a pull request via the repository's web interface.

### Review and Merge Pull Request

Review and merge pull requests on the repository's web interface.

## Advanced Git

### Rebase Commits

```bash
git rebase branch_name
```

### Resolve Merge Conflicts

Edit conflicted files, then commit changes.

### Tag a Commit

```bash
git tag tag_name
```

### Cherry-Pick Commits

```bash
git cherry-pick commit_hash
```

### Amend the Last Commit

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

This Git cheatsheet covers essential Git commands and concepts for everyday use. Refer to it as you work with Git to manage version control in your software development projects.

*Estimated Reading Time: 5 minutes*