Skip to content

A3Brothers/git-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Welcome to the "Git Gold Mine," your comprehensive guide to mastering Git, the version control system that powers collaborative software development. Whether you're a beginner looking to grasp the fundamentals or an experienced user aiming to deepen your Git knowledge, this guide has you covered.

The Git Command List

  1. Branch Management:

  2. Collaboration and Remotes:

    • git remote: Manage remote repositories.
    • git fetch: Fetch changes from remote repositories.
    • git pull: Fetch and merge changes from a remote repository.
    • git push: Publish changes to a remote repository.
    • git clone: Create a local copy of a remote repository.
  3. Stashing and Managing Changes:

    • git stash: Temporarily save and manage changes.
    • git clean: Remove untracked files and directories.
    • git reset: Reset changes in the working directory and/or index.
    • git revert: Reverse a previous commit's changes by creating a new commit that undoes it, preserving the commit history.
  4. History and Log:

    • git log: View commit history.
    • git show: Show detailed information about a specific commit.
    • git diff: View changes between commits, branches, or files.
    • git bisect: Find the commit that introduced a bug using binary search.
  5. Tagging and Releases:

    • git tag: Create and manage tags to mark specific commits as releases or milestones.
  6. Configurations and Settings:

    • git config: Set and view Git configuration settings.
    • .gitignore: Specify files and directories to be ignored by Git.
  7. Submodules:

    • git submodule: Manage and work with submodules within a Git repository.
  8. Advanced Topics:

  9. Git Hooks: Custom scripts that can be triggered by various Git events, allowing for automation and custom workflows.

  10. Git Workflows: Familiarize yourself with popular Git workflows like Git Flow, GitHub Flow, and GitLab Flow, depending on your team's needs.

Understanding these Git concepts and commands will provide you with a strong foundation for effectively using Git in various development scenarios and collaborative projects. Depending on your specific needs and project requirements, you can explore and deepen your knowledge in these areas to become proficient in Git.


Branch Management:

Command: git branch [options] [branch-name]

Purpose: git branch is used to manage branches in a Git repository. It allows you to list existing branches, create new branches, rename branches, delete branches, and switch between branches.

Examples:

  1. List all branches: Show a list of all local branches in the repository.

    git branch
  2. Create a new branch: Create a new branch based on the current branch.

    git branch new-feature
  3. Create a new branch starting from a specific commit: Create a new branch starting from a specific commit.

    git branch hotfix abc1234
  4. Switch to a branch: Change the working directory to the specified branch.

    git checkout existing-branch
  5. Create and switch to a new branch in one command: Create and immediately switch to a new branch.

    git checkout -b bug-fix
  6. Rename a branch: Rename an existing branch to a new name.

    git branch -m old-name new-name
  7. Delete a branch: Delete an existing branch (use with caution).

    git branch -d branch-to-delete

Common Options:

  • -a or --all: List both local and remote branches.
  • -d or --delete: Delete a branch (use -d for a safe delete that prevents data loss).
  • -m or --move: Rename a branch.
  • -r or --remotes: List remote-tracking branches.

Useful Tips:

  • When creating a branch, you can optionally specify the starting point (commit) by appending it to the branch name.
  • The current branch is indicated with an asterisk * when you run git branch.

Command: git checkout [options] <branch or commit>

Purpose: git checkout is used to switch between branches or commit snapshots in a Git repository. It allows you to navigate your project's history and work on different branches or inspect specific points in time.

Examples:

  1. Checkout a Branch:
  • To switch to an existing branch, use:
git checkout branch-name

Example: git checkout main

  1. Create and Checkout a New Branch:
  • To create a new branch and switch to it, use:
git checkout -b new-branch-name

Example: git checkout -b feature-branch

  1. Checkout a Specific Commit:
  • To temporarily move to a specific commit (creates a "detached HEAD" state), use:
git checkout commit-hash

Example: git checkout abc123

  1. Checkout a File from a Specific Commit:
  • To retrieve a specific version of a file from a commit, use:
git checkout commit-hash -- file-name

Example: git checkout abc123 -- app.js

  1. Create a New Branch from a Specific Commit and Checkout:
  • To create a new branch from a specific commit and switch to it, use:
git checkout -b new-branch-name commit-hash

Example: git checkout -b bugfix-branch abc123

  1. Discard Uncommitted Changes in a File:
  • To discard uncommitted changes in a file, use:
git checkout -- file-name

Example: git checkout -- config.txt

  1. Switch Back to the Previous Branch:
  • To return to the previously checked-out branch (after using git checkout for a specific commit), use:
git checkout -
  1. Checkout a Remote Branch:
  • To create and switch to a local branch that tracks a remote branch, use:
git checkout -t origin/remote-branch-name

Example: git checkout -t origin/feature-branch

  1. Checkout a Tag:
  • To switch to the state of the code at a specific tag (creates a "detached HEAD" state), use:
git checkout tag-name

Example: git checkout v1.0

These are some common scenarios for using git checkout in different situations, including branch switching, commit navigation, file retrieval, and more.

Command: git merge [options] <branch>

Purpose: git merge is used to integrate changes from one branch into another. It combines the commit history of two branches, creating a new merge commit if necessary. This facilitates collaboration and code synchronization among team members.

Examples:

  1. Merge a Branch into the Current Branch:
  • To incorporate changes from one branch into the current branch, use:
git merge branch-name

Example: git merge feature-branch

  1. Fast-Forward Merge (Linear History):
  • When the current branch can be updated directly to match another branch (no divergent changes), Git performs a fast-forward merge:
git merge branch-name

Example: git merge hotfix-branch

  1. Create a Merge Commit:
  • If there are divergent changes, Git creates a new merge commit to combine them:
git merge branch-name

