Git Branches
Understanding Branches
A branch in Git establishes an independent line of development within a project. Multiple branches can progress simultaneously without interference. After development and testing on a branch, it can be merged back into the primary branch.
Benefits of Branching
- Isolates development work, preventing conflicts between different feature versions.
- Encapsulates a distinct phase of development, such as a new feature or experimental change.
- Facilitates bug fixes by allowing dedicated branches for investigation without affecting stable code.
Key Branching Commands
Create a new branch:
~/project$ git branch feature-experimental
List all branches:
~/project$ git branch
feature-experimental
* main # The asterisk (*) indicates the currently active branch.
Delete a branch:
~/project$ git branch -d feature-experimental
Rename a branch:
~/project$ git branch
issue-patch
* main
~/project$ git branch -m issue-patch hotfix # Renames 'issue-patch' to 'hotfix'
~/project$ git branch
hotfix
* main
Switch to a different branch:
~/project$ git branch
hotfix
* main
~/project$ git switch hotfix # Activates the 'hotfix' branch
Switched to branch 'hotfix'
~/project$ git branch
* hotfix
main
Note: If you have uncommitted modifications in your working directory, Git will prevent you from switching branches to protect those changes. You can use the -f flag to force the switch, but this will discard local changes, which is generally not recommended.
Example Workflow:
~/project$ git branch
hotfix
* main
~/project$ ls
readme.md
~/project$ git switch hotfix
Switched to branch 'hotfix'
~/project$ ls
readme.md
~/project$ touch config.yaml
~/project$ git add .
~/project$ git commit -m "Add initial config file"
[hotfix a1b2c3d] Add initial config file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 config.yaml
~/project$ ls
config.yaml readme.md
~/project$ git switch main
Switched to branch 'main'
~/project$ ls
readme.md # The config.yaml file exists only on the hotfix branch
Merge a branch:
~/project$ git branch
hotfix
* main
~/project$ git merge hotfix # Merges the 'hotfix' branch into the current ('main') branch
Updating x7y8z9a..a1b2c3d
Fast-forward
config.yaml | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 config.yaml
~/project$ ls
config.yaml readme.md
Note: A merge conflict occurs if the same file is modified in diffferent ways on both branches (e.g., changed on hotfix and also chenged differently on main). To list files with conflicts, use: git diff --name-only --diff-filter=U.
Using git diff
Common Applications
- Resolving merge conflicts by examining conflicting changes.
- Generating patch files to share code changes.
git diff Commands
Compare the working directory to the staging area (index):
~/project$ cat logfile.txt
Initial log entry
~/project$ echo "New debug info" >> logfile.txt
~/project$ git add logfile.txt
~/project$ echo "Another line" >> logfile.txt
~/project$ git diff # Shows unstaged changes not yet added
diff --git a/logfile.txt b/logfile.txt
index f123abc..a456def 100644
--- a/logfile.txt # Represents the file as staged
+++ b/logfile.txt # Represents the file in the working directory
@@ -1,2 +1,3 @@ # Hunk header: -1,2 (old) and +1,3 (new) lines
Initial log entry
New debug info
+Another line # Line added in working dir but not staged
View changes that are staged but not committed:
~/project$ git diff --staged # Or --cached
diff --git a/logfile.txt b/logfile.txt
index e69de29..f123abc 100644
--- a/logfile.txt
+++ b/logfile.txt
@@ -0,0 +1,2 @@
+Initial log entry
+New debug info
Compare the working directory to the last commit (HEAD):
~/project$ git diff HEAD logfile.txt # 'logfile.txt' is optional
diff --git a/logfile.txt b/logfile.txt
index f123abc..b789ghi 100644
--- a/logfile.txt
+++ b/logfile.txt
@@ -1,2 +1,4 @@
Initial log entry
New debug info
+Another line
+Final test entry
Other useful diff commands:
git diff feature-branch # Compare current branch to 'feature-branch'
git diff branch-alpha branch-beta # Compare two specified branches (beta vs. alpha)
git diff abc123 def456 # Compare two specific commits by their hash
git diff --stat # Show a summary of changed files