Git is a distributed version control system that helps developers track code changes, manage project versions, and collaborate with teams. Learning essential Git commands makes it easier to manage repositories, work with branches, and maintain a clean development workflow.
What are Git Commands?
Git commands are instructions used to perform version control operations on a local or remote repository. They help developers manage files, record changes, and collaborate on shared projects.
- Create and manage Git repositories.
- Track changes and maintain commit history.
- Create branches and merge code changes.
- Synchronize local repositories with remote repositories.
1. git config
The git config command manages Git settings, such as the username and email associated with commits. Configuring these details helps identify who made each commit.
Syntax:
git config --global user.name "Harsh"git config --global user.email "harsh@example.com"
- The
--globaloption applies the settings to all repositories for the current user. - Use
git config --localto configure settings for a specific repository. - These settings are recommended for commit attribution but are not required before every Git operation.
Example:

2. git init
The git init command initializes a new Git repository in the current directory. It creates the repository metadata needed to track project changes.
Syntax:
git init- Run the command inside the project directory.
- Use it when starting version control for a local project.
- Existing files remain in place and can be added to Git afterward.
Example:

3. git clone
The git clone command creates a local copy of an existing repository. It downloads the repository's data and sets up a remote, usually named origin.
Syntax:
git clone <repository-url>Example:
git clone https://github.com/user/project.git- Downloads the repository and its available history.
- Creates a local working directory by default.
- Allows developers to work on an existing project.
Example:

4. git add
The git add command stages file changes for the next commit. Staging lets developers choose which modifications should be included in a commit.
Syntax:
git add <file-name>To stage multiple changes under the current directory:
git add .- Stages new files and modifications.
- Allows specific files to be selected instead of staging everything.
- Does not create a commit; use
git commitafterward.
Example:

5. git commit
The git commit command records the staged changes as a new commit in the local repository. Each commit includes a message describing the changes.
Syntax:
git commit -m "Add user authentication"- Records the currently staged changes.
- Uses a message to explain the purpose of the commit.
- Creates a checkpoint that can be reviewed through Git history.
Example:

6. git push
The git push command uploads local commits to a remote repository. It helps team members share their changes with others.
Syntax:
git push <remote> <branch>Example:
git push -u origin main- Sends local commits to the specified remote branch.
- The
-uoption sets the upstream branch for easier future pushes. - Requires suitable permissions and a compatible remote branch state.
Example:

7. git tag
The git tag command marks a specific commit, often to identify a release or milestone. Tags help developers locate important versions of a project.
Syntax:
git tag <tag-name>To create an annotated release tag:
git tag -a v1.0.0 -m "Release v1.0.0"- Lightweight tags point directly to a commit.
- Annotated tags store additional metadata, such as the tagger and message.
- Push tags to a remote using
git push origin <tag-name>.
Example:

8. git merge
The git merge command combines changes from one branch into the current branch. It is commonly used to integrate completed features into a shared development branch.
Syntax:
git switch maingit merge <branch-name>
- Integrates changes from the specified branch.
- May create a merge commit, depending on the branch history.
- Can require conflict resolution when changes overlap.
Example:

9. git pull
The git pull command fetches changes from a remote repository and integrates them into the current branch. It helps developers synchronize their local work with shared code.
Syntax:
git pull origin main- Downloads updates from the specified remote branch.
- Integrates the fetched changes through merging or, when configured, rebasing.
- May report conflicts that need to be resolved before continuing.
Example:

10. git checkout
The git checkout command can switch branches or restore files, depending on how it is used. For switching branches, git switch is generally the clearer modern alternative.
Syntax:
git checkout <branch-name>To create and switch to a new branch:
git checkout -b feature/login- Allows developers to move between existing branches.
- Can create a new branch with the
-boption. - Check for uncommitted changes before switching branches.
Example:

11. git commit --amend
The git commit --amend command modifies the most recent commit. It can update the commit message or include additional staged changes.
Syntax:
git commit --amend -m "Update configuration"To include staged changes without changing the existing message:
git add <file-name>git commit --amend --no-edit
- Helps correct a recent commit before sharing it.
- Replaces the previous commit with a new commit.
- Avoid amending commits that teammates already rely on unless the team coordinates the history change.
Example:

12. git stash
The git stash command temporarily saves changes from the working directory and staging area so developers can switch tasks without creating a commit.
Syntax:
git stashTo restore the latest stash and remove it from the stash list:
git stash pop- Saves unfinished tracked-file changes for later use.
- Use
git stash -uto include untracked files. - Use
git stash applyto restore changes without removing the stash entry.
Example:


There are multiple functionalities of stash which you can refer to here - Git Stash
13. git log
The git log command displays the commit history of a repository. It helps developers review previous changes and identify commits related to a feature or bug.
Syntax:
git logFor a compact history with a graph:
git log --oneline --graph --decorate- Shows commit messages, authors, dates, and commit identifiers.
- Supports filters for reviewing particular commits or authors.
- Helps investigate when changes were introduced.
Example:

14. git status
The git status command displays the state of the working directory and staging area. It helps developers identify changes that are staged, unstaged, or untracked.
Syntax:
git status- Shows files modified since the last commit.
- Identifies files ready to be committed.
- Helps check the repository state before committing or switching branches.
Example:

15. git blame
The git blame command shows which commit last modified each line of a file. It helps developers understand the history of specific sections of code.
Syntax:
git blame <file-name>Example:
git blame app.py- Displays the associated commit and author information for each line.
- Helps locate the change that introduced a particular line.
- Provides historical context but does not necessarily identify who originally designed the code.
Example:

16. git branch
The git branch command lists, creates, or deletes local branches. Branches allow developers to work on features and fixes independently.
Syntax:
git branchTo create a branch:
git branch feature/payment- Lists local branches when used without arguments.
- Creates a branch without automatically switching to it.
- Helps organize parallel development work.
17. git switch
The git switch command is designed specifically for switching branches. It provides a clearer alternative to using git checkout for branch operations.
Syntax:
git switch mainTo create and switch to a new branch:
git switch -c feature/payment- Switches to an existing branch.
- Creates a new branch with the
-coption. - Makes branch operations easier to distinguish from file restoration.
18. git fetch
The git fetch command downloads updates from a remote repository without integrating them into the current branch. It lets developers inspect incoming changes before merging or rebasing.
Syntax:
git fetch origin- Updates remote-tracking references.
- Does not automatically modify the current branch's files.
- Helps review remote changes before integrating them.
19. git diff
The git diff command displays differences between file versions or repository states. It helps developers review changes before staging or committing them.
Syntax:
git diffTo view staged changes:
git diff --staged- Shows unstaged changes by default.
- Displays staged changes with
--staged. - Helps catch unintended modifications before committing.
20. git reset
The git reset command moves the current branch reference or changes the staging area, depending on the option used. It is useful for correcting recent local Git operations.
Syntax:
git reset <commit>To unstage a file while keeping its changes:
git reset HEAD <file-name>--softmoves the branch reference while keeping changes staged.--mixedis the default and keeps changes in the working directory but unstaged.--harddiscards tracked working-directory changes and should be used with extreme care.
21. git revert
The git revert command creates a new commit that reverses the changes introduced by an earlier commit. It is often safer than rewriting history on shared branches.
Syntax:
git revert <commit-id>- Preserves the existing commit history.
- Records the reversal as a new commit.
- May require conflict resolution if later changes overlap with the reverted changes.
22. git rm
The git rm command removes files from Git tracking and normally removes them from the working directory as well.
Syntax:
git rm <file-name>To stop tracking a file while keeping its local copy:
git rm --cached <file-name>- Stages the removal for the next commit.
- Supports removing files that should no longer be part of the repository.
- Use
.gitignoreas well when the file should not be added again.
23. git remote
The git remote command manages references to remote repositories. It helps developers inspect or configure the locations used for fetching and pushing changes.
Syntax:
git remote -vTo add a remote:
git remote add origin <repository-url>- Lists configured remote names and URLs.
- Allows a remote repository to be added or updated.
- Commonly uses
originas the default remote name.
24. git cherry-pick
The git cherry-pick command applies the changes from a specific commit to the current branch. It is useful when a particular fix is needed without merging an entire branch.
Syntax:
git cherry-pick <commit-id>- Applies the selected commit's changes to the current branch.
- Creates a new commit with those changes.
- Can result in conflicts that must be resolved before continuing.
25. git reflog
The git reflog command displays a local history of updates to references such as HEAD. It can help locate commits that are no longer visible through the usual branch history.
Syntax:
git reflog- Records recent movements of branch references and
HEAD. - Helps recover from certain accidental resets or branch changes.
- Is local to the repository and entries are not permanent backups.