Example: git merge develop

  1. Conflict Resolution:
  • When there are conflicting changes, manually resolve conflicts in affected files before completing the merge:
git merge branch-name

# Resolve conflicts...

git add resolved-file

git commit -m "Merge branch-name"
  1. Non-Interactive Merge with a Specific Commit:
  • Merge specific commits from another branch into the current branch without interactive prompts:
git merge <commit-hash>

Example: git merge abc123

  1. Abort a Merge:
  • To cancel a merge in progress and return to the pre-merge state, use:
git merge --abort
  1. Three-Way Merge (Typical Merge):
  • Git performs a three-way merge by comparing the common ancestor, source branch, and target branch to create a new merge commit:
git merge source-branch

Example: git merge feature-branch

  1. Octopus Merge (Multiple Branches):
  • Combine changes from multiple branches simultaneously using an octopus merge:
git merge branch1 branch2 branch3

Example: git merge feature1 feature2

These are common scenarios for using git merge to integrate changes from one branch into another, manage divergent development, and resolve conflicts when necessary.

Command: git rebase [options] <base> or git rebase -i <base>

Purpose: git rebase is used to reapply a series of commits onto a new base commit. It's often used to integrate changes from one branch into another and rewrite commit history.

Using git rebase to Reorganize Commits

  1. Rebase onto Another Branch:

    • To rebase your current branch onto another branch, use:
      git rebase target-branch
      Example: git rebase main
  2. Interactive Rebase (Edit, Reorder, Squash Commits):

    • Use interactive rebase to edit, reorder, squash, or split commits interactively:
      git rebase -i target-branch
      Example: git rebase -i main
  3. Rebase to a Specific Commit:

    • To rebase to a specific commit or commit hash, use:
      git rebase <commit-hash>
      Example: git rebase abc123
  4. Rebase with Autostash (Preserve Local Changes):

    • When you have local changes, use --autostash to temporarily stash and apply them during rebase:
      git rebase --autostash target-branch
  5. Rebase Onto Remote Branch:

    • To rebase onto a remote branch, specify the remote and branch name:
      git rebase origin/remote-branch
      Example: git rebase origin/feature-branch
  6. Abort a Rebase:

    • To abort a rebase in progress and return to the pre-rebase state, use:
      git rebase --abort
  7. Continue a Rebase After Conflict Resolution:

    • After resolving conflicts during a rebase, continue with:
      git rebase --continue
  8. Skip a Commit During Interactive Rebase:

    • In an interactive rebase, delete the line corresponding to a commit to skip it:
      pick <commit-hash>   # Change 'pick' to 'd' or 'drop'
  9. Edit a Commit During Interactive Rebase:

    • In an interactive rebase, change 'pick' to 'edit' to amend a commit:
      pick <commit-hash>   # Change 'pick' to 'edit'

These are common scenarios for using git rebase to reorganize and manage your commit history, create cleaner and more logical histories, and integrate changes from one branch into another.

Command: git cherry-pick [options] <commit>

Purpose: git cherry-pick is used to apply the changes from one or more specific commits to your current branch. It allows you to selectively choose commits from one branch and apply them to another branch.

Examples:

  1. Cherry-pick a single commit:

    git cherry-pick abc1234

    This command applies the changes introduced by the commit abc1234 to your current branch.

  2. Cherry-pick multiple commits:

    git cherry-pick abc1234 def5678

    You can cherry-pick multiple commits by specifying their commit hashes one after another.

  3. Cherry-pick a range of commits:

    git cherry-pick xyz7890^..abc1234

    This cherry-picks all commits in the range from xyz7890 (exclusive) to abc1234 (inclusive).

  4. Cherry-pick a commit from another branch:

    git cherry-pick feature-branch~3

    You can cherry-pick a commit from another branch by specifying the branch name and the commit's relative position.

Common Options:

  • -e or --edit: Edit the commit message before applying the cherry-picked commit.
  • -x: Append a line to the commit message indicating that it was cherry-picked.
  • -n or --no-commit: Apply the changes but don't create a new commit. Allows for further modifications before committing.

Use Cases:

  • Cherry-picking is useful when you want to selectively bring specific changes from one branch into another without merging the entire branch.
  • It's commonly used for applying bug fixes or features from feature branches to the main or master branch.

Collaboration and Remotes:

Command: git remote [options]

Purpose: git remote is used to manage connections to remote repositories. It allows you to list, add, or remove remote repositories linked to your local Git project. Remote repositories are used for collaboration and code sharing.

Examples:

  1. Add a Remote Repository:

    • You can add a remote repository using the git remote add command, specifying a name for the remote and its URL.
    git remote add remote-name remote-URL

    Example:

    git remote add origin https://github.com/your-username/your-repo.git
  2. Add Another Remote Repository:

    • You can add another remote repository using the same git remote add command with a different name and URL.
    git remote add another-remote remote-URL

    Example:

    git remote add upstream https://github.com/upstream-user/upstream-repo.git
  3. List Remote Repositories:

    • To see a list of all the remote repositories associated with your local repository, use:
    git remote -v

    Example output:

    origin   https://github.com/your-username/your-repo.git (fetch)
    origin   https://github.com/your-username/your-repo.git (push)
    upstream https://github.com/upstream-user/upstream-repo.git (fetch)
    upstream https://github.com/upstream-user/upstream-repo.git (push)
    

Removing Remote Repositories:

  1. Remove a Remote Repository:

    • To remove a remote repository, use the git remote remove or git remote rm command followed by the remote's name.
    git remote remove remote-name

    Example:

    git remote remove another-remote

    After running this command, the specified remote (another-remote in the example) will be removed from your local Git repository's configuration.

Now, you have learned how to both add and remove remote repositories from your local Git repository. Adding remotes allows you to collaborate with multiple contributors and fetch/push to different remote locations, while removing remotes is useful when you no longer need to interact with a specific remote.

