Git Concepts and Workflows
Version Control Systems (VCS) help developers track changes in code, collaborate efficiently, and revert to previous versions if needed. It maintains a history of modifications and enables multiple people to work on a project simultaneously without conflicts.
Benefits of Using Git Over Other VCS like SVN
Distributed System : Unlike SVN, where there's a single central repository, Git allows each developer to have a local copy of the entire project.
Faster Operations : Since Git works locally most of the time, it's much faster than SVN.
Branching and Merging : Git has lightweight branches, making feature development and testing easier.
Offline Work : Developers can commit changes and create branches without needing internet access.
Efficient Storage : Git uses a data structure called a Directed Acyclic Graph (DAG), which optimizes storage and speeds up performance.
Local vs Remote Repositories
Local Repository : A Git repository on a developer’s machine where they can commit changes without affecting others.
Remote Repository : A shared repository (e.g., on GitHub, GitLab, or Bitbucket) that multiple developers can push/pull changes from.
Setting Up Git Repositories
Installing Git on Different OS
Windows : Download from git-scm.com and install.
Mac : Install via Homebrew:
Linux : Install via package manager:
sudo apt-get install git # Ubuntu/Debian
sudo yum install git # CentOS/RHEL
Configuring Git (Username, Email, Editor, Aliases)
git config --global user.name " Your Name"
git config --global user.email " your.email@example.com"
git config --global core.editor " vim"
git config --global alias.co checkout # Creating an alias
Creating and Cloning Repositories
Create a new repository :
Clone an existing repository :
git clone https://github.com/user/repo.git
Working Directory vs Staging Area vs Repository
Working Directory : The current state of files on your machine.
Staging Area (Index) : Changes added using git add but not yet committed.
Repository : Where committed changes are stored.
Commits, Snapshots, and Hashes
Each commit in Git is like a snapshot of the project at a given point. It is assigned a unique SHA-1 hash for tracking.
Reset to previous commit (removes uncommitted changes):
Revert a commit (keeps history) :
Checkout a file from history :
git checkout -- < filename>
Creating, Deleting, and Switching Branches
Create a new branch:
git branch feature-branch
Switch to a branch:
git checkout feature-branch
Delete a branch:
git branch -d feature-branch
Feature Branches, Release Branches, and Hotfix Branches
Feature Branches : Used for developing new features.
Release Branches : Created when preparing for a new release.
Hotfix Branches : Created to fix urgent production issues.
Tracking Remote Branches and Working with Origin
View remote branches:
Set an upstream branch:
git branch --set-upstream-to=origin/main
Merging: Keeping All History with Multiple Merge Commits
Merging keeps the full commit history, which may lead to messy logs.
Rebasing: Linear History by Moving Commits
Rebase applies commits one by one, creating a cleaner history.
When to Use Merge vs Rebase
Use merge for combining completed work into the main branch.
Use rebase when integrating changes into a feature branch before merging.
Using git cherry-pick to Pick Specific Commits
git cherry-pick < commit-hash>
Useful for applying bug fixes from one branch to another without merging everything.
Saving Work Temporarily with git stash
Managing Multiple Stashes and Applying Them Later
List all stashes:
Apply the last stash:
Apply a specific stash:
git stash apply stash@{1}
Understanding Merge Conflicts and Why They Happen
Conflicts occur when multiple people edit the same lines of code.
Git marks conflicts using <<<<<<, =======, and >>>>>>.
Resolving Conflicts Manually and Using git mergetool
git mergetool
git commit -m " Resolved merge conflict"
Corporate Branching Strategies
Git Flow vs GitHub Flow vs Trunk-Based Development
Git Flow : Uses long-lived develop and main branches with features in separate branches.
GitHub Flow : Simpler model using only main and feature branches.
Trunk-Based Development : Developers commit directly to main with feature flags.
Best Practices for Enterprise Git Usage
Use pull requests and code reviews.
Keep commits small and meaningful.
Rebase before merging to keep a clean history.
Real Company Branching Strategies
Feature Branches, Hotfix Branches, and Long-Term Support Branches
Feature branches for ongoing work.
Hotfix branches for urgent fixes.
LTS branches for stable releases that receive only critical updates.
CI/CD Integration with Git Workflows
Automate tests and deployments using Git hooks.
Use GitHub Actions, Jenkins, or GitLab CI/CD for automated pipelines.
Git Workflows in Corporate Projects
Using Git Effectively in Agile and DevOps
Developers push frequently and use pull requests.
Short-lived feature branches keep merging smooth.
Automated testing before merging into main.
Managing Releases and Rollback Strategies
Tag releases for easy rollbacks:
Use feature flags instead of long-lived feature branches.
Command
Description
Example
git --version
Check installed Git version.
git --version
git config --global user.name
Set the global username for Git commits.
git config --global user.name "Your Name"
git config --global user.email
Set the global email for Git commits.
git config --global user.email "you@example.com"
git config --global core.editor
Set the default editor for Git.
git config --global core.editor "vim"
git config --list
Show current Git configuration.
git config --list
Command
Description
Example
git init
Initialize a new Git repository.
git init
git clone
Clone a remote repository to the local machine.
git clone https://github.com/user/repo.git
git remote -v
View remote repositories linked to the local repo.
git remote -v
git remote add origin
Add a remote repository.
git remote add origin https://github.com/user/repo.git
git remote remove origin
Remove a remote repository.
git remote remove origin
3. Staging and Committing Changes
Command
Description
Example
git status
Show the working directory and staging area status.
git status
git add
Add files to the staging area.
git add file.txt
git add .
Stage all changed files.
git add .
git commit -m
Commit changes with a message.
git commit -m "Initial commit"
git commit --amend
Modify the last commit.
git commit --amend -m "Updated commit message"
Command
Description
Example
git branch
List all branches.
git branch
git branch branch-name
Create a new branch.
git branch feature-branch
git checkout branch-name
Switch to another branch.
git checkout feature-branch
git switch branch-name
Alternative to checkout for switching branches.
git switch feature-branch
git checkout -b branch-name
Create and switch to a new branch.
git checkout -b new-branch
git branch -d branch-name
Delete a branch.
git branch -d feature-branch
git branch -D branch-name
Force delete a branch.
git branch -D feature-branch
Command
Description
Example
git merge branch-name
Merge changes from one branch into another.
git merge feature-branch
git merge --abort
Abort a conflicted merge.
git merge --abort
git rebase branch-name
Reapply commits from one branch onto another.
git rebase main
git rebase --abort
Abort an ongoing rebase.
git rebase --abort
Command
Description
Example
git reset HEAD file.txt
Unstage a file while keeping changes.
git reset HEAD file.txt
git reset --hard HEAD~1
Remove the last commit and all changes.
git reset --hard HEAD~1
git revert commit-id
Create a new commit that undoes a previous commit.
git revert <commit-id>
git checkout -- file.txt
Discard uncommitted changes in a file.
git checkout -- file.txt
Command
Description
Example
git stash
Save uncommitted changes temporarily.
git stash
git stash list
Show all saved stashes.
git stash list
git stash pop
Apply the most recent stash and remove it.
git stash pop
git stash apply
Apply the most recent stash but keep it in the stash list.
git stash apply
git stash drop
Remove the most recent stash.
git stash drop
8. Viewing History and Logs
Command
Description
Example
git log
View commit history.
git log
git log --oneline
View commit history in a single line.
git log --oneline
git log --graph
Show a visual representation of the branch history.
git log --graph
git show commit-id
Show details of a specific commit.
git show <commit-id>
git blame file.txt
Show who last modified each line of a file.
git blame file.txt
git reflog
View the history of HEAD changes.
git reflog
Command
Description
Example
git diff
Show differences between files.
git diff
git diff main feature-branch
Compare two branches.
git diff main feature-branch
git diff --staged
Show differences between staged changes and the last commit.
git diff --staged
Command
Description
Example
git tag
List all tags.
git tag
git tag v1.0
Create a new tag.
git tag v1.0
git tag -a v1.0 -m "Version 1.0"
Create an annotated tag with a message.
git tag -a v1.0 -m "Version 1.0"
git tag -d v1.0
Delete a local tag.
git tag -d v1.0
git push origin --tags
Push all local tags to the remote repository.
git push origin --tags
11. Submodules (Nested Repositories)
Command
Description
Example
git submodule add
Add a submodule to a repository.
git submodule add <repo-url>
git submodule update --init
Initialize and update submodules.
git submodule update --init
git submodule deinit
Remove a submodule.
git submodule deinit <submodule-path>
This categorized table covers everything from basic Git usage to advanced workflows like rebasing, cherry-picking, and submodules. 🚀