Techtales.

📝The Ultimate Git Cheat Sheet🚀
TD
The Don✨Author
Oct 26, 2024
4 min read

📝The Ultimate Git Cheat Sheet🚀

12

Git is an essential tool for developers, enabling smooth version control, collaboration, and code management across projects. This cheat sheet breaks down the basics of Git, from setup to advanced commands, so you can navigate Git’s functionality with ease.

1. Setting Up Git

To get started with Git, you first need to install it and configure some basic settings.

Installation

  • On Linux: sudo apt-get install git
  • On Mac: brew install git
  • On Windows: Download from git-scm.com/downloads

Configuration

After installation, configure your identity for commits:

You can also enable helpful settings like colorized output:

2. Starting a Project with Git

Git can initialize a repository for new projects or clone an existing one.

  • Initialize a Repository:

    This command creates a new Git repository in your project folder.

  • Clone a Repository:

    Cloning downloads the entire project history from the remote repository.

3. Basic Git Commands

For day-to-day work, Git’s main commands help manage changes effectively.

  • Check Status:

    Displays the current state of your working directory.

  • Add Changes:

    Add a file to the staging area. To add all files, use git add ..

  • Commit Changes:

    Creates a new commit from the changes in the staging area.

  • View Differences:

    • To see unstaged changes:
    • For staged changes:

4. Branching and Merging

Git’s branching model makes it easy to work on separate features or fixes without affecting the main project.

  • Create a Branch:

    This command creates a new branch based on the current branch.

  • Switch Branches:

    To switch to an existing branch. Use -b to create and switch to a new branch simultaneously.

  • Merge Branches:

    Merges the specified branch into the current branch.

  • Delete a Branch:

    Deletes a branch if it has already been merged into another.

5. Working with Remote Repositories

Remote repositories allow teams to collaborate. Here’s how to manage them:

  • Setup Remote Repository
  • Fetch Changes:

    This command updates your local repository with changes from the remote, without merging them.

  • Pull Changes:

    Fetches changes and merges them into the current branch.

  • Push Changes:

    Pushes your local changes to the remote branch.

  • Setting an Upstream Branch You can link your local branch with a remote branch using the -u or --set-upstream flag. This makes future git push and git pull commands simpler by associating the local branch with a specific remote branch.

6. Git Stash

Git’s stash feature lets you temporarily store changes without committing them.

  • Stash Changes:

    Stashes current changes, so you can work on something else.

  • Apply Stashed Changes:

    Reapplies the most recent stash and removes it from the stash list.

  • Delete a Stash:

    Deletes the specified stash.

7. Inspecting Project History

Git’s logging tools allow for an in-depth look at the project history.

  • View Commit Log:

    Shows the commit history.

  • Compact View:

    Displays a compact, graphical view of commits with branch and tag references.

  • View Recent Commits:

    Limits the output to the last n commits.

8. Tagging Commits

Tags are useful for marking specific points in history, like version releases.

  • Create a Tag:

    Adds a lightweight tag to the current commit.

  • Annotated Tag:

    Annotated tags include additional metadata, like messages.

9. Undoing Changes

Git provides commands to undo mistakes, but use them with caution.

  • Unstage Changes:

    Removes the file from the staging area without changing the working directory.

  • Revert a Commit:

    Creates a new commit that undoes changes from a specific commit.

  • Discard All Local Changes:

    Resets the working directory and staging area to the last commit.

10. Ignoring Files with .gitignore

A .gitignore file specifies files and directories Git should ignore.

Add patterns to exclude specific files, like logs or temporary files, from Git tracking.

Final Tips

Mastering Git takes practice, and each project will offer new challenges to grow your Git skills. Try using this cheat sheet as a reference for a smoother, more efficient Git workflow.

For more detailed learning, consider checking the free Pro Git book or downloading this cheatsheet. Happy coding!

1
2