Skip to content

Instantly share code, notes, and snippets.

@Mgregchi
Created May 14, 2025 14:37
Show Gist options
  • Save Mgregchi/2051b90001c9efe142072091258a32f4 to your computer and use it in GitHub Desktop.
Save Mgregchi/2051b90001c9efe142072091258a32f4 to your computer and use it in GitHub Desktop.

Git Cheat Sheet

Basic Git Workflow


1. Cloning a Repository

git clone is used to create a copy of a remote repository on your local machine.

git clone <repository-url>
  • What it does: This command copies the entire repository, including its history, branches, and files, from a remote server (like GitHub, GitLab, etc.) to your local machine. It initializes a new Git repository locally, links it to the remote, and checks out the default branch (usually main or master).

  • Example:

    git clone https://github.com/username/repository.git

2. Checking the Status

git status shows the state of the working directory and staging area.

git status
  • What it does: It helps you see:

    • Which changes have been staged (ready to be committed).
    • Which changes are in the working directory but haven't been staged.
    • If there are untracked files (files not yet added to Git).
  • Example:

    git status

.gitignore

A .gitignore file tells Git which files or directories to ignore in a repository. This is particularly useful when you want to avoid committing temporary files, build artifacts, or sensitive information that should not be included in version control.

How to use a .gitignore file

  1. Creating a .gitignore file:

Simply create a file named .gitignore in the root of your repository. This file should be placed in the same directory as your .git folder (which is the root directory of your project).

You can create the .gitignore file by running:

touch .gitignore

Or manually create the file using your code editor or file explorer.

  1. Structure of .gitignore:

Inside the .gitignore file, you specify patterns for files or directories you want Git to ignore. These can be specific files, file types, or entire directories.

Some common patterns used in .gitignore files:

  • *.log: Ignore all .log files.
  • *.tmp: Ignore all .tmp files.
  • node_modules/: Ignore the node_modules directory.
  • *.DS_Store: Ignore .DS_Store files (macOS-specific system files).
  • *.env: Ignore environment configuration files (often used for sensitive information like database passwords, API keys, etc.).

Examples of .gitignore content:

# Ignore all log files
*.log

# Ignore all .env files (commonly used for environment variables)
*.env

# Ignore node_modules folder
node_modules/

# Ignore build directories
build/
dist/

# Ignore macOS system files
.DS_Store

# Ignore IDE or text editor-specific files (e.g., VSCode)
.vscode/
.idea/

# Ignore npm-debug.log
npm-debug.log