Command: git fetch [options] [remote]

Purpose: git fetch is used to retrieve changes and updates from a remote repository. It fetches new branches, commits, and other data from the specified remote repository, making them available locally for review or merging.

Examples:

  1. Fetch Remote Branches:

    • Use git fetch to retrieve updates from a remote repository, including all branches:
      git fetch
  2. Fetch a Specific Remote:

    • Fetch updates from a specific remote repository (e.g., origin):
      git fetch remote-name
      Example: git fetch upstream
  3. View Remote Changes:

    • After fetching, you can view remote branches and their changes:
      git branch -r
  4. Fetch a Specific Branch:

    • Fetch updates for a specific remote branch without merging:
      git fetch remote-name branch-name
      Example: git fetch origin feature-branch
  5. Fetch All Remotes:

    • Fetch updates from all remotes:
      git fetch --all
  6. Fetch and Prune Deleted Branches:

    • Remove local references to remote branches that no longer exist:
      git fetch --prune
  7. Fetch Tags and Objects:

    • Fetch tags and objects from the remote repository (useful for updated tags):
      git fetch --tags
  8. Fetch a Specific Tag:

    • Fetch a specific tag from the remote repository:
      git fetch remote-name tag-name
      Example: git fetch origin v1.0

These are common scenarios for using git fetch to retrieve updates and changes from remote repositories without automatically merging them into your local branches. git fetch helps you keep your local repository in sync with remote repositories and provides visibility into the changes made by others.

Command: git pull [options] [remote] [branch]

Purpose: git pull is used to update your local repository with changes from a remote repository. It fetches changes from a specified remote branch and merges them into your current local branch.

Examples:

  1. Pull changes from the default remote and branch (usually origin/master):

    git pull

    This command fetches changes from the default remote (origin) and the currently checked out branch and merges them into your current branch.

  2. Pull changes from a specific remote and branch:

    git pull upstream develop

    Here, upstream is the name of the remote, and develop is the branch. It fetches changes from the develop branch of the upstream remote and merges them into your current branch.

  3. Pull changes from a remote and branch and rebase instead of merge:

    git pull --rebase origin feature-branch

    This command fetches changes from the feature-branch of the origin remote and rebases your current branch onto it instead of merging.

Common Options:

  • --rebase: Rebase your current branch instead of merging.
  • --ff-only: Ensure that the pull is only a fast-forward merge; it won't create a merge commit if fast-forward is not possible.
  • --no-commit: Fetch the changes but don't create a merge commit. Allows for further modifications before committing.

Useful Tips:

  • If you want to fetch changes without merging or rebasing, you can use git fetch instead.
  • The default remote is typically named origin, but you can configure additional remotes for your repository.

Command: git push [options] [remote] [branch]

Purpose: git push is used to send your local commits to a remote repository, updating the remote branch with your changes.

Examples:

  1. Push the current branch to its remote counterpart:

    git push

    This command pushes your current branch to its remote counterpart, typically using the same branch name.

  2. Push a specific branch to its remote counterpart:

    git push origin my-feature-branch

    Here, my-feature-branch is the name of the branch you want to push to the origin remote.

  3. Push to a remote branch with a different name:

    git push origin local-branch-name:remote-branch-name

    This command pushes the local-branch-name to remote-branch-name on the origin remote.

  4. Push all branches to the remote repository:

    git push --all origin

    This pushes all local branches to the origin remote.

Common Options:

  • -u or --set-upstream: Sets the upstream branch for the current branch, allowing you to use git push and git pull without specifying the remote and branch names each time.

Useful Tips:

  • To set up the upstream branch for your local branch, use the -u or --set-upstream flag with your initial git push command. For example:
    git push -u origin my-feature-branch
    This associates your local branch with its remote counterpart, making future pushes and pulls more convenient.

Command: git clone [options] <repository> [directory]

Purpose: git clone is used to create a copy of a remote Git repository on your local machine. It allows you to start working on a project by copying its entire history, branches, and files.

Examples:

  1. Clone a remote repository into a new directory:

    git clone https://github.com/example/repo.git

    This command creates a new directory called repo and copies the contents of the remote repository into it.

  2. Clone a specific branch from a remote repository:

    git clone -b my-branch https://github.com/example/repo.git

    This command clones only the my-branch from the remote repository into a new directory.

  3. Clone a repository into a custom directory:

    git clone https://github.com/example/repo.git my-custom-directory

    This command clones the remote repository into a directory named my-custom-directory.

Common Options:

  • -b <branch>: Clone a specific branch from the remote repository.
  • --depth <depth>: Clone a shallow repository with a limited commit history.
  • --single-branch: Clone only a single branch from the remote repository.
  • --recursive: Clone submodules as well (if the repository contains submodules).

Explanation of -u (or --update-head-ok) Flag:

  • When you clone a repository using git clone, Git sets up a default remote named origin.
  • The -u flag is used to set up an upstream tracking relationship for the default branch of the cloned repository.
  • This means that if you create new branches based on the default branch, Git will automatically track the corresponding branch on the remote repository as its upstream branch.

Example of Using -u Flag:

git clone -u origin https://github.com/example/repo.git

In this example, the -u flag is used to set up an upstream relationship between the local default branch and the origin remote's default branch. This is helpful for tracking changes and performing git pull without specifying the remote and branch names.

Stashing and Managing Changes:

Command: git stash [options]

Purpose: git stash is used to temporarily save changes in your working directory that are not ready to be committed. It allows you to switch branches or perform other tasks without committing or discarding your changes.

