Chuck's Academy

Git Branching and Merging

Advanced Tools and Commands

Advanced Tools and Commands in Git

Git offers a wide variety of advanced tools and commands that can significantly improve your workflow. This chapter introduces some of these tools and commands, delving into their uses and applications.

Stash: Save Temporary Changes

The git stash command allows you to save temporary changes that you do not want to commit yet. It is useful when you need to switch branches but do not want to lose your current work.

Save Changes in the Stash:

bash

Apply the Stash Changes:

bash

List the Stashes:

bash

Apply and Delete the Last Stash:

bash

Cherry-Pick: Apply Specific Commits

The git cherry-pick command allows you to apply specific commits from one branch to another. It is useful when you want to bring in only certain changes instead of a full merge.

Apply a Specific Commit:

bash

Example:

bash

Bisect: Find Problematic Commits

The git bisect command allows you to isolate the commit that introduced an error using a binary search between a range of commits.

Start Bisect:

bash

Mark the Commit as Good:

bash

Mark the Commit as Bad:

bash

Git will iterate through the commits to find the one that introduced the error. Once found, use the following command to finish:

bash

Interactive Rebasing: Modify Commit History

Interactive rebase is a powerful tool to rewrite commit history, allowing you to edit, combine, and reorder commits.

Start an Interactive Rebase:

bash

Reflog: Reference Log

The git reflog command allows you to see all the changes made to the references (such as HEAD) in your local repository. It is useful for recovering lost commits.

View the Reflog:

bash

Amending: Modify the Last Commit

If you made a mistake in the last commit, you can modify it without creating a new commit using git commit --amend.

Correct the Last Commit:

bash

Hooks: Task Automation

Git Hooks are scripts that run automatically during certain events. They can help automate workflows, such as pre-commit validations, deployments, or integrations with other tools.

Examples of Hooks:

  • pre-commit: Runs before a commit is made.
  • pre-push: Runs before a push is made.
  • post-merge: Runs after a merge is completed.

Hooks are located in the .git/hooks directory and can be configured by writing scripts in Shell, Python, Ruby, etc.

Example of a pre-commit hook in Bash:

bash

Submodules: Git in Subdirectories

Submodules allow you to include and manage Git repositories within another Git repository. They are useful for managing separate code dependencies.

Add a Submodule:

bash

Initialize and Update Submodules:

bash

Advanced Configurations

Aliases

Customize lengthy and repetitive commands by defining aliases in your Git configuration:

bash

Identity Configuration

You can define different identities for different repositories:

bash

Summary

  1. Stash: Save and recover temporary changes.
  2. Cherry-Pick: Apply specific commits to another branch.
  3. Bisect: Find the commit that introduced an error.
  4. Reflog: View the reference change history.
  5. Amending: Correct the last commit.
  6. Hooks: Automate tasks with scripts.
  7. Submodules: Manage dependencies within a repository.
  8. Advanced Configurations: Aliases and identity configuration.

Using these advanced tools and commands can significantly increase your efficiency and organization when working with Git. In the next chapter, we will explore the conclusion and final recommendations for using Git.


Ask me anything