Git Commands Explained with Real-World Examples

M.F.M Fazrin
6 min readMay 30, 2024

--

Git is an essential tool for modern software development, providing powerful version control capabilities that help teams manage their codebases efficiently. Whether you are a seasoned developer or just starting your journey in coding, understanding Git commands is crucial for smoother collaboration, effective project management, and maintaining a clean and traceable history of your work.

Git is an essential tool for modern software development, providing powerful version control capabilities that help teams manage their codebases efficiently. Whether you are a seasoned developer or just starting your journey in coding, understanding Git commands is crucial for smoother collaboration, effective project management, and maintaining a clean and traceable history of your work.

In this article, we will break down some of the most commonly used Git commands, explaining each one with practical, real-world scenarios you might encounter while working on a website project.

From viewing changes and committing updates to branching strategies and merging code, these examples will guide you through the practical applications of Git, empowering you to use this tool with confidence. By the end of this guide, you will have a comprehensive understanding of various Git commands and how to apply them in your daily development workflow. Let’s dive in and explore the world of Git, one command at a time.

Here’s a breakdown of commonly used Git commands, explained with scenarios you might encounter while working on a website project:

1. git diff: Shows the difference between your working directory (unsaved changes) and the last commit.

Example: You’re editing the style.css file, adding new styles. git diff style.css will show you the lines of code you’ve added or modified, highlighted in green.

2. git commit -a -m “commit message”: Saves all tracked changes in your local repository with a descriptive message.

Example: After finishing the new website layout, you want to save your progress. git commit -a -m “Updated website layout with new styles” records all changes to tracked files with a clear message.

3. git status: Displays the current state of your repository, showing which files are modified, staged, or untracked.

Example: Before committing, you run git status. It tells you that style.css and index.html have been modified, giving you a chance to review changes before saving them.

4. git add file_path: Stages changes in a file, preparing them for the next commit.

Example: You’ve fixed a bug in script.js. git add script.js stages this file’s changes. You can continue working and stage other files before committing everything together.

5. git checkout -b branch_name: Creates a new branch from your current position and switches to it.

Example: You want to implement a contact form without affecting the main website. git checkout -b feature/contact-form creates a new branch specifically for this feature.

6. git checkout branch_name: Switches to an existing branch.

Example: After finishing the contact form, you run git checkout main to return to the main development branch.

7. git commit — amend: Modifies the last commit message or adds staged changes to it.

Example: You realize your last commit message had a typo. git commit — amend lets you fix it. Or, if you forgot to stage a file, you can stage it and then use git commit — amend — no-edit to add the changes to the last commit without changing the message.

8. git push origin branch_name: Uploads your local branch to a remote repository like GitHub.

Example: After thoroughly testing the contact form feature, git push origin feature/contact-form pushes your branch to the remote repository for collaboration or backup.

9. git pull: Downloads changes from a remote repository and merges them into your current branch.

Example: Your colleague pushed changes to the main branch. git pull origin main downloads and merges those changes into your local main branch, keeping your code up-to-date.

10. git rebase -i: Interactively rewrites commit history, allowing you to reorder, edit, or squash commits.

Example: You want to clean up your branch before merging it into main. git rebase -i lets you combine several small commits into one meaningful commit, improving the readability of your project history.

11. git clone <repository URL>: Creates a local copy of a remote repository.

Example: You want to contribute to an open-source project. git clone https://github.com/username/project.git creates a local copy of the project on your computer.

12. git merge branch_name: Combines the changes from a specified branch into your current branch.

Example: Your contact form feature is complete and tested. git checkout main followed by git merge feature/contact-form merges the changes into the main branch.

13. git log — stat: Displays a detailed commit history, including which files were modified and how many lines were added or deleted in each commit.

Example: You want to understand the evolution of a specific file. git log — stat — style.css shows you all commits that modified that file, providing insights into its development.

14. git stash: Temporarily saves changes you don’t want to commit yet, allowing you to switch branches or work on something else.

Example: You’re working on a new feature but need to fix a bug on the main branch immediately. git stash saves your current changes, giving you a clean working directory to work on the bug fix.

15. git stash pop: Reapplies the most recently stashed changes to your working directory.

Example: After fixing the bug, git stash pop restores your previous work on the new feature, allowing you to continue where you left off.

16. git show commit_id: Displays detailed information about a specific commit, including the changes made, author, and timestamp.

Example: You encounter a bug and suspect a specific commit introduced it. git show <commit ID> shows you the changes made in that commit, helping you pinpoint the source of the issue.

17. git reset HEAD~1: Reverts the last commit locally, keeping the changes in your working directory.

Example: You made a commit but realize it was a mistake. git reset HEAD~1 undoes the commit, but your changes remain in the working directory for you to adjust before committing again.

18. git format-patch -1 commit_id: Creates a patch file containing the changes introduced by a specific commit.

Example: You fixed a bug in your project and want to share the fix with someone else without them needing to pull your entire branch. git format-patch -1 <commit ID> generates a patch file that they can apply to their codebase.

19. git apply patch_file_name: Applies the changes from a patch file to your working directory.

Example: You received a patch file containing a bug fix. git apply bugfix.patch applies the changes from the patch file to your code.

20. git branch -D branch_name: Deletes a branch.

Example: After merging your contact form feature, you no longer need the feature branch. git branch -D feature/contact-form deletes it.

21. git reset: Resets your HEAD pointer and optionally the staging area and working directory to a specific commit, allowing you to undo changes.

Example: You want to discard all changes made since the last commit. git reset — hard HEAD resets your working directory and staging area to the last commit, effectively discarding all uncommitted changes.

22. git revert: Undoes a commit by creating a new commit that applies the inverse changes. This is a safer method for undoing changes on public branches.

Example: You accidentally pushed a commit containing sensitive data. git revert <commit ID> creates a new commit that removes the sensitive data, maintaining a clean history.

23. git cherry-pick commit_id: Applies the changes introduced by a specific commit from one branch to another.

Example: You have a bug fix on a feature branch that you need to apply to the main branch before the feature is fully complete. git cherry-pick <commit ID> applies the bug fix to your current branch without merging the entire feature branch.

24. git branch: Lists all branches in your local repository.

Example: You want to see all the branches you’ve created for your project. git branch will list them out, indicating the currently checked-out branch.

25. git reset — hard: Resets your local branch to match a specific commit, discarding all uncommitted changes. Use caution with this command.

Example: You’ve made a lot of changes to your code and decide to discard everything and start fresh from the last commit. git reset — hard HEAD will revert your working directory and staging area to the last commit.

By understanding and effectively using these Git commands, you can manage your codebase efficiently, collaborate with others seamlessly, and confidently navigate the world of version control.

--

--

M.F.M Fazrin
M.F.M Fazrin

Written by M.F.M Fazrin

Senior Software Development Specialist @ Primary Health Care Corporation (Qatar)

No responses yet