Git is easier to use when the working tree, staging area and committed history are understood as separate states. Most commands change only one or two of those states; failures occur when a developer assumes they have changed all three. The twenty commands below support daily work from initializing a repository through staging, reviewing differences, collaborating across branches and backing out mistakes. Safe recovery matters as much as speed: git revert records an inverse commit, while git reset may move references and discard local work depending on its mode. Treat shared history as an explicit team agreement.
Create, stage and inspect local work
The git init command creates a new Git repository in the current directory by generating a hidden .git folder that contains all the metadata, object storage, and configuration files Git needs to track changes in the project. Running git init transforms any ordinary directory into a version-controlled workspace where every subsequent file change can be staged, committed, and reviewed. This command is typically the first step when starting a new project from scratch locally before connecting it to a remote hosting platform like GitHub, GitLab, or Bitbucket for team collaboration and backup purposes.
The git add command moves changes from the working directory into the staging area, also called the index, where they wait to be included in the next commit. This two-step process of staging and then committing gives developers precise control over what goes into each commit, allowing unrelated changes made during the same working session to be grouped into separate, logically cohesive commits that tell a clear story about what changed and why. Running git add followed by a specific filename stages only that file, while git add followed by a period stages all modified and new files in the current directory recursively.
The git commit command saves staged changes permanently into the local repository history as a new commit object containing the snapshot of staged files, the author identity, a timestamp, and the commit message that describes what changed. Every commit in Git is immutable and identified by a unique SHA-1 hash, creating a permanent and tamper-evident record of every change made to the project over time. Writing meaningful commit messages is one of the most important habits a developer can cultivate because commit history serves as documentation that teammates and future contributors rely on to understand why specific decisions were made.
The git status command displays the current state of the working directory and staging area, showing which files have been modified, which are staged for the next commit, which are untracked by Git, and which have conflicts that need resolution after a merge operation. Running git status is one of the most frequently used Git commands in any developer’s daily workflow because it provides an immediate, clear picture of where things stand before deciding what action to take next.
Read history and manage branches
The git log command displays the commit history of the current branch in reverse chronological order, showing each commit’s hash, author, date, and message in a format that allows developers to trace the evolution of the project over time. In its default form, git log produces a detailed multi-line output for each commit, which can become difficult to scan in projects with long histories. Using git log with the one-line flag compresses each commit to a single line showing only the abbreviated hash and subject, producing a much more navigable view of recent project activity.
The git branch command creates, lists, renames, and deletes branches within a repository, making it the primary tool for managing the parallel lines of development that define modern Git workflows. Running git branch with no arguments lists all local branches and highlights the currently active one with an asterisk, while adding the all flag includes remote tracking branches in the output. Creating a new branch with git branch followed by a name establishes a new pointer to the current commit without switching to it, which is useful when preparing a branch that will be checked out immediately after creation using a separate command.
The git checkout command has historically served as the primary way to switch between branches in a Git repository, moving the HEAD pointer to the specified branch and updating the working directory to reflect the files at the tip of that branch. When switching branches, Git applies any differences between the current and target branch to the working directory, which can produce conflicts if uncommitted changes in the working directory overlap with changes on the target branch. Developers should always commit or stash work in progress before switching branches to avoid these conflicts and the confusion they create.
Switch, integrate and temporarily set aside work
The git merge command integrates changes from one branch into another by combining their histories and creating a merge commit that records the convergence of the two development lines. When the target branch has not diverged from the source branch since the feature branch was created, Git performs a fast-forward merge that simply moves the target branch pointer forward to the tip of the source branch without creating an additional merge commit. When the branches have diverged, Git creates a three-way merge commit that records both lines of development and their combined state at the point of integration.
The git rebase command moves or replays commits from one branch onto the tip of another, producing a linear project history without the merge commits that accumulate when using git merge for every integration. When a developer rebases a feature branch onto the main branch, Git temporarily removes the feature branch commits, advances the feature branch to the current tip of main, and then reapplies the removed commits one by one on top of the updated base. The result is a history that reads as if the feature work was done sequentially after the latest main branch changes rather than in parallel with them.
The git stash command saves uncommitted changes in the working directory and staging area to a temporary stack-based storage area, restoring the working directory to a clean state that matches the last commit. This is extremely useful when a developer needs to switch branches urgently to fix a bug or review a colleague’s code without committing incomplete work that is not yet ready to be saved to the project history. Running git stash push with a message provides a descriptive label for the stashed state, making it easier to identify the correct stash to restore when multiple stashes have accumulated.
The git fetch command downloads commits, branches, and tags from a remote repository into the local repository without modifying any local branches or the working directory. This makes fetch a safe operation that updates knowledge of the remote state without disrupting ongoing local work, unlike git pull which combines fetching with merging and can introduce conflicts into the working directory immediately. Running git fetch followed by the remote name downloads all new information from that remote, while specifying a branch name limits the download to updates for that specific branch only.
Synchronize safely with remotes
The git pull command fetches updates from a remote repository and immediately merges them into the current local branch, combining two operations into one convenient step that keeps local branches synchronized with their remote counterparts. By default, git pull performs a merge after fetching, which creates a merge commit when the local and remote branches have diverged. Configuring git pull to rebase instead of merge through the rebase flag or a global configuration setting produces a cleaner linear history by replaying local commits on top of the fetched remote commits rather than creating an explicit merge commit.
The git push command uploads commits from a local branch to a remote repository, making them available to other team members who share access to that remote. Running git push followed by the remote name and branch name sends local commits to the specified remote branch, creating it if it does not already exist. The upstream tracking flag available during the first push of a new branch establishes a persistent link between the local branch and its remote counterpart, so that subsequent push and pull operations on that branch require only the git push command without additional arguments.
The git reset command moves the current branch pointer to a specified earlier commit and optionally modifies the staging area and working directory to match, making it a versatile but potentially destructive tool for undoing changes in a local repository. The soft reset mode moves the branch pointer without modifying the staging area or working directory, leaving all changes from the reset commits staged and ready to be recommitted in a different form. The mixed mode, which is the default, moves the pointer and clears the staging area but leaves the working directory changes intact as unstaged modifications.
The git revert command creates a new commit that undoes the changes introduced by a specified earlier commit, providing a safe way to reverse changes in a repository’s history without rewriting it. Unlike git reset, which moves the branch pointer backward and can discard commits entirely, git revert adds a new commit that explicitly records the act of reversal, preserving the complete history of both the original change and its correction. This makes revert the appropriate tool for undoing changes that have already been pushed to a shared remote branch where rewriting history would cause problems for other contributors.
Inspect, restore, tag and select commits
The git diff command shows the line-by-line differences between two states in a Git repository, whether between the working directory and the staging area, between staged changes and the last commit, or between any two commits, branches, or tags. Running git diff with no arguments shows unstaged changes in the working directory compared to the staging area, while the staged flag shows what has been staged compared to the last commit. These two variations together give developers a complete picture of all pending changes before they decide what to commit and what to leave out of the next commit.
The git tag command creates named references to specific commits in the repository history, most commonly used to mark release points such as version numbers that correspond to software releases shipped to users or deployed to production environments. Lightweight tags are simple pointers to commits with no additional metadata, while annotated tags are full objects stored in the Git database with a tag message, tagger identity, and timestamp that make them more suitable for marking significant project milestones that benefit from additional context and documentation.
The git cherry-pick command applies the changes introduced by one or more specific commits from any branch onto the current branch, creating new commits with the same changes but different hashes. This is useful when a bug fix committed on a feature branch needs to be applied to the main or release branch immediately without merging the entire feature branch, which may contain incomplete or untested work that is not yet ready for wider distribution. Cherry-picking allows surgical extraction of individual changes from one line of development and their precise application to another.
The git config command sets configuration values that control how Git behaves at the local repository level, at the user level through a global configuration file, or at the system level affecting all users on a machine. The most essential global configurations are the user name and email address that Git attaches to every commit as author metadata, which should be set correctly before making any commits to ensure that contributions are attributed accurately in project history and on remote hosting platforms like GitHub where author information is displayed alongside commits.
The twenty essential Git commands covered throughout this article represent the foundation of effective version control practice for individual developers and collaborative teams working on projects of any size or complexity. From the initial repository setup with git init and git clone through the daily workflow commands of add, commit, status, and log, and into the more advanced territory of rebasing, cherry-picking, and interactive history editing, each command serves a specific purpose in the broader discipline of managing code changes with precision and intentionality.