Git Commit, Git Show, and Git Checkout

What is Git?
Before we start, let’s quickly understand what Git is. Git is like a time machine for your code. It helps you save different versions of your work, so you can go back to an older version if something goes wrong. It’s also great for collaborating with others on the same project.
Now, let’s talk about the three commands:
1. Git Commit: Saving Your Work
Think of Git Commit as taking a snapshot of your work. When you’re working on a project, you make changes to your files. Once you’re happy with those changes, you can save them using git commit.
Step-by-Step: How to Commit
Make Changes: Edit your files (e.g., add new code, fix a bug, or write some text).
Stage Your Changes: Before committing, you need to tell Git which changes you want to save. Use the command:
Copy
git add <file-name>For example, if you changed a file called
script.js, you’d type:Copy
git add script.jsIf you want to stage all your changes, you can use:
Copy
git add .Commit Your Changes: Now, save your changes with a message describing what you did. Use:
Copy
git commit -m "Your message here"For example:
Copy
git commit -m "Fixed the login button issue"This saves your changes in Git’s history.
Why is this useful?
Commits let you save your progress step by step. If something breaks later, you can always go back to a previous commit.
2. Git Show: Viewing a Commit
Once you’ve made a commit, you might want to see what changes were made in that commit. That’s where Git Show comes in. It’s like opening a photo album to look at a specific snapshot.
Step-by-Step: How to Use Git Show
Find the Commit ID: Every commit has a unique ID (a long string of numbers and letters). You can find it by running:
Copy
git logThis shows a list of all your commits. Look for the commit you want to inspect and copy its ID (you only need the first few characters).
View the Commit: Use the
git showcommand followed by the commit ID:Copy
git show <commit-id>For example:
Copy
git show 1a2b3c4This will show you the changes made in that commit, including which lines were added or removed.
Why is this useful?
It helps you understand what was changed in a specific commit, which is great for debugging or reviewing your work.
3. Git Checkout: Switching Between Versions
Git Checkout is like a time-traveling tool. It lets you switch between different versions of your project. You can go back to an old commit or switch to a different branch (a separate version of your project).
Step-by-Step: How to Use Git Checkout
Find the Commit or Branch: Use
git logto see a list of commits orgit branchto see a list of branches.Switch to a Commit: If you want to go back to a specific commit, use:
Copy
git checkout <commit-id>For example:
Copy
git checkout 1a2b3c4This will take you back to the state of your project at that commit. (Note: This puts you in a “detached HEAD” state, which means you’re not on a branch. To go back, you can switch to a branch using
git checkout mainorgit checkout master.)Switch to a Branch: If you want to switch to a different branch, use:
Copy
git checkout <branch-name>For example:
Copy
git checkout feature-loginThis switches your project to the
feature-loginbranch.
Why is this useful?
It lets you explore different versions of your project without losing your current work. You can always switch back to where you were.
Quick Recap
Git Commit: Save your changes with a message.
Git Show: View the details of a specific commit.
Git Checkout: Switch between different versions or branches.




