It's a tech blog!

Advancing Your Git Skills: Intermediate Git Commands and Concepts

By Marcelo Vieyra, November 1st 2023 | 4 mins, 602 words

Once you've grasped the basics of Git, it's time to take your version control skills to the next level. In this article, we'll explore intermediate Git commands and concepts that will help you work more efficiently and effectively on your software projects.

1. Git Branching Strategies

Branching is not just about creating feature branches; it's also about organizing your workflow. Here are a few advanced branching strategies:

Feature Branches: Create a branch for each new feature or bug fix. Once the work is complete, merge it into the main branch.

Release Branches: Before deploying a new version, create a release branch. This branch allows you to fix critical bugs without disrupting ongoing development.

Hotfix Branches: When a critical issue arises in production, create a hotfix branch to address it. Afterward, merge the changes into both the release branch and the main branch.

2. Interactive Rebasing

Interactive rebasing lets you modify commit history. You can squash multiple commits into one, reword commit messages, or even reorder commits. Use the following command:

git rebase -i HEAD~n

Replace n with the number of commits you want to include in the interactive rebase.

3. Stash Changes

The git stash command allows you to save your current changes temporarily, switch branches, and then reapply the changes. It's handy when you're in the middle of something and need to work on a different task:

git stash save "Your stash message"

4. Cherry-Picking Commits

Cherry-picking enables you to choose specific commits from one branch and apply them to another. This is useful for selectively integrating changes:

git cherry-pick <commit_hash>

5. Git Hooks

Git hooks are scripts that run automatically when certain events occur, such as pre-commit, post-commit, or pre-receive. You can use them to enforce coding standards, run tests, or trigger deployment scripts.

Hooks are stored in the .git/hooks directory in your repository.

6. Reflog: Your Safety Net

The reflog is your safety net when things go wrong. It records all the changes to branch references, allowing you to recover lost commits or branches. Use:

git reflog

7. Git Bisect

When you're faced with a bug and need to identify the exact commit that introduced it, Git Bisect is your friend. It performs a binary search through your commit history to find the problematic commit:

  • Start a bisect session with git bisect start.
  • Mark a known good commit with git bisect good <commit_hash>.
  • Mark a known bad commit with git bisect bad <commit_hash>.
  • Git will guide you to the problematic commit.

8. Submodules

Submodules allow you to include one Git repository inside another. This is useful for managing dependencies on third-party libraries. To add a submodule, use:

git submodule add <repository_url>

To initialize and update submodules, use:

git submodule init
git submodule update

9. Git Workflows

Understanding and implementing Git workflows, like Gitflow or GitHub Flow, can greatly enhance collaboration within your team. These workflows define how branches are used, when to release code, and how to handle hotfixes.

10. Git Aliases

Git aliases allow you to create shortcuts for frequently used Git commands. For example, you can create an alias to simplify git status:

git config --global status

These are just a few of the intermediate Git commands and concepts that can streamline your development process. Git is a versatile and powerful tool, and the more you explore its features, the better equipped you'll be to manage your projects and collaborate effectively with your team.

As you continue to build your Git skills, don't be afraid to experiment and explore advanced features. The more you use Git, the more proficient you'll become in leveraging its capabilities for efficient and productive software development. Happy coding!