Practical Git Guide: Key Commands Every Developer Should Know

Practical Git Guide: Key Commands Every Developer Should Know

In the world of software development, Git has become the de facto standard for version control. From individual projects to teams distributed worldwide, Git allows managing code changes, collaborating in an orderly manner, and maintaining a reliable work history.
However, for those starting out—and even for those who use it daily—some commands can be confusing or even “dangerous” if not understood well. In this article, we review several key commands, with clear examples and explanations.


1. git init and git clone

The starting point.

  • git init: converts a folder into an empty Git repository.

     
    git init
    
  • git clone: downloads an existing repository from a remote server (GitHub, GitLab, Bitbucket, etc.)

     
    git clone https://github.com/usuario/repositorio.git
    

2. git status

Probably the most used command day-to-day. It shows which files have changed, which are ready to be committed, and which are not.

 
git status

3. git add and git commit

The basic flow: adding changes and committing them.

  • git add file.txt → adds a specific file.

  • git add . → adds all changes in the current directory.

  • git commit -m "descriptive message" → saves the snapshot in the project history.

Example:

 
git add .
git commit -m "Fixes bug in form validation" 

4. git push and git pull

This is where real collaboration begins.

  • git push: sends local commits to the remote repository.

     
    git push origin main
    
  • git pull: downloads the latest changes from the remote and merges them with your current branch.

     
    git pull origin main
    

⚠️ Tip: before doing a push, make sure you have done a pull to avoid conflicts.


5. git reset --hard

This command is powerful, but also risky.
It is used to force your branch back to a previous state, discarding all uncommitted changes and commits after the point you specify.

 
git reset --hard <commit_id>

Example:

 
git reset --hard a3f5b2c

⚠️ Caution: you will lose changes that you have not pushed to the remote.


6. git checkout and git switch

Two ways to switch between specific branches or commits.

  • With checkout:

     
    git checkout -b new-branch
    
  • With switch (new and more readable):

     
    git switch -c new-branch
    

7. git merge and git rebase

Both are used to integrate changes, but they work differently:

  • merge: merges branches, keeping the history of both.

     
    git merge feature-branch
    
  • rebase: rewrites the history to make it look linear. Useful for keeping a clean repository, but must be used carefully if working with shared branches.

     
    git rebase main
    

8. git log and git reflog

  • git log: shows the commit history.

  • git reflog: allows you to see everything that has happened in the repository, even commits that seem lost after a reset. Very useful for recovering work.


9. git stash

Allows you to temporarily save uncommitted changes so you can switch branches without losing them.

 
git stash
git stash pop

Conclusion

Git is much more than “push and pull”. Knowing commands like reset --hard, reflog, or stash can save you hours of work and prevent catastrophic errors. The key is to understand what each instruction does before executing it.

At ForgeNEX, we help companies and development teams to implement Git workflows, improve collaboration, and ensure that code is managed professionally. Because good version control is not a luxury: it is the foundation of any serious software project.

Share: