Core Concepts of Git
Git functions as a distributed version control system where every developer's working copy is also a complete repository, containing the full history of all changes. Unlike centralized alternatives, Git does not require a constant connection to a remote server; most operations occur locally.
Initializing and Tracking a Project
Before versioning begins, the workspace must be initialized. Navigate to the project root and run:
git init
After creating or editing files, check the state of the working tree:
git status
Untracked and modified items appear in red. Stage the desired snapshots using git add. For a single file use the exact name, or stage everything in the current directory with the dot flag:
git add presentation.pdf
git add .
Staged changes show in green. Commit them to the local repository with a concise, descriptive message:
git commit -m "feat: add quarterly data model"
If this is the first commit on a fresh machine, Git requires identity configuration (one-time step):
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Use git log to display the commit timeline. For a compact view, add flags like --oneline or --graph.
Navigating Through History
To move the HEAD and working directory to a previous state, apply a hard reset referencing the target commit hash:
git reset --hard a1b2c3d
If that commit needs to be recalled later, inspect the reference log (reflog) to find the "lost" hash:
git reflog
git reset --hard f7e8d9c
Note that reflog records every movement of HEAD, not just committed snapshots.
Shelving In-Progress Modifications
When an urgent hotfix interrupts active development, stash the current workspace:
git stash
This removes all tracked modifications and restores a clean state based on the last commit. Resume the interrupted task by popping the most recently stashed entry:
git stash pop
Other stash management operations:
git stash listenumerates saved stashes.git stash apply stash@{2}restores a specific stash without removing it from the list.git stash drop stash@{1}deletes an individual entry.git stash cleardiscards every stashed change.
Branch-Based Development
Branches isolate lines of development. The default branch is often named main or master. Create supplementary branches for features, experiments, and fixes.
View existing branches with git branch. The active branch is marked with an asterisk.
Create and switch to a new topic branch:
git branch feature-chart
git checkout feature-chart
# shorthand: git checkout -b feature-chart
When committing on a branch, its timeline diverges from the base. Merging brings those changes back. To integrate a bugfix branch into the current branch:
git checkout main
git merge hotfix-login
After successful integrasion, remove the temporary branch:
git branch -d hotfix-login
Resolving Merge Conflicts
If the same regions in files have been altered concurrently, Git marks the conflict. Manual intervention is required. After editing the conflicting sections and removing the conflict markers, finalize the merge:
git add .
git commit -m "chore: resolve merge conflict in dashboard.js"
A Typical Hotfix Workflow
- Create a dedicated branch from the stable base:
git checkout -b fix-payment master - Correct the issue, then stage and commit.
- Switch back to the main branch and merge:
git checkout master && git merge fix-payment - Delete the fix branch:
git branch -d fix-payment - Propagate the patch to other active branches by merging
masterinto them, handling any conflicts that arise.
Collaborating Through a Remote Repository
Remotes are hosted instances—services like GitHub, GitLab, or an on-premise server—that store a copy of the repository. Link a local repository to a remote:
git remote add origin https://github.com/username/repo.git
Push the main branch and its history to the new remote:
git push -u origin master
The -u flag sets the upstream tracking reference so subsequent pushes require only git push. Other branches are published analogously:
git push -u origin feature-chart
To push all local branches simultaneously:
git push --all
Cloning retrieves a entire remote repository:
git clone https://github.com/username/repo.git
Remote URLs and tracking details live in side .git/config and can be inspected or modified directly.