Git Commands Cheat Sheet
A handy cheat sheet of the most common Git commands you'll use, plus a simple, end-to-end tutorial on the typical Git workflow. This guide is perfect for beginners and anyone looking to understand version control basics, from setting up repositories to managing commits and branches. Gain the confidence to streamline your projects with Git!
Configuration Steps
Required (For Fresh Install)
First, check if you have already configured Git by running:
If the user.name and user.email configurations are empty, run the following commands to set them up:
git config --global user.email "[email protected]"
Recommended
git config --global merge.tool "vscode" // Sets VS Code as the merge tool
git config --global mergetool.vscode.cmd "code --wait $MERGED" // Defines the command to open the merge tool in VS Code
Most Common Commands
git clone [url] // Clone a repository from a URL
git status // Check the status of your repository
git add [file] // Add a file to the staging area
git commit -m "message" // Commit changes with a message
git push // Push changes to a remote repository
git pull // Pull updates from a remote repository
git branch // List branches
git checkout [branch] // Switch to a branch
git merge [branch] // Merge a branch into the current branch
Additional Commands
git reset [file] // Unstage a file
git reset --hard [commit] // Reset to a specific commit
git stash // Stash changes
git stash apply // Apply stashed changes
git remote -v // Show remote URLs
git tag [name] // Create a tag
git fetch // Fetch updates from a remote repository
git diff // Show changes between commits, branches, etc.
git rm [file] // Remove a file
git mv [old] [new] // Rename or move a file
git rebase [branch] // Reapply commits on top of another base tip
git cherry-pick [commit] // Apply the changes introduced by some existing commits
git bisect // Use binary search to find the commit that introduced a bug
Examples & Tutorial
This section provides a small tutorial demonstrating an end-to-end flow using the most common Git commands.
Step-by-Step Example
1. Initialize a new Git repository:
This command creates a new Git repository in your current directory. A Git repository is a virtual storage of your project where you can keep track of changes.
2. Clone an existing repository:
This command copies an existing repository from the URL to your local machine. "Clone" means creating a copy of the repository so you can work on it locally.
3. Check the status of your repository:
This command shows the state of your working directory and staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.
4. Add a file to the staging area:
This command adds the specified file to the staging area. The staging area is like a waiting room where your changes sit before you commit them.
5. Commit changes with a message:
This command saves your changes in the local repository. The message within quotes should describe the changes made, which helps in keeping track of your project's history.
6. Create a new branch and switch to it:
The first command creates a new branch called "new-feature." A branch is a separate line of development. The second command switches to this new branch, allowing you to work on it independently of the main branch.
7. Push the new branch to the remote repository:
This command uploads your new branch to the remote repository (e.g., GitHub). "Push" means sending your changes to the remote server so others can access them.
8. Create a Pull Request (PR) or Merge Request (MR):
Go to your remote repository (e.g., GitHub) and create a Pull Request (PR) or Merge Request (MR) from the new-feature branch to the main branch. This allows others to review your changes before merging.
9. Merge the new branch into the main branch after approval:
Switch to the main branch and pull the latest changes. This ensures your local main branch is up to date.
Learn Python & QA Automation With Self-Paced Video Courses
Python | Selenium WebDriver | API Testing | SQL | Robot Framework | BDD (Cucumber)
Stay connected with news and updates!
Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.
We hate SPAM. We will never sell your information, for any reason.