Introduction
Git is an absolutely indispensable tool for software engineers. It enables easy version control, collaboration, and project management by allowing you to work on your projects individually or in a large team. Indeed, one needs to master Git commands in order to be productive working solo on one’s projects or within a large team. Here are the most common Git commands you’ll need to get a solid start managing your codebase, tracking changes, and coordinating with other developers. Getting accustomed to these commands will make you more productive and make project works smooth. Let’s get deeper into the Git commands that a good software engineer should know.
1. Getting Started with Git
What is Git? Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows developers to keep track of changes, revert to previous states, and collaborate with others seamlessly.
Setting Up Git
- Configuring Git User Name and Email (
git config
) - Purpose: The
git config
command is used to set configuration options for Git, such as your user name and email address, which are used in commit messages. Example:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
This sets your name and email for all repositories on your system.
- Initializing a New Repository (
git init
)
Purpose: The git init
command creates a new Git repository. It initializes a .git directory in the root of your project, making it ready for version control.
Example:
git init
After running this command, you can start tracking changes in your project.
- Cloning an Existing Repository (
git clone
)
Purpose: The git clone
command copies an existing Git repository from a remote server to your local machine. It is useful for obtaining a working copy of a project.
Example:
git clone https://github.com/user/repo.git
This command downloads the repository located at the specified URL to your current directory.
2. Basic Git Commands
Checking Repository Status
- Viewing the Status of the Working Directory and Staging Area (
git status
) Purpose: Thegit status
command shows the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.Example:
git status
This output will list changes, both staged and unstaged, as well as untracked files.
Tracking and Committing Changes
- Adding Changes to the Staging Area (
git add
)Purpose: Thegit add
command adds changes in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. Example:
git add .
This command stages all changes in the current directory, preparing them for commit.
- Committing Changes with a Message (
git commit
)
Purpose: The git commit
command saves the staged changes to the repository. It’s like taking a snapshot of the project at a specific point in time, including a message that describes the changes.
Example:
git commit -m "Added new feature X"
This command commits the staged changes with the message “Added new feature X”.
Synchronizing with Remote Repositories
- Pushing Changes to a Remote Repository (
git push
) Purpose: Thegit push
command updates the remote repository with the changes made locally. It’s used to upload your local commits to the remote repository. Example:
git push origin main
This command pushes your changes to the ‘main’ branch of the remote repository named ‘origin’.
- Pulling Changes from a Remote Repository (
git pull
)
Purpose: The git pull
command fetches updates from the remote repository and merges them into your current branch. It’s a combination of git fetch
and git merge
.
Example:
git pull origin main
This command pulls the latest changes from the ‘main’ branch of the remote repository named ‘origin’ and merges them into your current branch.
3. Branching and Merging
Branch Management
- Creating and Listing Branches (
git branch
) Purpose: Thegit branch
command is used to create, list, rename, and delete branches. Branches are used to develop features, fix bugs, or experiment without affecting the main project. Example:
git branch new-branch
This creates a new branch named “new-branch”. To list all branches, simply use:
git branch
- Switching Branches (
git checkout
andgit switch
)
Purpose: These commands are used to switch between branches. git checkout
can also be used to restore working tree files.
Example:
git checkout new-branch
This switches to the “new-branch”. Alternatively, you can use:
git switch new-branch
git switch
is a simpler and more intuitive command introduced in newer versions of Git.
Merging Branches
- Merging Changes from One Branch to Another (
git merge
) Purpose: Thegit merge
command combines the changes from one branch into another. It’s used when you’re ready to integrate changes from a feature branch into the main branch or another branch. Example:
git merge new-branch
This merges the changes from “new-branch” into the current branch.
- Rebasing Branches (
git rebase
)
Purpose: The git rebase
command is an alternative to merging. It re-applies commits on top of another base tip, providing a cleaner project history.
Example:
git rebase main
This command re-applies the commits from the current branch on top of the “main” branch.
4. Inspecting History and Changes
Viewing Commit History
- Displaying the Commit Log (
git log
) Purpose: Thegit log
command shows a detailed history of commits, including commit hashes, author information, dates, and commit messages. Example:
git log
This displays the commit history for the current branch.
- Viewing a Simplified Commit History (
git log --oneline
)
Purpose: This variant of git log
provides a condensed view of the commit history, showing each commit on a single line.
Example:
git log --oneline
This gives a quick overview of the commits.
Comparing Changes
- Showing Differences Between Commits and the Working Directory (
git diff
) Purpose: Thegit diff
command is used to view changes between various states of the repository, such as between commits, branches, or the working directory. Example:
git diff
This shows unstaged changes in the working directory.
- Viewing Specific Changes in a Commit (
git show
)
Purpose: The git show
command displays detailed information about a particular commit, including the changes made.
Example:
git show HEAD
This shows the details of the most recent commit (HEAD).
5. Working with Stashes
Stashing Changes
- Saving Changes for Later (
git stash
) Purpose: Thegit stash
command temporarily shelves (or stashes) changes you’ve made to your working directory, so you can work on something else without losing your current progress. Example:
git stash
This command stashes the changes, saving them for later use.
- Applying Stashed Changes (
git stash pop
andgit stash apply
)
Purpose: These commands apply the stashed changes back to the working directory. git stash pop
removes the changes from the stash after applying them, whereas git stash apply
keeps them in the stash.
Example:
git stash pop
This applies the most recent stash and removes it from the list of stashes.
- Managing Stashes (
git stash list
andgit stash drop
)
Purpose: git stash list
shows all stashes you have made. git stash drop
removes a specific stash from the list.
Example:
git stash list
git stash drop stash@{0}
These commands list all stashes and remove the first stash in the list, respectively.
6. Managing Remote Repositories
Setting Up Remotes
- Adding and Removing Remote Repositories (
git remote add
andgit remote rm
) Purpose:git remote add
is used to add a new remote repository, whilegit remote rm
removes an existing remote. Example:
git remote add origin https://github.com/user/repo.git
git remote rm origin
These commands add and remove a remote named “origin”, respectively.
- Fetching Updates from Remotes (
git fetch
)
Purpose: The git fetch
command downloads objects and refs from another repository. It’s a safe way to review changes before integrating them into your code.
Example:
git fetch origin
This fetches updates from the remote repository named “origin”.
- Viewing Remote Repository URLs (
git remote -v
)
Purpose: The git remote -v
command displays the URLs that each remote repository points to.
Example:
git remote -v
This shows a list of the URLs for the remote repositories associated with your local repository.
7. Tagging and Releases
Creating and Managing Tags
- Creating Annotated Tags (
git tag
)Purpose: Thegit tag
command is used to create, list, delete, or verify a tag object signed with a GPG key. Tags are often used to mark release points. Example:
git tag -a v1.0 -m "Version 1.0"
This creates an annotated tag named “v1.0” with the message “Version 1.0”.
- Pushing Tags to a Remote (
git push origin --tags
)
Purpose: The git push origin --tags
command uploads all tags to the remote repository.
Example:
git push origin --tags
This pushes all tags to the “origin” remote.
- Deleting Local and Remote Tags (
git tag -d
andgit push origin --delete
)
Purpose: git tag -d
deletes a tag from the local repository, while git push origin --delete
deletes a tag from the remote repository.
Example:
git tag -d v1.0
git push origin --delete v1.0
These commands delete the tag “v1.0” locally and remotely, respectively.
8. Undoing Changes and Recovery
Reverting and Resetting
- Reverting Commits (
git revert
)Purpose: Thegit revert
command creates a new commit that undoes the changes from a previous commit. This is a safer way to undo changes as it doesn’t alter the commit history. Example:
git revert HEAD
This creates a new commit that reverses the changes made in the latest commit.
- Resetting the Repository State (
git reset
)
Purpose: The git reset
command resets the current HEAD to a specified state. It can modify the index (staging area) and the working directory depending on the options used.
Example:
git reset --hard HEAD~1
This command resets the repository to the state of the previous commit, discarding all changes.
- Discarding Local Changes (
git checkout --
)
Purpose: The git checkout --
command is used to discard changes in the working directory. This can be useful if you want to undo modifications to a specific file.
Example:
git checkout -- file.txt
This command discards changes made to “file.txt”.
9. Advanced Git Commands
Cherry-Picking and Rebasing
- Cherry-Picking Commits (
git cherry-pick
)Purpose: Thegit cherry-pick
command applies the changes introduced by some existing commits. This is useful for applying specific fixes from one branch to another. Example:
git cherry-pick <commit-hash>
This applies the changes from the specified commit to the current branch.
- Interactive Rebasing (
git rebase -i
)
Purpose: The git rebase -i
command starts an interactive rebase session, allowing you to reorder, edit, or squash commits.
Example:
git rebase -i HEAD~3
- This starts an interactive rebase for the last three commits.
Cleaning Up and Optimization
- Cleaning Up the Working Directory (
git clean
)Purpose: Thegit clean
command removes untracked files from the working directory. This can help you clean up temporary files or other clutter.Example:
git clean -fd
This forcefully removes untracked directories and files.
- Garbage Collection (
git gc
)
Purpose: The git gc
command runs a number of housekeeping tasks within the Git repository, such as compressing file revisions and removing unreachable objects.
Example:
git gc
This optimizes the repository by cleaning up unnecessary files.
- Pruning Unreachable Objects (
git prune
)
Purpose: The git prune
command removes objects that are no longer referenced in any branch or tag, effectively cleaning up disk space.
Example:
git prune
- This command removes all objects that are not reachable from any of the references.
10. Additional Useful Commands
Viewing and Searching History
- Showing a Graphical Representation of the Commit History (
git log --graph
)Purpose: This command provides a visual representation of the commit history, showing branching and merging. Example:
git log --graph --oneline
This shows the commit history in a simplified graphical format.
- Searching for Specific Changes (
git log -p
)
Purpose: The git log -p
command shows the differences introduced in each commit, useful for code review or understanding changes.
Example:
git log -p file.txt
- This shows the changes over time for “file.txt”.
File Management
- Removing and Moving Files (
git rm
andgit mv
)Purpose:git rm
removes files from the working directory and the index, whilegit mv
moves or renames files.Example:
git rm file.txt
git mv oldname.txt newname.txt
These commands remove “file.txt” and rename “oldname.txt” to “newname.txt”, respectively.
Miscellaneous Commands
- Creating Patches (
git format-patch
)Purpose: Thegit format-patch
command prepares patch files for each commit, which can be sent via email or applied to another repository.Example:
git format-patch -1 HEAD
This creates a patch for the latest commit.
Applying Patches (git apply
)
Purpose: The git apply
command applies a patch to files and/or to the index.
Example:
git apply patch.diff
- This applies the changes from “patch.diff” to the working directory.
Conclusion
Mastering these Git commands is crucial for any software engineer. They enable efficient version control, collaboration, and project management. By understanding and using these commands, you can streamline your workflow, maintain a clean project history, and handle complex changes with confidence. Keep exploring Git’s features and commands to further enhance your development skills.
Happy Commiting
Leave a response to this article by providing your insights, comments, or requests for future articles.
Share the articles with your friends and colleagues on social media.
Let’s Get in Touch! Follow me on :
>GitHub: @gajanan0707
>LinkedIn: Gajanan Rajput
>Website: https://mrcoder701.com
>YouTube: mrcoder701
> Instagram: mr_coder_701

good
all the time i used to read smaller articles or reviews that also clear
their motive, and that is also happening with this paragraph which I am reading at this place.
I came across your site wanting to learn more and you did not disappoint. Keep up the terrific work, and just so you know, I have bookmarked your page to stay in the loop of your future posts. Here is mine at UY4 about Thai-Massage. Have a wonderful day!
It?¦s really a great and useful piece of information. I am glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.