DEV Community

Tejas
Tejas

Posted on • Updated on

Git cheatsheet

Git Cheatsheets and Tricks

Git is a powerful and versatile version control system that has become an essential tool for software development teams and individual developers alike. In this article, we'll cover some Git cheatsheets and tricks that will help you improve your Git skills.

Git Cheatsheets

A Git cheatsheet is a quick reference guide that summarizes the most commonly used Git commands and their usage. Here are some common Git commands that you might find on a Git cheatsheet:

Initialize a repository

git init # Initialize a new repository

Clone a repository

git clone <repository_url> # Clone an existing repository

Working with branches

git branch                  # List all branches
git branch <branch_name>    # Create a new branch
git checkout <branch_name>  # Switch to a branch
git merge <branch_name>     # Merge a branch into the current branch
git branch -d <branch_name> # Delete a branch

Enter fullscreen mode Exit fullscreen mode

Commiting changes

git status               # Show the status of your changes
git add <file_name>      # Stage changes for commit
git add .                # Stage all changes for commit
git commit -m "message"  # Commit staged changes with a message

Enter fullscreen mode Exit fullscreen mode

pulling and pushing changes

git remote add origin <repository_url>  # Add a remote repository
git push -u origin <branch_name>        # Push changes to a remote repository
git pull origin <branch_name>           # Pull changes from a remote repository

Enter fullscreen mode Exit fullscreen mode

View commit history and differences

git log             # Show commit history
git diff            # Show differences between current changes and previous commit
git diff <commit>   # Show differences between current changes and a specific commit

Enter fullscreen mode Exit fullscreen mode

Undoing changes

git reset <file_name>    # Unstage changes for a specific file
git reset                # Unstage all changes
git checkout <file_name> # Discard changes for a specific file
git checkout .           # Discard all changes
git revert <commit>      # Undo a commit by creating a new commit with the opposite changes

Enter fullscreen mode Exit fullscreen mode

tags

git reset <file_name>    # Unstage changes for a specific file
git reset                # Unstage all changes
git checkout <file_name> # Discard changes for a specific file
git checkout .           # Discard all changes
git revert <commit>      # Undo a commit by creating a new commit with the opposite changes

Enter fullscreen mode Exit fullscreen mode

stash

git stash        # Stash changes
git stash list   # List all stashes
git stash apply  # Apply the most recent stash
git stash drop   # Drop the most recent stash

Enter fullscreen mode Exit fullscreen mode

Top comments (0)