Welcome to the Advanced Git Guide

In this guide, we’ll explore advanced Git techniques and commands to elevate your version control skills. Whether you’re collaborating on a large project or managing complex development workflows, mastering these advanced Git concepts will make you a more efficient and effective developer.

We assume you have a basic understanding of Git and are familiar with common commands like git clone, git add, git commit, and git push. Now, let’s dive deeper into the world of Git and uncover its hidden treasures.

Let’s get started!


Branching and Merging

Create a new branch

git checkout -b new-feature

Switch between branches

git checkout master  # Switch to master branch
git checkout new-feature  # Switch to new-feature branch

Merge branches

git checkout master  # Switch to the branch you want to merge into
git merge new-feature  # Merge new-feature branch into master

Resolve merge conflicts

When there are merge conflicts, Git will indicate which files have conflicts. Open those files, resolve the conflicts manually, then add the changes and commit them.


Delete a branch

git branch -d new-feature  # Deletes the branch if changes are merged
git branch -D new-feature  # Forcefully deletes the branch

Rebasing

Rebase your branch onto another branch

git checkout feature-branch
git rebase master

Squash commits during rebase

During the rebase process, you can squash multiple commits into one for cleaner history.

git rebase -i HEAD~3  # Squash last 3 commits

Stashing

Stash changes

git stash save "Work in progress"  # Stash changes with a message

Apply stashed changes

git stash apply stash@{0}  # Apply the first stash

List stashed changes

git stash list  # List all stashed changes

Drop stashed changes

git stash drop stash@{0}  # Drop the first stash

Rewriting History

Amend the last commit

git commit --amend

Interactive rebase to edit, squash, or reorder commits

git rebase -i HEAD~3  # Interactively rebase last 3 commits

Git Hooks

Pre-commit hook

Automate checks before committing changes, like linting or running tests.

# Create a file named pre-commit in .git/hooks/ and make it executable

Post-merge hook

Automate tasks after a merge, like updating dependencies or generating documentation.

# Create a file named post-merge in .git/hooks/ and make it executable

Git Workflows

Git Flow

A branching model for larger projects with multiple developers.

git flow feature start new-feature  # Start a new feature
git flow feature finish new-feature  # Finish a feature

GitHub Flow

A simpler workflow focusing on pull requests for collaboration.

# Create a new branch, make changes, push branch, create a pull request, review, merge

Conclusion

These advanced Git commands and workflows will enhance your productivity and help you manage complex development scenarios more effectively. Experiment with them in your projects to become a Git expert. Remember, with great power comes great responsibility, so use these commands wisely!

Last updated 01 Sep 2024, 10:22 CEST . history