Today I continued learning Git and GitHub, focusing on branching and collaboration commands.
Branch
A branch in Git is a separate line of development. It allows you to work on new features, fixes, or experiments without affecting the main project.
Most repositories have a default branch called:
main
Developers usually create new branches to:
- Add new features
- Fix bugs
- Test ideas safely
Commands encountered:
git branch
The git branch command is used to view or manage branches.
View all branches
git branch
Example Output:
* main
feature-testing
The * indicates the current branch you are working on.
Create new branch
git branch feature-testing
This creates the branch but does not switch to it yet.
git checkout
git checkout is used to switch between branches.
git checkout -b <branch_name>
This command creates a new branch and switches to it at the same time.
Command
git checkout -b feature-testing
This is equivalent to running:
git branch feature-testing
git checkout feature-testing
git diff
git diff shows the difference between file changes.
This helps developers review what has changed before committing or merging.
git commit -am
This command is a shortcut that:
- Stages tracked files
- Creates a commit
Command
git commit -am "Update test feature"
Important notes:
- Works only for already tracked files
- New files still require
git add
Example:
git add newfile.go
git commit -m "Add new file"
git merge
git merge combines changes from one branch into another.
Example workflow
Switch to the branch you want to merge into:
git checkout main
Merge another branch:
git merge feature-testing
This brings the changes from feature-testing into main.
git pull
git pull updates your local repository with the latest changes from GitHub.
Command
git pull
It basically does two things:
- Fetch updates from the remote repository
- Merge them into your local branch
git branch -d
This command deletes a branch after it has been merged.
Command
git branch -d feature-login
This helps keep the repository clean by removing branches that are no longer needed.
Example Branch Workflow
A common workflow when adding a new feature:
git checkout -b feature-navbar
git add .
git commit -m "Add navbar feature"
git push
After finishing the feature:
git checkout main
git merge feature-navbar
git branch -d feature-navbar
In summary, I learned how branching works in Git and how it helps developers work on features without affecting the main branch. I practiced creating and switching branches using commands like git branch and git checkout -b. I also learned how to review changes with git diff, combine branches using git merge, and update my local repository with git pull. These commands helped me understand a basic workflow for developing features and managing code changes more efficiently using Git.
Top comments (0)