The Top 15 Git Commands for Better Version Control
Sandeep Kumar
???? Sr. Software Engineer | JavaScript/TypeScript | Angular | React.js | Express | Python | Generative AI | Spring Boot/Java
Hi Everyone,
I know this topic isn’t new, Developers on LinkedIn share slides on Git commands regularly.
However, this is my second article, and I want to share what I know well.
Git is undoubtedly one of the best version control systems out there.
I know we can use Git without running commands directly in Visual Studio Code, and there’s also a Git desktop app to simplify the process. However, knowing these commands will make you a better developer, so don’t rely solely on these apps and pre-installed extensions.
When something becomes so essential in life that we use it daily, it's our responsibility to understand it thoroughly and develop a deep knowledge of it.
I won’t waste your time, so here are the 15 Git commands that I believe every developer should know.
1. git init
What it does: Initializes a new Git repository.
When to use it: Use this command in a project folder to track changes with Git.
git init
This will create a hidden .git folder that stores all version history.
2. git clone
What it does: Copies an existing Git repository from a remote location to your local machine.
When to use it: When you want to work on a project that someone else has shared or a project hosted on platforms like GitHub.
git clone <repository_url>
Replace <repository_url> with the URL of the repository you want to clone.
3. git add
What it does: Stages changes to be committed.
When to use it: Whenever you make changes to your files and want to prepare them for a commit.
git add <filename>
Or, use git add . to stage all changes in the current directory.
4. git commit
What it does: Records changes that have been staged.
When to use it: After staging changes with git add, use git commit to save these changes with a descriptive message.
git commit -m "Your message here"
Replace "Your message here" with a brief description of the changes.
5. git status
What it does: Shows the status of files in the working directory.
When to use it: Use this to check which files are staged, unstaged, or untracked.
git status
6. git push
What it does: Sends local commits to a remote repository.
When to use it: After committing changes locally, use git push to upload them to a shared repository.
git push origin <branch_name>
Replace <branch_name> with the name of the branch you want to push.
7. git pull
What it does: Fetches changes from a remote repository and merges them into your local branch.
When to use it: Use git pull to keep your local branch up to date with the latest changes from the remote repository.
git pull origin <branch_name>
8. git branch
What it does: Manages branches in your repository.
When to use it: Use it to list branches, create a new branch, or delete branches.
git branch
Create a new branch:
git branch <new_branch_name>
Delete a branch:
git branch -d <branch_name>
9. git checkout
What it does: Switches branches or restores files.
When to use it: Use it to move to a different branch or revert changes in files.
Switch branches:
git checkout <branch_name>
Create and switch to a new branch:
领英推荐
git checkout -b <new_branch_name>
10. git merge
What it does: Combines changes from one branch into another.
When to use it: Use it to integrate work from different branches.
git merge <branch_name>
This command merges <branch_name> into the branch you’re currently on.
11. git log
What it does: Shows a history of commits.
When to use it: Use git log to see a list of previous commits, along with their messages, authors, and timestamps.
git log
12. git stash
What it does: Temporarily saves changes without committing them.
When to use it: Use this when you need to switch branches but aren’t ready to commit your changes yet.
Save changes to the stash:
git stash
Apply stashed changes:
git stash apply
13. git remote
What it does: Manages remote connections.
When to use it: Use git remote to view or update remote repository URLs.
List remote connections:
git remote -v
Add a new remote repository:
git remote add origin <repository_url>
14. git revert
What it does: Reverses a previous commit by creating a new commit.
When to use it: Use git revert if you want to undo changes safely without rewriting commit history.
git revert <commit_hash>
15. git reset
What it does: Undoes changes by moving the HEAD pointer to a previous commit.
When to use it: Use git reset to remove commits or changes from the history (typically used cautiously).
Soft reset (keeps changes in the working directory):
git reset --soft <commit_hash>
Hard reset (removes all changes):
git reset --hard <commit_hash>
The above commands are very basic and important to understand Git workflow.
By mastering these basics, you’ll have the tools you need to manage code changes efficiently and collaborate smoothly on any project.
I hope you like the article, feel free to comment, Happy Coding!
Checkout more articles at https://frontbackgeek.com/