git

Essential Git Commands for Daily Use

"Essential Git Commands for Daily Use"

Posted by Backend Byte on May 17, 2024

As a engineer at Poridhi, I frequently need to use Git commands to manage my codebase, track changes, collaborate with team members, and maintain an organized workflow. Over the past five months of working in this industry, I’ve become familiar with several essential Git commands that have been invaluable in my daily work.

In this blog post, I’ll share these essential Git commands and provide a brief explanation along with a small example for each one. Whether you’re a beginner or an experienced developer, mastering these commands can greatly enhance your productivity and efficiency when working with Git repositories.

  1. git status
    • Shows the current state of the working directory, including modified files, staged changes, and untracked files.
    • Example: git status
  2. git add
    • Stages changes for the next commit.
    • Example: git add app.go (stages a specific file) or git add . (stages all changes in the current directory)
  3. git commit
    • Records staged changes to the repository with a commit message.
    • Example: git commit -m "Add new feature"
  4. git push
    • Pushes committed changes to a remote repository.
    • Example: git push origin main (pushes changes to the main branch on the origin remote)
  5. git pull
    • Fetches changes from a remote repository and merges them into the current branch.
    • Example: git pull origin main
  6. git branch
    • Lists existing branches or creates a new branch.
    • Example: git branch new-feature (creates a new branch named new-feature)
  7. git checkout
    • Switches between branches or restores files from earlier commits.
    • Example: git checkout -b features/features-name (switches to the features/features-name branch)
  8. git merge
    • Merges changes from one branch into another.
    • Example: git merge new-feature (merges the new-feature branch into the current branch)
  9. git rebase
    • Integrates changes from one branch into another by rewriting the commit history.
    • Example: git rebase main (rebases the current branch onto the main branch)
  10. git log
    • Shows the commit history.
    • Example: git log -5 (displays the last 5 commits)
    • Example: git log -5 --oneline (displays the last 5 commits in a condensed, one-line-per-commit format, showing the commit hash and message)
  11. git clean
    • Removes untracked files and directories from the working directory.
    • Example: git clean -fd (removes untracked files and directories, using the f force and d directories flags)
  12. git stash
    • Temporarily shelves changes in the working directory.
    • Example: git stash (stashes current changes)
  13. git stash pop
    • Reapplies the most recently stashed changes to the working directory.
    • Example: git stash pop (applies the most recent stash and removes it from the stash list)
  14. git cherry-pick
    • Applies specific commits from one branch to another.
    • Example: git cherry-pick <commit-hash> (applies the commit with the specified hash to the current branch)
  15. git branch --delete
    • Deletes a specified branch from the local repository.
    • Example: git branch --delete feature/old-feature (deletes the feature/old-feature branch)
  16. git reset
    • Resets the working directory and staging area to a specific commit.
    • Example: git reset --hard HEAD (discards all changes since the last commit)

These commands cover various aspects of Git workflow, from staging and committing changes to managing branches, integrating changes, and reverting to previous states. By mastering these commands, you’ll be able to work more efficiently and collaborate more effectively with your team.

Remember, Git is a powerful tool, and it’s essential to use it responsibly and follow best practices to avoid potential issues or conflicts.