Master Git: From Init to Branching, Merging, and Rebase
This comprehensive guide walks you through Git's core concepts and commands—including repository initialization, adding and committing files, pulling, pushing, branching, merging, rebasing, and useful tips—so you can confidently manage version control in any project.
Git Repository Basics
Git stores project history as a directed acyclic graph (DAG) inside a .git directory. The repository contains all files and metadata, and snapshots are created by staging changes in the index.
Essential Commands
Initialize : git init creates or re‑initializes a repository.
Status : git status shows staged, modified and untracked files.
Add : git add <files> or git add -A stages changes for the next commit.
Commit : git commit -m "message" records a snapshot of the index; git commit -a stages and commits tracked files in one step.
Remote : git remote add origin <url> defines a remote repository.
Pull : git pull origin <branch> fetches and merges remote changes.
Push : git push origin <branch> uploads local commits to the remote.
Branch Management
Create a branch: git branch <name> Switch to a branch: git checkout <name> or git checkout -b <name> (create and switch).
List branches:
git branchMerging
To integrate work from another branch, ensure you are on the target branch (e.g., git checkout master) and run: git merge <source-branch> The merge creates a commit with two parents, preserving both histories.
Rebasing
Rebase rewrites a series of commits onto a new base, producing a linear history:
git checkout <feature-branch></code>
<code>git rebase masterAfter rebasing, the feature branch appears as if it were developed sequentially after master.
Typical Workflow Example
Install Git and open Git Bash in the project directory.
Initialize: git init Create files (e.g., edureka1.txt, edureka2.txt) and check status.
Stage all files: git add -A Commit: git commit -m "Add initial files" Set remote: git remote add origin <url> Pull latest changes: git pull origin master Push commits:
git push origin masterAdvanced Tips
Archive a snapshot: git archive master --format=zip --output=../repo.zip Create a bundle for offline transfer: git bundle create ../repo.bundle master Stash uncommitted changes: git stash, view with git status, reapply with git stash apply.
Force push when non‑fast‑forward:
git push <remote> --forceSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
