?? Mastering the 25 Essential Git Commands: A Software Engineer’s Experience
Vinoth Subbiah
"Skilled in Cloud Architecture and SRE with expertise in VMware, Infrastructure, Security, Automation, Monitoring, Python, GitOps, and Cloud Operations (CloudOps)."
Let’s face it – Git can feel like an enigma at times. As someone who has spent over two years navigating version control, I get it – Git can be confusing ??. But in practice, I’ve relied on a core set of commands covering 99% of my daily needs. Here’s a breakdown of the Git commands I use the most, along with the workflow that works for me.
?? git diff
Description: Shows the differences in your files that haven't been staged yet.
Example:
git diff
This command shows the changes in files that are modified but haven’t been added to the staging area.
?? git commit -a -m "commit message"
Description: Commits all tracked changes with a message in one command.
Example:
git commit -a -m "Fixed bug in login logic"
This saves all modified tracked files with a commit message, without needing to manually add them first.
?? git status
Description: Provides a snapshot of your working directory’s state.
Example:
git status
This gives you a summary of changes – files that are staged, unstaged, or untracked.
? git add file_path
Description: Stages specific file(s) for a commit.
Example:
git add src/app.js
This stages the app.js file, getting it ready for the next commit.
?? git checkout -b branch_name
Description: Creates and switches to a new branch.
Example:
git checkout -b feature/login-improvement
This creates a new branch called feature/login-improvement and switches to it immediately.
?? git checkout branch_name
Description: Switches to an existing branch.
Example:
git checkout main
This switches back to the main branch.
?? git commit --amend
Description: Modifies the last commit (e.g., to fix a message).
Example:
git commit --amend -m "Corrected typo in commit message"
This updates the message of your most recent commit without creating a new one.
?? git push origin branch_name
Description: Pushes your local branch to a remote repository.
Example:
git push origin feature/login-improvement
This pushes your feature/login-improvement branch to the remote repository (e.g., GitHub).
?? git pull
Description: Fetches and merges changes from a remote branch into your current branch.
Example:
git pull origin main
This fetches updates from the main branch and merges them into your current branch.
??? git rebase -i
Description: Interactively rebases your branch, letting you modify commit history.
Example:
git rebase -i HEAD~3
This interactively rebases the last three commits, allowing you to squash, edit, or reorder them.
?? git clone
Description: Creates a local copy of a remote repository.
Example:
git clone https://github.com/user/repo.git
This creates a local copy of the repository from GitHub.
?? git merge
Description: Merges changes from one branch into another.
Example:
git merge feature/login-improvement
This merges the feature/login-improvement branch into your current branch.
?? git log --stat
Description: Shows commit logs with detailed statistics on changes.
领英推荐
Example:
git log --stat
This displays the commit history along with details like the number of lines added or deleted.
??? git stash
Description: Temporarily saves changes without committing them.
Example:
git stash
This stashes your changes so you can work on something else without losing progress.
?? git stash pop
Description: Applies and removes stashed changes.
Example:
git stash pop
This restores your stashed changes and removes them from the stash list.
???♂? git show commit_id
Description: Displays detailed information about a specific commit.
Example:
git show 2c3a1d
This shows what was changed in the commit with the ID 2c3a1d.
? git reset HEAD~1
Description: Reverts the last commit but keeps the changes locally.
Example:
git reset HEAD~1
This undoes the last commit but preserves the changes so you can recommit or edit them.
?? git format-patch -1 commit_id
Description: Creates a patch file for a specific commit.
Example:
git format-patch -1 2c3a1d
This generates a patch file for the commit 2c3a1d.
?? git apply patch_file_name
Description: Applies changes from a patch file.
Example:
git apply 0001-fix-typo.patch
This applies the patch from the file 0001-fix-typo.patch to your working directory.
? git branch -D branch_name
Description: Deletes a branch forcefully.
Example:
git branch -D feature/old-branch
This deletes the branch feature/old-branch even if it hasn’t been merged.
?? git reset
Description: Moves the branch reference to a different commit, effectively undoing commits.
Example:
git reset --soft HEAD~2
This undoes the last two commits but keeps the changes staged.
?? git revert
Description: Creates a new commit that undoes changes from a previous commit.
Example:
git revert 2c3a1d
This creates a new commit that reverses the changes made by commit 2c3a1d.
?? git cherry-pick commit_id
Description: Applies changes from a specific commit from another branch.
Example:
git cherry-pick 2c3a1d
This applies the changes from commit 2c3a1d onto your current branch.
??? git branch
Description: Lists all branches in your repository.
Example:
git branch
This displays a list of all branches in your project, highlighting the current branch.
?? git reset --hard
Description: Resets everything to a previous commit, erasing all uncommitted changes.
Example:
git reset --hard HEAD~3
This rolls back your project to the state it was in three commits ago, discarding all changes.