Examples:

  1. Stash changes in the working directory:

    git stash

    This command stashes all changes in your working directory, including staged and unstaged changes.

  2. Stash changes with a message:

    git stash save "Feature work in progress"

    You can provide a descriptive message to identify the stash.

  3. List stashes:

    git stash list

    View a list of stashes with their unique identifiers.

  4. Apply the most recent stash and remove it:

    git stash pop

    This command applies the most recent stash to your working directory and removes it from the stash list.

  5. Apply a specific stash and remove it:

    git stash pop stash@{2}

    You can apply a specific stash by specifying its unique identifier.

  6. Apply the most recent stash without removing it:

    git stash apply

    This command applies the most recent stash but keeps it in the stash list.

Common Options:

  • --keep-index: Stash changes in the working directory but not the staged changes.
  • --all: Stash changes in the working directory, including untracked files.

Useful Tips:

  • Stashes are stored as a stack, and you can have multiple stashes.
  • You can apply stashes in any order, even if they are not the most recent ones.
  • Stashes can be especially useful when you need to switch branches or perform other tasks without committing half-finished work.

Command: git clean [options]

Purpose: git clean is used to remove untracked files and directories from your working directory. It helps keep your project clean by deleting files that are not being tracked by Git, making it useful for managing temporary or generated files.

Examples:

  1. The git clean command is used to remove untracked files and directories from your working directory.

  2. By default, git clean will only show a preview of what it intends to remove. To actually remove untracked files, you need to use the -f (force) option.

  3. To remove untracked directories as well, use the -d (directory) option.

  4. Be extremely cautious with git clean -f, as it will permanently delete untracked files and directories.

Example: Using git clean

Suppose your working directory contains untracked files and directories:

project/
  ├── file1.txt
  ├── file2.txt
  ├── temp/
  │    ├── temp_file.txt
  │    └── cache/
  │         ├── cache_file1.txt
  │         └── cache_file2.txt
  └── build/
       ├── binary.exe
       └── logs/
            ├── log1.txt
            └── log2.txt

To remove all untracked files and directories, you can run:

git clean -df

After running this command, all untracked files and directories will be permanently deleted, leaving your working directory clean:

project/
  ├── file1.txt
  └── file2.txt

Important: Use git clean with caution, especially with the -f option, as it can lead to data loss if you accidentally remove files or directories you didn't intend to delete. Always double-check what will be removed using the preview mode (-n or --dry-run) before running git clean -f.

Command: git reset [options] <commit>

Purpose: git reset is used to move the current branch and the branch's HEAD reference to a specified commit. It can be used to unstage changes, undo commits, or reset the branch to a previous state.

Examples:

  1. Unstage changes but keep them in the working directory:

    git reset

    This command resets the staging area but leaves the changes in the working directory intact.

  2. Unstage specific files:

    git reset file1.txt file2.txt

    You can unstage specific files by listing their names.

  3. Soft reset to a previous commit (keep changes in staging):

    git reset --soft abc1234

    This command moves the branch pointer and HEAD to abc1234, but it keeps the changes staged for a new commit.

  4. Mixed reset to a previous commit (unstage changes):

    git reset --mixed xyz7890

    The branch and HEAD move to xyz7890, and changes are unstaged, but they are still present in the working directory.

  5. Hard reset to a previous commit (discard changes):

    git reset --hard def5678

    This command moves the branch and HEAD to def5678 and discards all changes in both the staging area and the working directory.

Common Options:

  • --soft: Reset while keeping changes staged.
  • --mixed (default): Reset while unstaging changes but keeping them in the working directory.
  • --hard: Reset while discarding all changes in both staging and working directories.

Useful Tips:

  • Be cautious when using git reset --hard, as it can lead to data loss.
  • You can specify commits by their SHA-1 hashes or use relative references (e.g., HEAD~2).

Command: git revert [options] <commit>

Purpose: git revert is used to create a new commit that undoes the changes introduced by a specified commit or range of commits. It is a safe way to reverse the effects of previous commits while preserving the commit history.

Examples:

  1. Revert a single commit:

    git revert abc1234

    This command creates a new commit that undoes the changes made in commit abc1234.

  2. Revert a range of commits:

    git revert abc1234..def5678

    Reverting a range of commits creates a new commit for each of them, effectively undoing their changes in reverse order.

  3. Revert a commit and edit the commit message:

    git revert -e abc1234

    Use the -e option to open an editor to modify the commit message before creating the revert commit.

  4. Revert a merge commit:

    git revert -m 1 merge-commit

    When dealing with merge commits, specify which parent (1 or 2) to consider as the mainline with the -m option.

  5. Revert a commit but don't create a new commit yet:

    git revert -n abc1234

    Use the -n option to apply the changes to the working directory but not create a new commit immediately. This allows you to make additional modifications before committing.

Common Options:

  • -e or --edit: Edit the commit message before creating the revert commit.
  • -n or --no-commit: Apply the changes to the working directory but don't create a new commit right away.

Useful Tips:

  • git revert is a non-destructive way to undo changes because it creates new commits that record the reversal of previous commits.
  • Each revert commit is a separate record of the undo operation in the commit history.

History and Log:

Command: git log [options]

Purpose: git log is used to display a chronological list of commit history in a Git repository. It provides information about commits, such as commit messages, authors, dates, and commit hashes.

Examples:

  1. Display the entire commit history:

    git log

    This command displays the entire commit history in reverse chronological order, with the most recent commit at the top.

  2. Display a specific number of recent commits:

    git log -n 5

    Use the -n option to limit the output to a specific number of recent commits (e.g., the last 5 commits).

  3. Display a specific branch's commit history:

    git log my-feature-branch

    You can specify a branch name to view the commit history of that branch.

  4. Display a commit's details and changes:

    git log -p abc1234

    Use the -p option to display the patch (changes) introduced by a specific commit (abc1234 in this example).

Common Options:

  • --oneline: Display each commit on a single line, showing only the commit hash and the first line of the commit message. This is useful for a more concise view of commit history.

Useful Tips:

  • You can navigate through the commit history using keyboard shortcuts (e.g., q to exit git log).
  • Combine git log with other options and filters to view specific parts of the commit history (e.g., by author, date, or file).

Example Using --oneline:

git log --oneline

This command displays a condensed commit history with each commit represented on a single line, showing the commit hash and the first line of the commit message.

Command: git show [commit]

Purpose: git show is used to display detailed information about a specific commit in your Git repository, including the commit message, author, date, and the actual changes made in that commit.

Examples:

  1. Show the most recent commit:

    git show
  2. Show information about a specific commit by its hash (SHA-1):

    git show abc1234
  3. Show information about a commit relative to HEAD (e.g., the last commit, HEAD~1 for the second-to-last commit, etc.):

    git show HEAD~2

Information Displayed by git show:

  • Commit Details: Commit hash, author, date, and commit message.
  • Changes: A unified diff of the changes introduced by the commit, including added, modified, and deleted lines in the affected files.
  • Patch: A detailed patch showing the exact changes made in the commit.
  • Tree: The tree object associated with the commit, which represents the state of the project at that commit.

Useful Options:

  • -p or --patch: Shows the changes in patch format (same as the default behavior).
  • --stat: Displays a summary of changes, showing how many files were changed and the number of lines added or removed.
  • --name-only: Lists only the names of the files changed in the commit.

Example Output:

commit abc1234def5678
Author: John Doe <john@example.com>
Date:   Fri Oct 15 14:25:37 2023 -0700

    Update README.md

diff --git a/README.md b/README.md
index 1234567..9876543 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
 # My Project

-This is a sample project.
+This is a sample project with updated information.

Command: git diff [options] [<commit>] [--] [<path>...]

Purpose: git diff is used to show differences between commits, branches, the working directory, or specific files in a Git repository.

Examples:

  1. Show changes in the working directory (unstaged changes):

    git diff

    This command displays the differences between the working directory and the last commit.

  2. Show changes between two commits:

    git diff abc1234 def5678

    Replace abc1234 and def5678 with the commit hashes to compare changes between those two commits.

  3. Show changes in a specific file:

    git diff file.txt

    This command displays the differences in the specified file (file.txt) between the working directory and the last commit.

  4. Show changes in a specific directory:

    git diff directory/

    Display differences in all files within a specific directory (directory/) between the working directory and the last commit.

Common Options:

  • --cached: Show changes in the staging area (between the last commit and the current staged changes).
  • --stat: Display a summary of changes, showing the number of insertions and deletions per file.
  • --color: Highlight the differences in color for better readability.

Useful Tips:

  • You can compare different branches, tags, or commits by specifying them as arguments to git diff.
  • Use git diff --cached to see changes in the staging area before committing.
  • Pipe the output of git diff to a file or a text comparison tool for more detailed analysis.

Command: git bisect [options]

Purpose: git bisect is used for binary searching through the commit history to find the commit that introduced a bug or issue. It helps you efficiently identify when a problem was introduced.

Examples:

  1. Start a Bisect Session:

    • Begin a git bisect session to find the commit where a bug was introduced or where a bug is fixed:
      git bisect start
  2. Mark a Good and Bad Commit:

    • Mark a known "good" commit where the bug is not present and a "bad" commit where the bug is known to exist:
      git bisect good <commit>
      git bisect bad <commit>
  3. Automated Binary Search:

    • Git will automatically check out a commit between the "good" and "bad" points for you to test. You'll need to test each commit and specify if it's "good" or "bad."
  4. Mark Commit as Good or Bad:

    • After testing each commit, mark it as "good" or "bad" using:
      git bisect good   # If the bug is not present
      git bisect bad    # If the bug is present
  5. Repeat Testing and Marking:

    • Continue testing and marking commits until Git identifies the specific commit introducing or fixing the bug.
  6. Finish Bisect Session:

    • When Git identifies the culprit commit, it will display the commit hash. Finish the bisect session:
      git bisect reset

Example Usage:

Suppose you have a bug, and you know it was working in commit abc123 but is broken in commit def456. You can use git bisect to find the exact commit where the bug was introduced:

# Start the bisect session
git bisect start

# Mark the known good and bad commits
git bisect good abc123
git bisect bad def456

# Git will automatically check out a commit for testing.
# Test and mark it as good or bad, and repeat the process.

# When Git identifies the bad commit, finish the bisect session
git bisect reset

git bisect helps you perform a binary search through your commit history to pinpoint when a bug was introduced or fixed, making it an efficient debugging tool.

Tagging and Releases:

Command: git tag [options] <tagname>

Purpose: git tag is used to create, list, and manage tags in a Git repository. Tags are used to mark specific commits as significant points in the project's history.

Examples:

  1. Creating Tags:

    • git tag <tagname>: Creates a tag for the last commit on the current branch.
  2. Creating Annotated Tags:

    • git tag -a <tagname> -m "Tag message": Creates an annotated tag with a message for the last commit.
  3. Listing Tags:

    • git tag: Lists all tags in the repository.
  4. Viewing Tag Details:

    • git show <tagname>: Displays details about a specific tag, including its associated commit and message.
  5. Deleting Tags:

    • git tag -d <tagname>: Deletes a tag locally.
      • If the tag points to a deleted commit, it will remove the tag reference.
      • If the tag is not pointing to a deleted commit, it will simply delete the tag.
  6. Pushing Tags:

    • git push origin <tagname>: Pushes a specific tag to a remote repository.
    • git push origin --tags: Pushes all tags to a remote repository.
  7. Checking Out a Tag:

    • git checkout <tagname>: Switches to the state of the code at the time of the specified tag, in a "detached HEAD" state.
  8. Creating Tags for Specific Commits:

    • git tag <tagname> <commit>: Creates a tag for a specific commit by specifying its SHA-1 hash or a branch/tag name.

