Exploring Lesser-Known but Essential Git Commands

While many are familiar with basic commands like git add, git commit, and git push, there are several lesser-known commands that can be equally important. Understanding these commands can help you become more efficient and effective in your version con…


This content originally appeared on DEV Community and was authored by JohnKagunda

While many are familiar with basic commands like git add, git commit, and git push, there are several lesser-known commands that can be equally important. Understanding these commands can help you become more efficient and effective in your version control practices.

1. git stash

Imagine you're working on a feature, and suddenly you need to switch branches to fix a critical bug. You don't want to commit incomplete work, but you also don't want to lose your changes. This is where git stash comes in handy.

Command:

git stash

This command temporarily shelves (or stashes) your changes, allowing you to switch branches without committing. Later, you can reapply your stashed changes with:

git stash apply

2. git cherry-pick

Have you ever committed a bug fix on one branch and then realized that the same fix is needed on another branch? Instead of copying and pasting the changes, you can use git cherry-pick.

Command:

git cherry-pick <commit-hash>

This command applies the changes from a specific commit to your current branch. It’s like plucking a cherry from one tree and placing it on another!

3. git bisect

When trying to find the commit that introduced a bug, git bisect can be a lifesaver. It uses a binary search algorithm to efficiently pinpoint the problematic commit.

Command:

git bisect start
git bisect bad
git bisect good <commit-hash>

Git will then check out different commits, asking you to mark them as good or bad, until it identifies the offending commit.

4. git reflog

Ever made a mistake and wished you could turn back time? git reflog is your time machine. It logs every change made to the tip of branches and allows you to recover lost commits.

Command:

git reflog

You can then use git checkout <commit-hash> or git reset --hard <commit-hash> to revert to a previous state.

5. git blame

Want to know who made a specific change in a file? git blame can tell you. It shows the commit hash, author, and timestamp for each line in a file.

Command:

git blame <file>

This can be useful for tracking down when a change was introduced and who made it.

6. git clean

When your working directory gets cluttered with untracked files, git clean can help you tidy up.

Command:

git clean -f

The -f flag stands for force, as Git wants to ensure you really want to remove those files. For added caution, use the -n flag to perform a dry run and see what would be removed:

git clean -n

7. git log --graph --oneline

If you want a visual representation of your commit history, git log --graph --oneline is perfect. It provides a concise, graphical view of your branch history.

Command:

git log --graph --oneline

This is particularly useful for understanding complex branching and merging histories.

8. git shortlog

For a summary of contributions by author, git shortlog is very helpful. It groups commit messages by author and can give you an overview of who has contributed what.

Command:

git shortlog

Add the -s flag to see a summary:

git shortlog -s

9. git diff

While git diff is commonly known, many don't use it to its full potential. It shows changes between commits, commit and working tree, etc.

Command:

git diff

To compare changes between two branches:

git diff branch1..branch2

10. git show

git show is a versatile command that shows various types of objects, including commits, trees, and tags.

Command:

git show <object>

For instance, to see the details of a specific commit:

git show <commit-hash>

Conclusion

By incorporating these lesser-known but powerful Git commands into your workflow, you can enhance your productivity and make your version control more effective.

Feel free to explore these commands in your projects, and don't hesitate to refer back to this guide whenever you need a quick refresher. Git is an indispensable tool, and mastering it can significantly improve your development experience.


This content originally appeared on DEV Community and was authored by JohnKagunda


Print Share Comment Cite Upload Translate Updates
APA

JohnKagunda | Sciencx (2024-08-05T12:30:28+00:00) Exploring Lesser-Known but Essential Git Commands. Retrieved from https://www.scien.cx/2024/08/05/exploring-lesser-known-but-essential-git-commands/

MLA
" » Exploring Lesser-Known but Essential Git Commands." JohnKagunda | Sciencx - Monday August 5, 2024, https://www.scien.cx/2024/08/05/exploring-lesser-known-but-essential-git-commands/
HARVARD
JohnKagunda | Sciencx Monday August 5, 2024 » Exploring Lesser-Known but Essential Git Commands., viewed ,<https://www.scien.cx/2024/08/05/exploring-lesser-known-but-essential-git-commands/>
VANCOUVER
JohnKagunda | Sciencx - » Exploring Lesser-Known but Essential Git Commands. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/05/exploring-lesser-known-but-essential-git-commands/
CHICAGO
" » Exploring Lesser-Known but Essential Git Commands." JohnKagunda | Sciencx - Accessed . https://www.scien.cx/2024/08/05/exploring-lesser-known-but-essential-git-commands/
IEEE
" » Exploring Lesser-Known but Essential Git Commands." JohnKagunda | Sciencx [Online]. Available: https://www.scien.cx/2024/08/05/exploring-lesser-known-but-essential-git-commands/. [Accessed: ]
rf:citation
» Exploring Lesser-Known but Essential Git Commands | JohnKagunda | Sciencx | https://www.scien.cx/2024/08/05/exploring-lesser-known-but-essential-git-commands/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.