Git Cheat Sheet: Top Commands and Examples

Git Cheat Sheet: Top Commands and Examples

Git, the ubiquitous distributed version control system, is a cornerstone of modern software development. Its power and flexibility come with a certain learning curve, but mastering its core commands can significantly boost your productivity and collaboration effectiveness. This comprehensive cheat sheet provides detailed explanations, examples, and practical tips for the most frequently used Git commands, covering everything from basic repository management to advanced branching and merging strategies.

I. Getting Started: Initializing and Cloning Repositories

  • git init: This command initializes a new Git repository in the current directory. It creates a hidden .git folder which stores all the repository’s metadata, including the commit history, branches, and configuration.

    bash
    git init

    Example: Initialize a Git repository for a new project called “my-project”:

    bash
    mkdir my-project
    cd my-project
    git init

  • git clone <repository_url>: Clones an existing Git repository from a remote server to your local machine. The <repository_url> can be an HTTPS or SSH URL.

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

    Example: Clone a public repository from GitHub:

    bash
    git clone https://github.com/torvalds/linux.git

II. Staging and Committing Changes

  • git status: Displays the status of your working directory and staging area. It shows which files have been modified, added, or deleted.

    bash
    git status

  • git add <file>: Adds a file to the staging area. This prepares the file to be included in the next commit. You can also use git add . to add all modified and new files in the current directory and its subdirectories.

    bash
    git add index.html
    git add .

  • git commit -m "<commit_message>": Commits the staged changes with a descriptive message. The -m flag allows you to provide the commit message inline.

    bash
    git commit -m "Fix bug in login form"

    Best Practice: Write clear and concise commit messages that explain the purpose of the change.

  • git diff: Shows the differences between the working directory and the staging area, or between two commits.

    bash
    git diff # Shows changes between working directory and staging area
    git diff --staged # Shows changes between staging area and last commit
    git diff HEAD~1 # Shows changes between the last two commits

III. Working with Branches

  • git branch: Lists all the branches in the repository. The current branch is marked with an asterisk.

    bash
    git branch

  • git branch <branch_name>: Creates a new branch.

    bash
    git branch feature/new-feature

  • git checkout <branch_name>: Switches to a different branch.

    bash
    git checkout feature/new-feature

  • git checkout -b <branch_name>: Creates a new branch and switches to it in a single command.

    bash
    git checkout -b feature/new-feature

  • git merge <branch_name>: Merges the specified branch into the current branch.

    bash
    git checkout main
    git merge feature/new-feature

  • git branch -d <branch_name>: Deletes a branch.

    bash
    git branch -d feature/new-feature

IV. Interacting with Remote Repositories

  • git remote -v: Lists all remote repositories associated with the local repository.

    bash
    git remote -v

  • git remote add <remote_name> <repository_url>: Adds a new remote repository. Typically, the remote name is “origin”.

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

  • git push <remote_name> <branch_name>: Pushes the local branch to the remote repository.

    bash
    git push origin main

  • git pull <remote_name> <branch_name>: Fetches and merges changes from the remote repository into the local branch.

    bash
    git pull origin main

  • git fetch <remote_name>: Downloads changes from the remote repository without merging them into the local branch. This allows you to review the changes before merging.

    bash
    git fetch origin

V. Inspecting History and Reverting Changes

  • git log: Displays the commit history.

    bash
    git log
    git log --oneline # Shows a condensed version of the log
    git log -p <file> # Shows the history of changes for a specific file

  • git checkout <commit_hash>: Checks out a specific commit. This allows you to inspect the code at a particular point in time. This creates a detached HEAD state.

    bash
    git checkout a1b2c3d4

  • git revert <commit_hash>: Creates a new commit that undoes the changes introduced by a specific commit. This is a safer way to undo changes than rewriting history.

    bash
    git revert a1b2c3d4

  • git reset <commit_hash>: Moves the current branch pointer to a specific commit, effectively discarding subsequent commits. Use with caution as this can lead to data loss if not used carefully. There are three modes for git reset:

    • --soft: Keeps changes in staging area.
    • --mixed (default): Keeps changes in working directory.
    • --hard: Discards all changes.

    bash
    git reset --hard HEAD~1 # Reverts to the previous commit and discards changes

VI. Stashing Changes

  • git stash: Temporarily stores changes that are not ready to be committed. This allows you to switch branches or perform other operations without committing incomplete work.

    bash
    git stash

  • git stash pop: Applies the latest stashed changes and removes them from the stash.

  • git stash list: Lists all stashed changes.

  • git stash drop: Discards the latest stashed changes.

VII. Ignoring Files

  • .gitignore: A file used to specify files and directories that should be ignored by Git. This is useful for excluding build artifacts, temporary files, and other files that should not be tracked in the repository. Create a .gitignore file in the root of your repository and add patterns for the files you want to ignore.

    Example .gitignore file:

    *.o
    *.class
    /build/
    /tmp/

VIII. Tagging Releases

  • git tag <tag_name>: Creates a tag for a specific commit. Tags are used to mark important points in the repository’s history, such as releases.

    bash
    git tag v1.0.0

  • git tag -a <tag_name> -m "<tag_message>": Creates an annotated tag with a message.

    bash
    git tag -a v1.0.0 -m "Release version 1.0.0"

  • git push --tags: Pushes tags to the remote repository.

IX. Advanced Git Commands

  • git cherry-pick <commit_hash>: Applies a specific commit from one branch to another.

  • git rebase <branch_name>: Rewrites the commit history by applying commits from one branch onto another. This can create a cleaner and more linear project history.

  • git bisect: Helps to identify the commit that introduced a bug by using a binary search algorithm.

This detailed cheat sheet provides a solid foundation for using Git effectively. Remember to consult the official Git documentation for more in-depth information and explore further commands and options as you become more comfortable with the system. Continuous practice and experimentation are the keys to mastering Git and leveraging its full potential for your development workflow.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top