# Ignore all files ending with .tmp
*.tmp
  1. Important Patterns:

    • Wildcard (*): Matches any string of characters (including no characters).

      *.log        # Ignore all files ending in .log
      temp/*       # Ignore everything in the 'temp' directory
      
    • Negating patterns (!): You can negate a pattern by prefixing it with !. This is useful if you want to include a specific file or directory that is otherwise excluded by a broader pattern.

      *.log       # Ignore all .log files
      !important.log  # Do not ignore 'important.log'
      
    • Commenting: You can add comments by starting the line with #.

      # Ignore all temporary files
      *.tmp
      
  2. How Git Uses .gitignore:

    • When you create a .gitignore file, Git will not track any files that match the patterns specified in this file, even if those files are added to the working directory.

    • However, files that were already tracked by Git (i.e., files that have already been committed to the repository) will not be removed automatically from Git's history if they are later added to .gitignore. In such cases, you would need to untrack the file manually using:

      git rm --cached <file-name>

      For example:

      git rm --cached node_modules/
      git commit -m "Stop tracking node_modules"

3. Adding Changes

git add stages changes to be included in the next commit.

git add <file-name>
  • What it does: It tells Git to start tracking changes to the specified files. You need to use git add before you can commit the changes. You can stage individual files or all files (git add . or git add -A).

  • Example:

    git add index.html  # Add a specific file
    git add .           # Add all modified files in the current directory

4. Committing Changes

git commit records the staged changes in the Git history.

git commit -m "Commit message"
  • What it does: It creates a snapshot of the changes that have been staged with git add. The -m flag allows you to specify a message describing the changes made. Commits should have clear, concise messages to describe what was changed.

  • Example:

    git commit -m "Fix typo in the header"

5. Viewing Commit History

git log shows the commit history of your current branch.

git log
  • What it does: Displays a list of recent commits on the current branch, showing each commit's hash, author, date, and message. You can add flags for more options (e.g., --oneline for a condensed view).

  • Example:

    git log --oneline  # Show a simplified, one-line commit history

6. Pushing Changes

git push uploads your local commits to a remote repository.

git push origin <branch-name>
  • What it does: Pushes your committed changes to the specified remote (origin) and branch. If you're working with a team, this is how you share your changes with others.

  • Example:

    git push origin main  # Push changes to the 'main' branch on remote

7. Pulling Changes

git pull fetches and merges changes from a remote repository into your local repository.

git pull origin <branch-name>
  • What it does: It fetches the latest changes from the remote repository and merges them into your current branch. It's often used to update your local branch with changes made by others before you start working.

  • Example:

    git pull origin main  # Pull latest changes from 'main' branch

8. Creating a Branch

git branch is used to create, list, or delete branches.

git branch <branch-name>
  • What it does: It creates a new branch in your repository. You can then switch to it using git checkout. Branches allow you to work on different features or fixes without affecting the main codebase.

  • Example:

    git branch feature-x  # Create a new branch called 'feature-x'

9. Switching Branches

git checkout or git switch is used to move between branches.

git checkout <branch-name>

or

git switch <branch-name>
  • What it does: It switches your working directory to the specified branch. Any changes you make will now affect this branch.

  • Example:

    git checkout feature-x  # Switch to 'feature-x' branch

10. Merging Branches

git merge is used to combine changes from one branch into another.

git merge <branch-name>
  • What it does: It merges the changes from the specified branch into your current branch. This is often done after completing work on a feature branch, merging it into the main branch.

  • Example:

    git checkout main    # First, switch to the branch you want to merge into
    git merge feature-x  # Merge 'feature-x' branch into 'main'

11. Deleting a Branch

git branch -d deletes a local branch.

git branch -d <branch-name>
  • What it does: It deletes a local branch that is no longer needed. Use -D for a force delete (useful if the branch has unmerged changes).

  • Example:

    git branch -d feature-x  # Delete the 'feature-x' branch locally

12. Viewing Remote Repositories

git remote shows the remotes linked to your local repository.

git remote -v
  • What it does: It lists the remote repositories associated with your project, showing their URL and the fetch/push details.

  • Example:

    git remote -v  # Show remote repositories and their URLs

13. Adding a Remote

git remote add links a remote repository to your local repository.

git remote add origin <remote-repository-url>
  • What it does: It adds a remote repository (often called origin) that allows you to push and pull changes to/from a central server (like GitHub, GitLab, etc.).

  • Example:

    git remote add origin https://github.com/username/repository.git

14. Fetching Changes

git fetch downloads new commits and data from a remote without merging them.

git fetch origin
  • What it does: It fetches all branches and tags from the remote repository, but doesn't merge them into your local branch. You can then decide to merge or inspect them later.

  • Example:

    git fetch origin  # Fetch changes from the remote but don't merge them

15. Reverting Changes

git revert undoes a commit by creating a new commit that reverses its changes.

git revert <commit-hash>
  • What it does: It creates a new commit that undoes the changes made in the specified commit. This is safer than git reset because it preserves the history.

  • Example:

    git revert 3f1d2a3  # Revert the commit with hash '3f1d2a3'

16. Resetting Changes

git reset moves the HEAD pointer and optionally changes the staging area and working directory.

git reset <commit-hash>
  • What it does: It allows you to undo commits, staging, or even changes in your working directory, depending on the flags used (--soft, --mixed, --hard).

  • Example:

    git reset --hard HEAD~1  # Reset to the previous commit and discard changes

17. Stashing Changes

git stash temporarily saves changes that are not yet ready to be committed.

git stash
  • What it does: It saves your local changes (both staged and unstaged) and reverts the working directory to the last commit. You can apply the stashed changes later.

  • Example:

    git stash  # Stash your changes
    git stash pop  # Apply the most recent stash

Summary

Here’s a high-level overview of common Git actions:

  1. Cloning: git clone <repo-url>
  2. Checking Status: git status
  3. Adding Files: git add <file>
  4. Committing: git commit -m "message"
  5. Pushing: git push origin <branch>
  6. Pulling: git pull origin <branch>
  7. Branching: git branch <branch-name>
  8. Merging: git merge <branch-name>
  9. Deleting: git branch -d <branch-name>
  10. Viewing History: git log
  11. Fetching: git fetch origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment