DEV Community

Documendous
Documendous

Posted on

10 Lesser Known Git Commands and Techniques You Should Know

Git is a powerful version control system used by developers worldwide to manage and track changes in their codebases. While most developers are familiar with basic Git commands like git commit, git push, and git pull, there are many advanced commands and techniques that can greatly enhance your workflow and productivity. In this post, we’ll explore 10 lesser-known Git commands and techniques that you probably haven't used yet but can significantly streamline your development process and improve your version control skills. Dive in and discover how these advanced features can help you tackle complex tasks with ease.

  1. git bisect: This command is used for binary search to find the commit that introduced a bug. Start with git bisect start, mark the current commit as bad with git bisect bad, and mark a known good commit with git bisect good [commit]. Git will then help you find the exact commit that introduced the issue.

  2. git reflog: This command allows you to view the history of all actions in the local repository, including those that are not part of the commit history, such as resets and rebases. Use git reflog to see this history.

  3. git rerere: This stands for "reuse recorded resolution." It helps to remember how you resolved a conflict and reuses that resolution if the same conflict happens again. Enable it with git config --global rerere.enabled true.

  4. git blame -C -C: This variant of git blame tracks moved or copied lines of code across files. The -C -C options increase the likelihood of detecting such changes.

  5. git commit --fixup [commit]: This command marks a commit as a fixup of an existing commit. Later, you can use git rebase -i --autosquash to automatically squash the fixup commit into the target commit.

  6. git worktree: This command allows you to check out multiple branches at once in separate working directories. Use git worktree add [path] [branch] to create a new working tree.

  7. git stash push -m [message]: This command allows you to stash changes with a message. Use git stash list to see the list of stashes with their messages.

  8. git cherry-pick -n [commit]: The -n (no-commit) option applies the changes from the commit but does not create a commit. This allows you to make additional changes before committing.

  9. git filter-branch: This powerful command allows you to rewrite history, such as removing sensitive data or changing author information. Use with caution, as it can rewrite large parts of history.

  10. git clean -fdx: This command removes all untracked files and directories, including ignored ones. Use it to clean your working directory completely.

Top comments (2)

Collapse
 
audioboxer217 profile image
Scott Eppler

Several of these I knew, and use somewhat regularly, but git commit --fixup [commit] is going to a be huge help! I hate it when I notice a typo or something that needs to be in a commit several commits back, and I really hate to create a stupid "Fix typo in x" commit. So I usually go through the pain of reverting back to that commit, making the fix, then re-doing every commit that came after.

Collapse
 
documendous profile image
Documendous

Yes, very helpful to make sure those messages are correct. Thanks for the read, Scott!