What is a Branch?
A branch is an independent line of development within a repository. It allows developers to work on specific features, fixes, or experiments without impacting the stability of the main branch. Once changes are finalized and tested, they can be merged back into the main branch, and the temporary branch can be deleted.
Why Use Branches?
Branches enable developers to work concurrently and isolate their work. This ensures that unfinished or experimental changes don’t disrupt ongoing projects or production environments.
Common Types of Branches:
Master/Main: The default branch that contains stable, production-ready code.
Feature: For developing new features or functionalities.
Release: For final testing and preparation of a release version.
Hotfix: For urgent fixes to address critical production issues.
Working with Git and GitHub
Using GitHub UI
The GitHub UI simplifies repository management. Here’s how to create a repository:
Log in to your GitHub account.
Navigate to the Repositories tab and click New.
Fill in the repository name, optional description, and visibility (public/private).
Initialize the repository with optional files like a README, .gitignore, or license file.
Click Create Repository.
Your repository is now ready to use.
Using Git CLI
The Command Line Interface (CLI) offers flexibility and control for repository management. Here’s a detailed guide:
Repository Initialization and Cloning
Initialize a Git Repository:
git init
This command sets up a new Git repository in your current directory. A hidden
.git
folder is created to store metadata and objects for version control. Use this when starting a new project.Clone an Existing Repository:
git clone <repository-url>
Example:
git clone https://github.com/your-username/example-repo.git
This creates a local copy of a remote repository, including all files, commit history, and branches.
Staging and Committing Changes
Check Repository Status:
git status
Displays the state of your working directory and staging area, listing untracked, modified, or staged files.
Stage Changes:
Stage specific files:
git add <file-name>
Stage all files:
git add .
Commit Changes:
git commit -m "Your descriptive commit message"
Saves staged changes with a descriptive message.
Undo Staging (if needed):
git reset <file-name>
Removes a file from the staging area while retaining changes in the working directory.
Managing Branches
Creating and Switching Branches
Create a New Branch:
git branch <branch-name>
Switch to a Branch:
git checkout <branch-name>
Alternatively, create and switch in one step:
git checkout -b <branch-name>
List All Branches:
git branch
Deleting Branches
Delete Locally:
git branch -d <branch-name>
Use
-D
to force-delete an unmerged branch.Delete Remotely:
git push origin --delete <branch-name>
Working with Remotes
View Remote Repositories:
git remote -v
Add a Remote Repository:
git remote add origin <repository-url>
Rename a Remote:
git remote rename <old-name> <new-name>
Remove a Remote:
git remote remove <remote-name>
Pushing and Pulling Changes
Push Changes to a Remote Branch:
git push origin <branch-name>
Pull Changes from a Remote Repository:
git pull origin <branch-name>
Combines
git fetch
andgit merge
to update your local branch with changes from the remote.Fetch Changes Without Merging:
git fetch
Downloads updates without merging them.
Resolving Conflicts
Merge Another Branch:
git merge <branch-name>
Handle Merge Conflicts:
Open conflicting files and manually resolve issues.
Stage resolved files:
git add <resolved-file>
Complete the merge:
git commit
Advanced Git Commands
Rebasing Changes
Reapply Changes on Top of Another Base:
git rebase <branch-name>
Example:
git checkout feature-branch git rebase main
Interactive Rebase:
git rebase -i HEAD~3
Modify, squash, or reorder commits interactively.
Caution: Avoid rebasing shared branches as it rewrites commit history.
Picking Specific Commits
Apply a Specific Commit to Another Branch:
git cherry-pick <commit-hash>
Example:
git checkout main git cherry-pick abc123
Resolve Conflicts During Cherry-Pick:
Resolve conflicts manually.
Complete the cherry-pick:
git cherry-pick --continue
Pro Tip: Apply multiple commits:
git cherry-pick <commit-hash1> <commit-hash2>
Useful Debugging Commands
View Commit History:
git log
For a concise view:
git log --oneline
Check Differences Between Commits:
git diff <commit-hash1> <commit-hash2>
Revert a Commit:
git revert <commit-hash>
Discard Local Changes:
git checkout -- <file-name>
Resets a file to its last committed state.
Conclusion
Git branching strategies and commands empower developers to work efficiently and collaboratively. By isolating changes, maintaining clear commit histories, and managing conflicts effectively, teams can ensure code stability and seamless integration. Adopting a structured approach to Git usage not only streamlines development but also fosters better collaboration and project organization.