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
- Stash: Save and recover temporary changes.
- Cherry-Pick: Apply specific commits to another branch.
- Bisect: Find the commit that introduced an error.
- Reflog: View the reference change history.
- Amending: Correct the last commit.
- Hooks: Automate tasks with scripts.
- Submodules: Manage dependencies within a repository.
- 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.
- Introduction to Git
- Initial Setup and Basic Workflow
- Basic Concepts of Branches in Git
- Creating and Deleting Branches
- Branch Navigation
- Branch Merging
- Resolución de Conflictos de Fusión
- Merge Strategies: Fast-Forward vs. Recursive
- Rebase in Git: Concepts and Uses
- Merge vs. Rebase: When to Use Each
- Remote Branches and Their Management
- Git Flow and Other Workflow Models
- Best Practices for Branching and Merging
- Advanced Tools and Commands
- Conclusion and Final Recommendations