Git Cheat Sheet

Git is a popular version control system that allows developers to keep track of changes made to their codebase, collaborate with others, and manage multiple versions of their code. It can be a powerful tool, but it can also have a steep learning curve for those new to it. That's where a Git cheat sheet can come in handy.

A Git cheat sheet is a quick reference guide that summarizes some of the most commonly used Git commands and their syntax. Here are some of the basic Git commands that you might find on a cheat sheet:

Git Basic commands:

  1. Git init: Creates / Initializes a new Git repository within the current directory to start tracking changes.
    • git init

       

  2. Git clone: Clone an existing Git repository (on Github, Bitbucket, etc) to a new folder/directory
    • git clone <repository>

       

  3. Git add: Adds a file to the staging area (index) for the next commit
    • git add .
      git add <filename>

       

  4. Git add -p: Interactively adds changes to the staging area (useful for selectively staging changes within a file)
    • git add -p

       

  5. Git commit: To commit changes that have been staged
    • git commit -m "commit message"

       

  6. Git status: Displays the current state of both the working directory and the staging area
    • git status

       

  7. Git log: Displays the commit history
    • git log

       

  8. Git branch: Lists all local branches in the repository
    • git branch

       

  9. Git checkout <branch>: You can switch to a designated branch within the repository
    • git checkout <branch>

       

  10. Git checkout -b <new-branch>: Creates a new branch and switches to the created branch
    • git checkout -b <new-branch>

       

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

       

  12. Git pull: Fetch and merge changes from the remote repository to the current branch
    • git pull

       

  13. Git push: Used to send modifications made in the current branch to a remote repository
    • git push

       

  14. Git stash: Allows users to temporarily store changes that are not yet prepared for commit.
    • git stash

       

Git Advanced commands:

  1. Git rebase: Reapplies commits from the current branch on top of the specified branch (useful for integrating changes from another branch into your own)
    • git rebase <branch>

       

  2. Git reset <commit>: Resets the repository to a previous commit (use with caution as it can permanently discard commits)
    • git reset <commit>

       

  3. Git reset --hard: Resets the repository to the last commit and discards all changes in the working directory
    • git reset --hard

       

  4. Git cherry-pick: Applies a single commit from one branch to another
    • git cherry-pick <commit>

       

  5. Git tag: Tags a specific commit with a meaningful name
    • git tag <tag-name> <commit>

       

  6. Git remote add <name> <url>: Adds a new remote repository with a given name and URL
    • git remote add <name> <url>

       

  7. Git remote set-url: The commend change the URL of remote repository associated with 'origin'.
    • git remote set-url origin <NEW_GIT_URL_HERE>

       

  8. Git remote -v: Using the command displays a list of all remote repositories along with their corresponding URLs.
    • git remote -v

       

  9. Git push --tags: Pushes all tags to a remote repository
    • git push --tags

       

  10. Git submodule add: Adds a Git submodule to the repository at the specified path
    • git submodule add <repository> <path>

       

  11. Git submodule update: Initializes and updates Git submodules in the repository
    • git submodule update

       

  12. Git bisect: Binary search for a specific commit that introduced a bug (useful for debugging)
    • git bisect

       


Git Tips:
Always start by checking the status of your repository before making any changes using git status
It's advisable to make frequent commits with clear descriptions by using the command 'git commit -m "commit message"
Use branches to keep your work organized and to collaborate with others
Always pull before you push to avoid conflicts using git pull and git push
Use .gitignore to ignore files and directories that don't belong in the repository
When in doubt, consult the Git documentation or seek help from experienced Git users.