Essential Git Commands I Use Every Day as a Software Engineer
Yashraj
Posted on September 5, 2024
Hi there,
I am a software engineer and use Git for managing code repositories across the organization. In this blog, I will share the Git commands that are useful to me and which I use daily for my work.
First of all, if you don't know about Git, it is basically a "distributed version control system." It saves snapshots of your entire code repository, or codebase, and manages these snapshots over time. You can add, modify, or delete your code from the repository using specific commands provided by Git.
Here are the commands I use daily to push my code and manage it:
1. git add
This command moves modified files to the staging area, preparing them for the next commit.
Example:
git add <filename>
git add . # Adds all modified files in the current directory
Use git add
when you've made changes to one or more files and want to include those changes in the next commit.
2. git commit
This command creates a snapshot of the staged files and saves them to the local repository.
Example:
git commit -m "Your commit message here"
The -m
flag allows you to include a message that describes the changes. A good commit message should be concise yet descriptive.
3. git push
This command pushes your committed changes from your local repository to the remote repository, sending updates to the associated repository.
Example:
git push origin main
Use git push
after committing to share your changes with others.
4. git status
This command shows the current status of your repository, indicating which files have been modified, added, or deleted since the last commit.
Example:
git status
It's helpful to run this command frequently to check what changes are staged for the next commit.
5. git reflog
This command provides the history of all git
commands executed on your local repository, including their commit hash.
Example:
git reflog
git reflog
is useful when you need to recover a lost commit or see a log of actions you’ve taken locally.
6. git checkout
This command is used for switching between branches or creating new branches.
Example:
git checkout <branch-name>
git checkout -b <new-branch-name> # Creates and switches to a new branch
git checkout
is essential when you need to work on different features or bug fixes simultaneously.
7. git clone
This command creates a copy of a remote code repository on your local machine.
Example:
git clone <repository-url>
Use git clone
to download a repository for the first time.
8. git cherry-pick
My favorite command! It allows you to apply changes from specific commits made by someone else to your branch.
Example:
git cherry-pick <commit-hash>
This is useful when you want to include a specific fix or feature from another branch without merging the whole branch.
9. git pull
This command fetches the latest changes from the remote repository and merges them into your current branch.
Example:
git pull origin main
Use git pull
to keep your local branch up to date with the remote repository.
10. git fetch
This command downloads all the latest changes from the remote repository, including updates to all branches, without merging them into your local branches.
Example:
git fetch origin
git fetch
is useful when you want to see what others have committed without immediately merging their changes into your branch.
11. git stash
When you want to switch branches but have uncommitted changes, you can temporarily save your changes using this command.
Example:
git stash
git stash apply # To apply stashed changes later
Use git stash
to store your work in progress without committing it.
Thank you for reading! If I missed any essential commands, feel free to share them in the comments.
Also Read : What Happens When You Enter 'google.com' ?
Posted on September 5, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.