DEV Community

Cover image for Essential Git Commands I Use Every Day as a Software Engineer
Yashraj
Yashraj

Posted on

Essential Git Commands I Use Every Day as a Software Engineer

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (5)

Collapse
 
tomasdevs profile image
Tomas Stveracek

Great overview for anyone who wants to master Git! πŸ‘ I’d like to add that writing good commit messages is just as important as using the commands. If you want to learn how to write clear and meaningful commits, check out my article Write Commits Like a Pro.

Collapse
 
tinapyp profile image
tinApyp

love this spot on article bro, btw that cherry-pick very romantic right?

Collapse
 
andrewbaisden profile image
Andrew Baisden

Git makes productivity and project management super easy.

Collapse
 
codycodes profile image
Cody Antonio Gagnon

Spot on - I'm also using these commands each and every day! 😁

Collapse
 
codycodes profile image
Cody Antonio Gagnon • Edited

some git aliases based on your post for the day to day ⚑

[alias]
s = status
a = add
cm = commit -m
c = clone
ch = checkout
cp = cherry-pick
f = fetch
pl = pull
ps = push
st = stash 
Enter fullscreen mode Exit fullscreen mode

If you're using Windows & WSL, I wrote an article on sharing aliases between them here!

Happy coding πŸ§‘πŸΌβ€πŸ’»