# Git Commands: A Revision Guide
This document outlines commonly used and less frequently used Git commands,
providing a brief explanation for each of the most essential ones.
## Mostly Used Git Commands
These commands form the backbone of daily Git workflows and are essential for
effective version control.
### `git init`
Initializes a new Git repository in the current directory. This command creates a
hidden `.git` subdirectory, which contains all the necessary files for Git to track
changes, including the object database, references, and configuration. It's the
foundational step to begin version controlling a new project from scratch. After
execution, your directory becomes a Git-managed workspace, ready for you to stage
and commit file modifications, thereby building a comprehensive history of your
project's evolution. It essentially transforms a regular folder into a powerful
version-controlled environment.
### `git clone <repository_url>`
Creates a local copy of an existing remote Git repository. This command downloads
the entire project history, including all branches, commits, and tags, from the
specified URL to your local machine. It automatically sets up a remote tracking
branch, typically named `origin`, which links your local repository to the original
remote. This allows for easy synchronization, enabling you to pull updates from the
remote and push your local changes back. It's the standard method for joining an
existing project or obtaining a working copy.
### `git add <file(s)>`
Stages changes from your working directory for the next commit. This command moves
modifications (new files, changes to existing files, deletions) into the staging
area, also known as the index. Only changes explicitly added to the staging area
will be included in the subsequent `git commit`. You can specify individual files,
directories, or use `.` to stage all current modifications. This granular control
allows developers to craft logical, atomic commits, ensuring that each commit
represents a coherent set of changes.
### `git commit -m "message"`
Records the staged changes as a new snapshot in the repository's history. Each
commit represents a specific state of your project at a given time, identified by a
unique SHA-1 hash. The `-m` flag is used to provide a concise, mandatory message
summarizing the changes introduced in that commit. Committing frequently with
clear, descriptive messages is a fundamental practice for maintaining a readable
and navigable project history, facilitating collaboration and debugging.
### `git status`
Displays the current state of the working directory and the staging area. This
command provides a comprehensive overview of what Git sees as changes. It lists
files that are untracked (new and not yet added), modified (changed but not
staged), and staged (ready for the next commit). `git status` is an indispensable
tool for understanding your current progress, verifying what will be included in
your next commit, and guiding your next Git actions like `git add` or `git commit`.
### `git log`
Shows the commit history of the current branch. This command presents a
chronological list of commits, detailing each commit's unique ID (hash), author,
date, and the associated commit message. It's an invaluable tool for reviewing past
changes, understanding the project's evolution, and pinpointing specific
modifications. `git log` supports various options for filtering (e.g., by author,
date) and formatting (e.g., `--oneline` for brevity, `--graph` for visualizing
branch merges), making it highly versatile for historical analysis.
### `git branch`
Manages branches within your repository. When executed without arguments, it lists
all local branches and indicates the currently active one. You can create a new
branch with `git branch <new_branch_name>`, which essentially creates a new pointer
to the current commit. Branches enable developers to work on new features or bug
fixes in isolation, preventing interference with the main development line until
their work is complete and ready for integration.
### `git checkout <branch_name_or_commit_hash>`
Switches between branches or restores files. This command moves your `HEAD` pointer
to a different branch, updating your working directory to match the state of that
branch. It can also be used to switch to a specific commit, allowing you to inspect
past states of the project. Additionally, `git checkout -- <file>` discards local
changes in a specific file. For branch switching, `git switch` is the modern, safer
alternative.
### `git merge <branch_name>`
Integrates changes from one branch into the current active branch. This command
combines the commit history of the specified branch into your current branch,
creating a new merge commit if necessary. Git attempts to automatically reconcile
changes; however, if conflicting modifications occur on the same lines, you must
manually resolve these "merge conflicts" before the merge can be completed. Merging
is a core operation for incorporating completed features or fixes back into the
main development line.
### `git pull`
Fetches changes from a remote repository and integrates them into the current local
branch. This command is a convenient shorthand for executing `git fetch` (which
downloads changes from the remote but doesn't apply them) followed by `git merge`
(which integrates the fetched changes into your current branch). It's used to
synchronize your local repository with the remote, ensuring you have the latest
updates from collaborators and preventing potential conflicts when you eventually
push your own work.
### `git push`
Uploads local branch commits to a remote repository. This command sends your
committed changes from your local branch to its corresponding remote tracking
branch, making your work available to others. You typically need write access to
the remote repository. If others have pushed changes to the same branch since your
last `git pull`, Git will usually reject your push, requiring you to `pull` and
resolve any conflicts locally before attempting to `push` again.
### `git remote`
Manages the set of remote repositories associated with your local repository. When
used without arguments, it lists the shortnames of your configured remotes (e.g.,
`origin`). You can add a new remote with `git remote add <name> <url>` to link to
another repository, or remove one with `git remote remove <name>`. This command is
essential for interacting with different versions of your project hosted elsewhere,
facilitating collaboration and providing backup locations for your codebase.
## Rarely Used Git Commands
These commands are used less frequently in day-to-day workflows but are powerful
for specific, often more advanced, scenarios.
* `git bisect`
* `git rebase`
* `git reflog`
* `git cherry-pick`
* `git revert`
* `git reset`
* `git stash`
* `git submodule`
* `git filter-branch`
* `git gc`
* `git fsck`
* `git worktree`
* `git blame`
* `git shortlog`
* `git tag`