Git (Part 3) – Branching, Merging, and Advanced Concepts
In this part of my Git learning journey, I explored some advanced concepts that are commonly used in real-world projects, such as pushing code to a new repository, branching, merging, rebasing, and handling conflicts.
Scenario 1: Sending Code to a New Remote Repository (Without Cloning)
Often we already have code locally but need to push it to a new GitHub repository.
The steps are:
git init # Initialize Git in the local project
git remote add origin <repo_url> # Link the local repo to remote
git add . # Stage all files
git commit -m "Initial commit" # Commit changes
git push -u origin main # Push to the remote repository
Explanation:
git initconverts the project into a Git repository.git remote add originconnects the local repo with the GitHub repo.After staging and committing,
git pushsends the code to GitHub.
Scenario 2: Can We Skip the Staging Area?
Question: Can files move directly from the working directory to the local repository (commit stage)?
Answer: No.
Git requires files to be staged before committing. This staging step ensures only selected changes are included in the commit.
Git Branches
Branches allow developers to work on features independently without disturbing the main codebase.
Creating and Switching Branches
git branch feature-1 # Create a new branch
git checkout feature-1 # Switch to the new branch
Explanation:
When you create and switch to a new branch, all existing files from the current branch are copied into the new branch.
Listing Branches
git branch # Show local branches
git branch -r # Show remote branches
git branch -a # Show all branches (local + remote)
Counting Branches
git branch | wc -l # Count local branches
git branch -r | wc -l # Count remote branches
git branch -a | wc -l # Count all branches
.gitignore
The .gitignore file specifies which files or folders should not be tracked by Git.
Example:
node_modules/
*.log
.env
Explanation:
node_modules/ensures dependency files are ignored.*.logexcludes all log files..envexcludes environment variable files.
Renaming a Branch
git branch -m oldName newName
This renames an existing branch without affecting its history.
Pull Requests (Remote to Remote Merge)
A Pull Request (PR) is a request to merge code from one branch into another on a remote repository (e.g., GitHub).
Commonly used for code reviews.
Ensures collaboration and controlled integration of features.
Deleting a Branch
git branch -D branchName
This permanently deletes a branch locally.
Branch Merging (Local to Local)
To merge one branch into another locally:
git checkout main
git merge feature-1
This merges changes from feature-1 into the main branch.
Git Rebase
Rebase is an alternative to merge. It integrates commits from one branch into another but creates a linear history.
Example:
git checkout feature-1
git rebase main
Merge vs Rebase
Merge: Retains commit history and shows multiple branches.
Rebase: Moves commits onto the target branch, resulting in a cleaner history without showing branch merges.
Merge Conflicts
Conflicts occur when the same line of code is modified in different branches. Git marks these conflicts in the file:
<<<<<<< HEAD
Code from current branch
=======
Code from merging branch
>>>>>>> feature-1
Steps to resolve:
Manually edit the file to choose or combine changes.
Stage the resolved file:
git add <file>.Commit the resolution:
git commit.
Key Takeaways
Code can be pushed to a new remote repo without cloning by initializing and linking the repo.
The staging area is mandatory before committing.
Branching helps in parallel development and can be listed, renamed, or deleted easily.
.gitignoreprevents unnecessary files from being tracked.Merging combines branches, while rebasing keeps history linear.
Merge conflicts are common and must be resolved manually.