Common Options:

  • -a or --annotate: Create an annotated tag with a message.
  • -m or --message: Specify a message when creating an annotated tag.
  • -l or --list: List tags matching a pattern.
  • -d or --delete: Delete a tag.

Useful Tips:

  • Annotated tags store extra information like tagger name, email, and a message, making them more informative.
  • Lightweight tags are just pointers to specific commits and do not store additional information.
  • Tags are often used to mark release points in a project's history.

Configurations and Settings:

Command: git config [options]

Purpose: git config is used to set or display configuration options in a Git repository. These options can be related to your identity, repository settings, or Git behavior.

Examples:

  1. Set your name and email globally:

    git config --global user.name "Your Name"
    git config --global user.email "youremail@example.com"

    These commands set your name and email address globally for all Git repositories on your machine.

  2. Set a repository-specific configuration option:

    git config user.signingkey your-gpg-key

    This command sets a repository-specific configuration option. In this example, it sets the GPG key for signing commits.

  3. List all global configuration settings:

    git config --global --list

    This command displays a list of all global configuration settings.

  4. List repository-specific configuration settings:

    git config --list

    This command displays a list of all configuration settings specific to the current Git repository.

Common Options:

  • --global: Specifies that you want to set or display a global configuration option (affects all repositories).
  • --local: Specifies that you want to set or display a repository-specific configuration option (only affects the current repository).
  • --unset: Removes a configuration option.
  • --get: Displays the value of a configuration option.

Useful Tips:

  • Configuration options can be used to customize various aspects of Git, including your identity, text editor, merging behavior, and more.
  • You can edit the Git configuration file directly (usually located at ~/.gitconfig for global settings) if you prefer not to use the git config command.

File: .gitignore

Purpose: The .gitignore file is used to specify files and directories that should be ignored by Git. This is useful for excluding files that are not relevant to version control, such as build artifacts, temporary files, and dependencies.

