Common Git Commands
This document provides explanations for some common Git commands.
git push origin main
What it does: This command sends your local commits from the main
branch to the remote repository named origin
.
When to use it: Use this command when you have made changes and committed them locally, and you want to share those changes with others by uploading them to the remote repository.
git pull origin main
What it does: This command fetches changes from the main
branch of the remote repository named origin
and integrates them into your current local branch (usually main
). It’s a combination of git fetch
and git merge
.
When to use it: Use this command when you want to get the latest changes from the remote repository to update your local copy. This is important to stay up-to-date with collaborators’ work.
git checkout
What it does: This command is used to switch between branches or restore working tree files.
When to use it: * To switch to an existing branch: git checkout <branch-name>
* To create and switch to a new branch: git checkout -b <new-branch-name>
* To discard changes in a specific file: git checkout -- <file-name>
git reset --hard
What it does: This command resets the current branch to a specified commit, discarding all changes in the working directory and staging area that were made after that commit. It’s a powerful command that permanently removes uncommitted changes.
When to use it: Use this command with caution! It is typically used when you want to completely discard all uncommitted changes and revert your repository to a previous state. For example: * To discard all local uncommitted changes: git reset --hard HEAD
* To reset to a specific commit (using the commit hash): git reset --hard <commit-hash>
git add [filename]
This command stages a specific file for the next commit. You use this when you want to include changes from only that particular file in your upcoming commit.
git add .
This command stages all changes in the current directory and its subdirectories for the next commit. This is a convenient way to stage multiple new or modified files at once. After using git add (either form), the changes are moved to the staging area, ready to be included in your next commit.