# Cheat Sheet #day9 - GIT Commands

### Git Cheat Sheet

Git is a distributed version control system used to track changes in source code during software development. This cheat sheet covers the most essential Git commands and workflows.

`NOTE: Replace user names and emails to suit your environment setup accordingly !!`

#### Configuration
- **Set user name**:
  ```bash
  git config --global user.name "Cloud Tuned"
  ```
- **Set user email**:
  ```bash
  git config --global user.email "boss@cloudtuned.com"
  ```
- **Set default text editor**:
  ```bash
  git config --global core.editor "code --wait"
  ```
- **View all settings**:
  ```bash
  git config --list
  ```

#### Repository Setup
- **Initialize a new repository**:
  ```bash
  git init
  ```
- **Clone an existing repository**:
  ```bash
  git clone https://github.com/user/repo.git
  ```

#### Basic Commands
- **Check repository status**:
  ```bash
  git status
  ```
- **Track new files**:
  ```bash
  git add filename
  ```
- **Stage all changes**:
  ```bash
  git add .
  ```
- **Commit changes**:
  ```bash
  git commit -m "Commit message"
  ```
- **Add and commit changes**:
  ```bash
  git commit -am "Commit message"
  ```
- **Remove files from staging area**:
  ```bash
  git reset filename
  ```
- **Unstage everything**:
  ```bash
  git reset
  ```
- **Undo the last commit (keep changes)**:
  ```bash
  git reset --soft HEAD~
  ```
- **Undo the last commit (discard changes)**:
  ```bash
  git reset --hard HEAD~
  ```

#### Branching
- **List branches**:
  ```bash
  git branch
  ```
- **Create a new branch**:
  ```bash
  git checkout -b new-branch
  ```
- **Switch to a branch**:
  ```bash
  git checkout branch-name
  ```
- **Rename the current branch**:
  ```bash
  git branch -m new-branch-name
  ```
- **Delete a branch**:
  ```bash
  git branch -d branch-name
  ```

#### Merging and Rebasing
- **Merge a branch into the current branch**:
  ```bash
  git merge branch-name
  ```
- **Rebase the current branch onto another branch**:
  ```bash
  git rebase branch-name
  ```
- **Continue a rebase after resolving conflicts**:
  ```bash
  git rebase --continue
  ```
- **Abort a rebase**:
  ```bash
  git rebase --abort
  ```

#### Remote Repositories
- **Add a remote repository**:
  ```bash
  git remote add origin https://github.com/user/repo.git
  ```
- **List remote repositories**:
  ```bash
  git remote -v
  ```
- **Fetch changes from a remote repository**:
  ```bash
  git fetch
  ```
- **Push changes to a remote repository**:
  ```bash
  git push origin branch-name
  ```
- **Pull changes from a remote repository**:
  ```bash
  git pull
  ```

#### Tags
- **List tags**:
  ```bash
  git tag
  ```
- **Create a tag**:
  ```bash
  git tag tag-name
  ```
- **Delete a tag**:
  ```bash
  git tag -d tag-name
  ```
- **Push tags to a remote repository**:
  ```bash
  git push origin --tags
  ```

#### Stashing
- **Stash changes**:
  ```bash
  git stash
  ```
- **List stashes**:
  ```bash
  git stash list
  ```
- **Apply the last stash**:
  ```bash
  git stash apply
  ```
- **Apply a specific stash**:
  ```bash
  git stash apply stash@{index}
  ```
- **Drop a stash**:
  ```bash
  git stash drop stash@{index}
  ```
- **Pop the last stash (apply and remove)**:
  ```bash
  git stash pop
  ```

#### Logs and History
- **View commit history**:
  ```bash
  git log
  ```
- **View commit history with a graph**:
  ```bash
  git log --graph --oneline --all
  ```
- **Show changes for a specific commit**:
  ```bash
  git show commit-hash
  ```
- **Show changes between two commits**:
  ```bash
  git diff commit-hash1 commit-hash2
  ```

#### Undoing Changes
- **Revert a commit**:
  ```bash
  git revert commit-hash
  ```
- **Discard local changes to a file**:
  ```bash
  git checkout -- filename
  ```
- **Restore a deleted file**:
  ```bash
  git checkout commit-hash -- filename
  ```

#### Miscellaneous
- **Show help for a command**:
  ```bash
  git command --help
  ```
- **Show the current configuration**:
  ```bash
  git config --list
  ```
- **Generate an archive from a branch or tag**:
  ```bash
  git archive -o archive-name.tar.gz branch-name
  ```

This cheat sheet covers the most common Git commands and workflows. For more advanced usage and options, refer to the [Git documentation](https://git-scm.com/doc).