Examples:

  1. Ignore a specific file:

    filename.txt
    

    This entry will ignore a file named filename.txt.

  2. Ignore all files with a certain extension:

    *.log
    

    This entry will ignore all files with the .log extension.

  3. Ignore all files in a specific directory:

    mydirectory/
    

    This entry will ignore all files and directories within mydirectory.

  4. Ignore all files in a specific directory, but not its subdirectories:

    mydirectory/*
    

    This entry will ignore all files directly inside mydirectory but not its subdirectories.

  5. Ignore a specific directory and its contents:

    mydirectory/
    

    This entry will ignore mydirectory and all of its files and subdirectories.

  6. Ignore all files and directories with a specific name:

    node_modules/
    

    This entry will ignore any directory named node_modules and all of its contents.

  7. Use a wildcard to ignore files with similar names:

    debug*.log
    

    This entry will ignore files like debug.log, debug1.log, debug2.log, and so on.

Common Patterns:

  • *: Matches any sequence of characters within a filename or directory name.
  • ?: Matches a single character.
  • **: Matches zero or more directories or files recursively.

Useful Tips:

  • The .gitignore file can be placed in the root directory of your Git repository, and Git will automatically apply the rules to all files and directories within the repository.
  • You can create a global .gitignore file in your home directory (usually ~/.gitignore_global) to define patterns that apply to all your Git repositories.

File: .gitattributes

Purpose: The .gitattributes file is used to specify attributes and behaviors for files and paths in a Git repository. It allows you to control how Git handles things like line endings, merge strategies, and binary vs. text files.

Examples:

  1. Set text attribute for all *.txt files:

    *.txt text
    

    This entry specifies that all files with the .txt extension should be treated as text files, ensuring consistent line endings.

  2. Set eol attribute to lf for specific files:

    *.sh eol=lf
    

    This entry enforces the use of LF (Unix-style) line endings for all *.sh shell script files.

  3. Force binary mode for a specific file:

    binaryfile.bin -text
    

    This entry forces Git to treat binaryfile.bin as a binary file, preventing line ending conversions.

  4. Specify a merge driver for a file:

    conflictfile.txt merge=ours
    

    This entry sets a custom merge strategy (ours in this case) for resolving conflicts in conflictfile.txt.

  5. Ignore whitespace differences for a specific file:

    myfile.txt diff=whitespace
    

    This entry tells Git to ignore whitespace differences when comparing myfile.txt.

Common Attributes:

  • text: Specifies that a file should be treated as a text file, and line endings should be normalized.
  • eol: Controls line ending conversions (e.g., eol=lf for Unix line endings).
  • binary: Marks a file as binary, preventing any line ending conversions.
  • merge: Specifies custom merge strategies for resolving conflicts.
  • diff: Defines custom diff drivers for comparing files.

Useful Tips:

  • You can apply attributes to specific file patterns or even individual files.
  • .gitattributes can help ensure consistent handling of line endings, encoding, and merging behavior across different platforms and text editors.

Submodules:

Concept: Git submodules are a way to include one Git repository as a subdirectory inside another Git repository. This is useful for managing dependencies, incorporating external code, or keeping related projects together.

Examples:

  1. Adding a submodule to a repository:

    git submodule add <repository_url> <submodule_directory>
    • <repository_url> is the URL of the Git repository you want to include.
    • <submodule_directory> is the path where you want the submodule to be placed within your repository.
  2. Cloning a repository with submodules:

    git clone <repository_url> --recursive

    To clone a repository with its submodules, use the --recursive flag.

  3. Initializing and updating submodules:

    git submodule init
    git submodule update

    After cloning or adding submodules, use these commands to initialize and update them.

  4. Updating all submodules to their latest commits:

    git submodule update --remote

    This command fetches and updates all submodules to the latest commits.

  5. Checking out a specific commit in a submodule:

    cd <submodule_directory>
    git checkout <commit>
    cd ..
    git add <submodule_directory>
    git commit -m "Updated submodule to a specific commit"

    This sequence of commands allows you to set a specific commit in a submodule.

Useful Tips:

  • Submodules allow you to include external code while keeping it in a separate repository, making it easier to manage dependencies.
  • Remember to commit changes in the main repository after updating a submodule to record the new submodule commit.
  • Other developers who clone your repository need to run git submodule update --init to initialize and fetch submodules.

Advanced Topics:

Command: git reflog [options]

Purpose: git reflog is used to display a log of reference changes in a Git repository. It provides a detailed history of branch checkouts, commit resets, and other changes to references.

Examples:

  1. View Reflog for the Current Branch:

    • To see the reflog for the current branch, use:
      git reflog
  2. View Reflog for a Specific Branch:

    • To view the reflog for a specific branch, specify the branch name:
      git reflog branch-name
      Example: git reflog feature-branch
  3. Recover Lost Commits (Undo Mistakes):

    • If you accidentally delete a branch or reset to an unwanted state, use reflog to identify lost commits and recover them:
      git checkout -b new-branch-name <commit-hash>
      Example: git checkout -b recovery-branch HEAD@{3}
  4. Inspect Commit History (Before Recovery):

    • Review the commit history in the reflog to find the commit you want to recover:
      git reflog show branch-name
      Example: git reflog show main
  5. Find Reset or Rebase Points:

    • Use reflog to identify points where branches were reset or rebased:
      git reflog show branch-name
      Example: git reflog show feature-branch
  6. View Reference Changes (Moving HEAD and Branches):

    • Track references moving over time, such as branch creation, deletion, and checkout operations.
  7. Maintain Safety with Git Operations:

    • Reflog acts as a safety net, helping you recover from mistakes or unintended changes by providing a detailed history of reference updates.

These are common scenarios for using git reflog to inspect reference history, recover lost commits, and understand the state of your Git repository. It's a valuable tool for undoing mistakes and maintaining the integrity of your project's history.

Command: git annotate [options] <file>

Purpose: git annotate (or git blame) is used to display the author and revision information for each line in a file. It helps you determine who last modified each line of code in a file and when.

Examples:

  1. Annotate a file to see the author and commit info for each line:

    git annotate myfile.txt

    This command displays an annotated version of myfile.txt with author and commit details for each line.

  2. Limit annotation to a specific range of lines:

    git annotate -L 10,20 myfile.txt

    This command annotates only lines 10 to 20 of myfile.txt.

  3. Ignore whitespace changes when annotating:

    git annotate -w myfile.txt

    Use the -w option to ignore whitespace changes when annotating.

  4. Display the long format (includes commit messages):

    git annotate -p myfile.txt

    The -p option shows the long format, including the commit messages.

Common Options:

  • -L <start>,<end>: Limits annotation to a specific range of lines.
  • -w: Ignores whitespace changes.
  • -p: Shows the long format with commit messages.

Useful Tips:

  • git annotate helps identify code authors and the commit responsible for each line, which is useful for understanding code history and ownership.
  • It can be a valuable tool for tracing the origin of a specific line of code or identifying when and why a change was made.

Command: git filter-branch [options] [--] <commit-range>

Purpose: git filter-branch is used to rewrite the commit history of a Git repository by applying various filters to the commits in a specified range. It allows you to modify, delete, or split commits.

Examples:

  1. Remove a file from the entire commit history:

    git filter-branch --tree-filter 'rm -f <file>' HEAD

    This command removes <file> from every commit in the current branch's history.

  2. Change the author name and email for all commits:

    git filter-branch --env-filter 'GIT_AUTHOR_NAME="New Name"; GIT_AUTHOR_EMAIL="newemail@example.com"' HEAD

    This command updates the author name and email for all commits in the current branch's history.

  3. Delete a specific file from commit history:

    git filter-branch --tree-filter 'git rm -f <file>' HEAD

    Use this command to delete <file> from every commit in the current branch's history.

  4. Split a commit into two commits:

    git filter-branch --commit-filter 'if [ "$GIT_COMMIT" = <commit-to-split> ]; then git commit-tree -p $GIT_COMMIT <new-parent> <new-tree>; else echo "skip commit $GIT_COMMIT"; fi' HEAD

    This command splits <commit-to-split> into two commits by creating a new commit with a different parent and tree.

Common Options:

  • --tree-filter <command>: Applies a command to the working tree for each commit.
  • --env-filter <command>: Modifies commit environment variables like author and committer details.
  • --commit-filter <command>: Allows custom logic for commit creation during the filtering process.

Useful Tips:

  • Be cautious when using git filter-branch as it rewrites commit history. Make backups or clone your repository before applying complex filters.
  • After using git filter-branch, you may need to force-push the rewritten history to remote repositories.

Sure, let's provide a precise description of upstream branches and HEAD references in Git for students:

  • What They Are: Upstream branches are remote branches in a Git repository that serve as reference points for local branches. These remote branches typically exist in a central or remote repository, like on GitHub or GitLab.

  • Purpose: Upstream branches are used for tracking changes made by other team members or contributors. They help you stay up-to-date with the latest changes in a collaborative project.

  • How to Set Up: To set an upstream branch for your local branch, you can use the --set-upstream-to or -u flag with git push or git branch commands. For example, git push --set-upstream origin feature-branch sets the upstream branch for feature-branch to origin.

  • What It Is: The HEAD reference is a symbolic reference in Git that points to the current branch you have checked out. It's essentially a pointer to the tip (most recent commit) of your active branch.

  • Purpose: The HEAD reference is used to indicate which branch or commit you're currently working on. It helps Git know where to add new commits when you make changes.

  • How to Use: You don't typically manipulate the HEAD reference directly. Instead, you switch branches using git checkout or git switch, and Git automatically updates the HEAD reference to match the currently checked-out branch.

Concept: Git hooks are scripts that Git allows you to attach or run at key points in the Git workflow. They enable you to automate tasks or enforce specific behaviors before or after certain Git actions.

Examples:

  1. Pre-commit Hook:

    • Purpose: Executes before a commit is made.
    • Example: A pre-commit hook can run code formatting checks to ensure that code adheres to style guidelines before committing.
  2. Post-commit Hook:

    • Purpose: Executes after a commit is made.
    • Example: A post-commit hook can trigger notifications to team members or perform automated deployment tasks.
  3. Pre-receive Hook:

    • Purpose: Executes on the server before accepting a push.
    • Example: A pre-receive hook can enforce access control rules, deny or modify certain branches, or reject pushes based on specific criteria.
  4. Post-receive Hook:

    • Purpose: Executes on the server after a push is accepted.
    • Example: A post-receive hook can trigger continuous integration builds, update a production server, or send notifications.
  5. Prepare-commit-msg Hook:

    • Purpose: Executes before the commit message editor is shown.
    • Example: A prepare-commit-msg hook can automatically populate the commit message with issue numbers or task identifiers.
  6. Update Hook:

    • Purpose: Executes on the server after a push but before the ref is updated.
    • Example: An update hook can enforce branch naming conventions, prevent force pushes, or check for specific conditions.

Common Use Cases:

  • Enforcing Coding Standards: Use hooks to run linting or code formatting tools before allowing commits.
  • Integration with CI/CD: Trigger automated build and deployment processes after pushes are received.
  • Access Control: Implement access control and branch protection policies on the server.
  • Custom Commit Messages: Auto-generate or validate commit messages to ensure consistency.
  • Issue Tracking Integration: Automatically link commits to relevant issues or tasks.

Useful Tips:

  • Hooks are stored in the .git/hooks directory of your Git repository.
  • Git provides sample hook scripts in the .git/hooks directory that you can customize and use as a starting point.
  • Hooks are local to each repository, so they won't be automatically shared with collaborators. You can version control them to share with your team.

Git workflows define a set of conventions and practices for collaborating on and managing Git repositories. Different workflows are suited to different development scenarios and team sizes. Here are some common Git workflows and when they are most suitable:

  1. Centralized Workflow:

    • Suitable For: Small teams or solo developers.
    • Description: In this workflow, there's a single central repository. Developers clone the repository, create branches for their work, make changes, and push their branches back to the central repository. Commits to the main branch (e.g., master) represent the official project history.
    • Pros: Simple and easy to understand. Suitable for small projects with few contributors.
    • Cons: Limited collaboration features. All changes go through a single gatekeeper.
  2. Feature Branch Workflow:

    • Suitable For: Teams of all sizes.
    • Description: Each feature or bug fix is developed in a dedicated branch. Developers create feature branches off the main branch, work on their changes, and merge them back into the main branch when complete. Pull requests or merge requests are often used to facilitate code review and discussion.
    • Pros: Encourages isolated development and code review. Ideal for medium to large teams.
    • Cons: Can lead to a high number of branches, which may require careful branch management.
  3. Gitflow Workflow:

    • Suitable For: Medium to large teams, complex projects with frequent releases.
    • Description: This workflow defines a strict branching model with specific branch names like feature, release, and hotfix. Each type of development (features, releases, and fixes) occurs in its own branch. It emphasizes versioning and release management.
    • Pros: Organized and well-structured for handling multiple releases and feature developments.
    • Cons: Can be complex and may lead to branch proliferation in smaller projects.
  4. GitHub Flow:

    • Suitable For: Teams of all sizes, especially for web development and continuous deployment.
    • Description: Similar to the feature branch workflow, but with an emphasis on Continuous Integration/Continuous Deployment (CI/CD). Developers create feature branches, push changes, open pull requests, and deploy to production frequently. The main branch always reflects the production state.
    • Pros: Encourages small, frequent releases and fast feedback. Ideal for web development and DevOps-focused teams.
    • Cons: May not suit projects with infrequent releases.
  5. GitLab Flow:

    • Suitable For: Teams using GitLab for end-to-end DevOps.
    • Description: Based on feature branches, GitLab Flow integrates CI/CD pipelines, automatic deploys, and container orchestration. Developers work on feature branches, open merge requests, and rely on GitLab's built-in CI/CD to test, build, and deploy code.
    • Pros: Well-suited for teams leveraging GitLab's CI/CD capabilities. Promotes automation and quick feedback loops.
    • Cons: Tied to GitLab's ecosystem and may not be as flexible for other CI/CD platforms.
  6. Forking Workflow:

    • Suitable For: Large open-source projects with many contributors.
    • Description: Contributors fork the main repository, create feature branches in their forks, and submit pull requests to the main repository. Maintainers review and merge pull requests.
    • Pros: Provides strong isolation between contributors and project maintainers. Ideal for open-source collaboration.
    • Cons: May have a higher barrier to entry for newcomers due to the need to fork the repository.

Choosing the right Git workflow depends on factors like team size, project complexity, collaboration style, and tooling preferences. It's important to adapt or customize workflows to suit your project's specific needs, and you can also combine elements of different workflows to create a hybrid approach. Ultimately, the goal is to streamline development, maintain code quality, and support efficient collaboration.


Congratulations on completing this Git cheat sheet! You've acquired a solid foundation in Git essentials. Remember, Git offers a world of advanced options and flags. For specific cases, explore Git's official documentation and community resources. Your journey to Git mastery is just beginning—happy coding!

Found this helpful? Let's connect on LinkedIn: linkedin.com/in/a3brothers


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published