Git, the ubiquitous version control system, is celebrated for its robustness and versatility. Beyond the everyday commands like git add and git commit, there are lesser-known gems that can significantly enhance your workflow. In this exploration, we'll uncover the secrets of 4 uncommon commands that can assist you in day-to-day development.
Cherry-Pick: Picking Cherries from Different Branches
The git cherry-pick command allows developers to apply specific commits from one branch to another. It's like picking cherries from one tree and placing them onto another.
How to Use:
Let's say you want to bring a commit from branch-A to your current branch:
git cherry-pick <commit-hash>
This command grabs the changes introduced by the specified commit and applies them to your current branch.
Example:
Move to branch with source commit-
git checkout branch-A
Show list of all commits and copy one commit SHA from list-
git log
Switch to the target branch
git checkout target-branch
Cherry-pick a commit from another branch-
git cherry-pick abc123
Stash: Temporarily Shelving Changes
The git stash command is a versatile tool when you find yourself in a situation where you need to switch branches or perform some quick fixes without committing your current changes. It temporarily shelves your modifications, allowing you to work in a clean state.
How to Use:
Stash current uncomment changes:
git stash
This command saves your changes in a new stash. Git reverts your working directory to the last committed state.
List all Stashes:
git stash list
View the list of stashes to identify the one you want to apply. It stores all the stashes in a stack of stash (latest stash will be the first to apply)
Apply Stashed Changes:
git stash apply
Apply the latest stash to your working directory. If you have multiple stashes, specify desired the stash with git stash apply stash@{n}-
git stash apply stash@{n}
Pop latest Stash:
git stash pop
This command applies the latest stash and removes it from the stash list in one step.
When to use this command:
Suppose you're working on a feature branch, and an urgent bug fix is needed on the main branch. Instead of committing your changes, you can stash them, switch to main, make the fix, and then switch back.
Example:
Stash changes before switching branches
git stash
Switch to the main branch
git checkout main
Make the urgent bug fix
Switch back to the feature branch
git checkout feature-branch
Apply stashed changes
git stash apply
or if it's the latest stash - git stash pop
Commit Amend: Fixing the Last Commit
The git commit --amend command is a powerful tool for tweaking the last commit. It's useful when you realize there's something you missed, like a file to include or a small typo in the commit message.
How to Use:
Make changes to your files, and stage the changes
git add .
Amend the last commit
git commit --amend
This opens your default editor, allowing you to modify the commit message or leave it as is.
When to use:
Imagine you've just committed some changes but forgot to include a crucial change. Instead of creating a new commit, you can amend the last one.
Example:
Make changes
echo "New content" > crucial-file.txt
Stage the changes
git add crucial-file.txt
Amend the last commit
git commit --amend
The commit message editor will open, and you can add more changes to the existing commit or modify the message. This results in a single, updated commit rather than an additional one.
Diff: Spotting the Differences
The git diff command is invaluable for comparing changes between commits, branches, or working directory and staging area.
How to Use:
Compare working directory with the last commit
git diff
Compare two branches
git diff branch-A branch-B
Show changes staged for the next commit
git diff --staged
Example:
Compare the current changes with the last commit
git diff
Compare changes between two branches
git diff feature-branch master
Conclusion:
These lesser-known Git commands can offer powerful capabilities to enhance your version control workflow. Incorporating them into your daily Git repertoire can lead to more efficient and streamlined development processes. So go ahead, unleash the potential of these hidden gems, and elevate your Git mastery, and let me know if you want me to ubveal more commands!
Top comments (0)