Git Commands Cheat Sheet

Introduction

Git is a powerful version control system that helps developers track changes in their codebase. In this cheat sheet, we will cover essential Git commands that you should know to get started with Git.

Basic Git Commands

  • git add: Stages changes in the working directory.
  • git commit: Commits changes in the staging area.
  • git log: Displays commit history.
  • git branch: Lists or creates new branches.
  • git checkout: Switches to a different branch or file.

Git Branching and Merging

    Step 1: Create a new branch

    Use the command git branch to create a new branch.

    bash Create a new branch
    git branch feature/new-feature

    Step 2: Switch to the new branch

    Use the command git checkout to switch to the new branch.

    bash Switch to the new branch
    git checkout feature/new-feature

    Step 3: Make changes and commit

    Make changes in the new branch and commit them using git commit.

    bash Make changes and commit
    git add .
          git commit -m "New feature implementation"

    Step 4: Merge the branch

    Use the command git merge to merge the new branch into the main branch.

    bash Merge the branch
    git checkout master
          git merge feature/new-feature

Git Remotes

Git remotes are used to connect your local repository to a remote repository on a server. You can use the command git remote to add or remove remotes.

bash Add a remote
git remote add origin https://github.com/user/repository.git

Once you have added a remote, you can use the command git push to push your changes to the remote repository.

bash Push changes to the remote repository
git push origin master

Common Mistakes

  • Forgetting to commit changes before pushing to the remote repository.
  • Not using branches to isolate changes before merging them into the main branch.
  • Not regularly cleaning up the local repository by running git gc and git prune.

Quick Start

To get started with Git, follow these steps:

  1. Install Git on your system.
  2. Initialize a new Git repository by running git add . and git commit -m "Initial commit".
  3. Use git branch to create new branches and git checkout to switch between them.
  4. Use git push to push changes to the remote repository.

FAQ

Q: What is the difference between git add and git commit?

git add stages changes in the working directory, while git commit commits changes in the staging area.

Q: How do I resolve a merge conflict?

Use the command git status to identify the conflicting files, and then use a merge tool to resolve the conflicts.

Q: How do I remove a remote repository?

Use the command git remote rm to remove a remote repository.

Conclusion

This Git commands cheat sheet covers essential commands for Git version control system. Remember to use branches to isolate changes, commit regularly, and clean up the local repository regularly.