Git is a command-line tool used for version control. It helps developers track changes in their code, collaborate with others, and manage multiple versions of a project efficiently. Git stores data as snapshots, enabling you to roll back, branch out, and merge code safely. Here’s a curated list of some essential and handy Git commands to simplify your workflow.
1. git config: A command in Git used to set configuration options, such as user name, email, default editor, and more. It allows you to customize Git’s behavior either globally (across all repositories) or locally (within a specific project).
Example:
git config --global user.name "[name]" → Set your author name.
git config --global user.email "[email address]" → Set your author email.
2. git init: A command used to create a new, empty Git repository in your project directory. It sets up all the necessary .git
files and folders so you can start tracking changes, staging files, and committing to version control. It’s the first step when starting a new Git project locally.
Example:
git init [repository name] → Start a new Git repository.
3. git clone: The git clone
command is used to create a copy of an existing Git repository from a remote server (like GitHub) to your local machine. It downloads the entire project—including all files, branches, and commit history—so you can work on it locally.
Example:
git clone [url] → Copy a remote repository to your local machine.
4. git add: The git add
is a command used in Git to stage changes in your working directory, preparing them to be included in the next commit. It tells Git which files or parts of files you want to track and commit.
Example:
git add [file] → Add a file to the staging area.
git add * → Add all files to the staging area.
5. git commit: The git commit
is a command in Git used to save changes you’ve staged to your local repository. It creates a snapshot of your project’s current state, allowing you to track progress, collaborate with others, and roll back to previous versions if needed. Each commit includes a message describing what was changed.
Example:
git commit -m "[commit message]" → Commit changes with a message.
git commit -a → Commit all tracked file changes directly.
6. git diff: The git diff
is a Git command used to show the differences between changes in your working directory, staging area, and commits. It helps you see what lines have been added, modified, or deleted before committing the changes, making it useful for reviewing code updates.
Example:
git diff → View unstaged changes.
git diff --staged → View staged vs latest commit differences.
git diff [branch1] [branch2] → Compare two branches.
7. git reset: The git reset
is a Git command used to undo changes in your repository. It can move the current HEAD
and optionally update the staging area and working directory. Depending on the option used (--soft
, --mixed
, or --hard
), it can:
--soft
: Keep changes in staging (undo commit only)--mixed
: Unstage changes, but keep them in the working directory--hard
: Discard both commits and changes completely
It’s a powerful tool, but it should be used carefully to avoid losing work.
Example:
git reset [file] → Unstage a file.
git reset [commit] → Undo commits after a certain commit.
git reset --hard [commit] → Revert to a specific commit entirely.
8. git status: The git status
is a command used in Git to display the current state of the working directory and staging area. It shows which files have been modified, added, or deleted, and which changes are staged for the next commit. It helps you keep track of your repository’s status, giving you a summary of changes since the last commit and guiding you in preparing files for commit.
Example:
git status → See files to be committed and their status.
9. git rm: The git rm
is a command used in Git to remove files from both your working directory and the staging area (index). This means the file will be deleted from your project and also marked for removal in the next commit. You can use it with options like --cached
removing the file only from the staging area, but keeping it locally. It helps maintain a clean repository by tracking file deletions effectively.
Example:
git rm [file] → Delete a file and stage the deletion.
10. git log: The git log
is a command used in Git to display the commit history of a repository. It shows a list of commits made to the repository, along with important details like the commit hash, author, date, and commit message. This helps developers review the history of changes and track the progress of the project. The output can be customized with various options to filter, format, or limit the displayed logs.
Example:
git log → Show commit history.
git log --follow [file] → Track history of a specific file, even after renames.
11. git show: The git show
is a Git command that displays detailed information about a specific commit, branch, or tag in your Git repository. It shows the commit’s metadata (such as author, date, and commit message), as well as the changes made in that commit (e.g., added, modified, or deleted files). By default, it shows the latest commit on the current branch, but you can specify a commit hash to view information about any particular commit.
Example:
git show [commit] → Display changes from a specific commit.
12. git tag: A Git tag is a reference to a specific point in Git history, typically used to mark important milestones like releases (e.g., version 1.0.0). Unlike branches, tags do not change over time. They are static and used for labeling commits, making it easier to identify and reference specific versions of a project. Tags are often used in release management and are stored locally, but can be shared with others via a remote repository.
Example:
git tag [commitID] → Create a tag for a commit.
13. git branch: The git branch
is a Git command used to create, list, rename, or delete branches in a repository. Branches allow developers to work on separate features or fixes without affecting the main codebase, enabling parallel development and safer collaboration.
Example:
git branch → List all local branches.
git branch [branch name] → Create a new branch.
git branch -d [branch name] → Delete a branch.
14. git checkout: The git checkout
command is used in Git to switch between branches or restore files in your working directory. You can use it to:
- Switch branches:
git checkout branch-name
- Restore files:
git checkout -- filename
- Create and switch to a new branch (older Git versions):
git checkout -b new-branch
Note: In newer Git versions, git switch
is recommended for switching branches.
Example:
git checkout [branch name] → Switch branches.
git checkout -b [branch name] → Create and switch to a new branch.
15. git merge: The git merge
is a command in Git used to combine changes from one branch into another. Most commonly, it merges a feature or development branch into the main branch. It takes the contents of the specified branch and integrates them with the current branch, preserving the history of both.
Example:
git merge [branch name] → Merge a branch into the current one.
16. git remote: The git remote
is a Git command used to manage connections to remote repositories. It lets you view, add, rename, or remove remote URLs that link your local repository to external sources (like GitHub or GitLab). For example, git remote add origin <url>
links your local repo to a remote one named “origin” for pushing or pulling changes.
Example:
git remote add [variable name] [remote URL] → Link local repo to remote server.
17. git push: The git push
is a command used in Git to upload your local repository changes to a remote repository, such as GitHub. It transfers committed changes from your local branch to the corresponding branch on the server, making your updates available to others.
Example:
git push [variable name] master → Push master branch commits.
git push [variable name] [branch] → Push specific branch commits.
git push --all [variable name] → Push all branches.
git push [variable name] :[branch name] → Delete a remote branch.
18. git pull: The git pull
is a Git command used to fetch and download changes from a remote repository and merge them into your local branch. It’s a combination of git fetch
(which gets the latest updates) and git merge
(which integrates those updates). This keeps your local project up to date with the remote version.
Example:
git pull [repository link] → Fetch and merge remote changes.
19. git stash: The git stash
is a Git command that temporarily saves your uncommitted changes (both staged and unstaged) without committing them, allowing you to switch branches or pull updates without losing your work. Later, you can restore those changes using git stash apply
or git stash pop
.
Example:
git stash save → Temporarily save modified files.
git stash pop → Apply the most recent stash.
git stash list → List all stashes.
git stash drop → Discard the most recent stash.
Final thoughts: With these essential Git commands at your fingertips, you’ll be ready to manage your code efficiently, collaborate better, and ace version control! Happy coding!
1 thought on “Essential git commands: Every developer should know”