Git Developer Guide
Section titled “Git Developer Guide”Git is a distributed version control system that tracks changes in your codebase, enabling collaboration and version management for software development projects.1
Getting Started
Section titled “Getting Started”Installation and Configuration
Section titled “Installation and Configuration”Install Git from git-scm.com and verify the installation from your command line. Before starting any work, configure your identity with your name and email address:3
git config --global user.name "Your Name Comes Here"git config --global user.email you@yourdomain.example.comgit config --list # View all configurationgit config user.name # View specific config valueYou can access documentation for any Git command using man git-log or git help log.4
Repository Initialization
Section titled “Repository Initialization”Creating a New Repository
Section titled “Creating a New Repository”Initialize a new Git repository in your project directory:5
git init my-repo # Initialize with directory namegit init # Initialize in current directorygit init --bare my-repo.git # Create bare repository (for servers)cd my-repoThis creates a .git directory that stores all version control information.4
Cloning an Existing Repository
Section titled “Cloning an Existing Repository”To work with an existing project, clone the repository:1
git clone <repository-url> # Clone to default directorygit clone <repository-url> my-folder # Clone to specific directorygit clone --depth 1 <repository-url> # Shallow clone (latest commit only)git clone --branch develop <repository-url> # Clone specific branchgit clone --single-branch <repository-url> # Clone only one branchBasic Workflow
Section titled “Basic Workflow”Tracking Changes
Section titled “Tracking Changes”Stage files to prepare them for commit:1
git add file1.md file2.md # Stage specific filesgit add . # Stage all changes in current directorygit add -A # Stage all changes (entire repo)git add -u # Stage modified and deleted files onlygit add -p # Interactive staging (patch mode)git add *.py # Stage files by patternThe staging area (index) temporarily stores snapshots of your changes before committing them permanently.4
Committing Changes
Section titled “Committing Changes”Create a commit with a descriptive message:3
git commit -m "Your message about the commit"git commit -am "Message" # Add and commit tracked filesgit commit --amend # Modify the last commitgit commit --amend -m "New message" # Change last commit messagegit commit --amend --no-edit # Add changes to last commit, keep messagegit commit -v # Show diff in commit message editorgit commit --allow-empty -m "Message" # Create empty commitWrite meaningful commit messages that describe what the commit contains—new features, bug fixes, or specific changes. Commits are permanent snapshots stored in the repository.3
Checking Status
Section titled “Checking Status”View the current state of your working directory and staging area:1
git status # Full statusgit status -s # Short statusgit status -sb # Short status with branch infogit status --ignored # Show ignored files tooBranching and Merging
Section titled “Branching and Merging”Creating and Switching Branches
Section titled “Creating and Switching Branches”Branches allow you to develop features independently without affecting the main codebase:3
git branch experimental # Create a new branchgit branch feature/new-ui # Create branch with naming conventiongit branch -a # List all branches (local and remote)git branch -r # List remote branchesgit branch -v # List branches with last commitgit branch -d my-branch # Delete merged branchgit branch -D my-branch # Force delete branchgit branch -m old-name new-name # Rename branch
git switch experimental # Switch to the branchgit switch -c new-feature # Create and switch to new branchgit checkout my-branch # Alternative command to switch branchesgit checkout -b new-feature # Create and switch (alternative)git checkout - # Switch to previous branchThe master (or main) branch is created by default when you initialize a repository.4
Working with Branches
Section titled “Working with Branches”Make changes on your branch, commit them, and switch back to the main branch:4
git switch experimental# Make changes and commitgit switch masterMerging Changes
Section titled “Merging Changes”Integrate changes from one branch into another:4
git merge experimental # Merge branch into current branchgit merge --no-ff feature-branch # Create merge commit even if fast-forwardgit merge --squash feature-branch # Squash all commits into onegit merge --abort # Abort merge in case of conflictsgit merge origin/main # Merge remote branchCollaboration Workflows
Section titled “Collaboration Workflows”Pushing to Remote Repositories
Section titled “Pushing to Remote Repositories”Share your changes with others by pushing to a remote repository:1
git push --set-upstream origin my-branch # Push and set upstream trackinggit push origin my-branch # Push to specific remote and branchgit push # Push current branch to tracked remotegit push --all # Push all branchesgit push --tags # Push all tagsgit push origin :old-branch # Delete remote branch (old syntax)git push origin --delete old-branch # Delete remote branch (new syntax)git push --force # Force push (use with caution)git push --force-with-lease # Safer force pushPulling Changes
Section titled “Pulling Changes”Download and integrate changes from remote repository:1
git pull # Fetch and merge from tracked remotegit pull origin main # Pull from specific remote and branchgit pull --rebase # Fetch and rebase instead of mergegit pull --no-rebase # Explicitly use merge strategygit pull --all # Fetch from all remotesFetching Changes
Section titled “Fetching Changes”Download changes without merging:1
git fetch # Fetch from default remotegit fetch origin # Fetch from specific remotegit fetch --all # Fetch from all remotesgit fetch --prune # Remove deleted remote branchesgit fetch origin main # Fetch specific branchRemote Repository Management
Section titled “Remote Repository Management”Add and manage remote repositories:5
git remote add origin <repository-url> # Add remote repositorygit remote -v # List remotes with URLsgit remote show origin # Show detailed remote infogit remote rename origin upstream # Rename remotegit remote remove upstream # Remove remotegit remote set-url origin <new-url> # Change remote URLPull Requests
Section titled “Pull Requests”The standard GitHub collaboration workflow involves creating a branch, committing changes, opening a pull request to propose changes, and merging after review.3
Collaborative Development Models
Section titled “Collaborative Development Models”Two primary collaboration approaches exist on platforms like GitHub:1
- Shared repository: Team members have explicit read, write, or administrator access with protected branches
- Fork and pull: Contributors fork the repository, make changes in their copy, and submit pull requests
Inspecting History
Section titled “Inspecting History”Viewing Commits
Section titled “Viewing Commits”Reference commits using various identifiers:4
git show c82a22c39c # Using commit hash (first few characters)git show HEAD # Current branch tipgit show HEAD~1 # Previous commitgit show HEAD~3 # Three commits backgit show experimental # Branch tipgit show v1.0.0 # Specific tag
git log # View commit historygit log --oneline # Compact viewgit log --graph # Show branch graphgit log --all --decorate --oneline --graph # Detailed graph viewgit log -n 5 # Show last 5 commitsgit log --since="2 weeks ago" # Commits from time periodgit log --author="John" # Commits by authorgit log --grep="fix" # Search commit messagesgit log file.txt # History of specific filegit log -p # Show patches (diffs)git log --stat # Show statisticsComparing Changes
Section titled “Comparing Changes”Compare different versions of your code:4
git diff # Unstaged changesgit diff --staged # Staged changesgit diff --cached # Same as --stagedgit diff HEAD # All changes since last commitgit diff v2.5 HEAD # Compare current HEAD to version taggit diff branch1..branch2 # Compare two branchesgit diff branch1...branch2 # Compare from common ancestorgit diff --name-only # Show only file namesgit diff --stat # Show statisticsgit diff commit1 commit2 file.txt # Compare file between commitsCreating Tags
Section titled “Creating Tags”Tag important versions for easy reference:4
git tag v1.0.0 # Create lightweight taggit tag -a v1.0.0 -m "Version 1.0" # Create annotated taggit tag -a v1.0.0 commit-hash # Tag specific commitgit tag # List all tagsgit tag -l "v1.*" # List tags matching patterngit show v1.0.0 # Show tag detailsgit tag -d v1.0.0 # Delete local taggit push origin v1.0.0 # Push specific taggit push origin --tags # Push all tagsgit push origin :refs/tags/v1.0.0 # Delete remote tag
git branch stable v2.5 # Create branch from tagRecovering from Mistakes
Section titled “Recovering from Mistakes”Git provides commands to undo changes and recover from errors:2
git reset --hard HEAD^ # Reset branch and working directory to previous commitgit reset --soft HEAD^ # Undo commit, keep changes stagedgit reset --mixed HEAD^ # Undo commit and unstage (default)git reset HEAD file.txt # Unstage specific filegit reset --hard origin/main # Reset to match remote
git revert commit-hash # Create new commit that undoes changesgit revert HEAD # Revert last commitgit revert --no-commit HEAD~3..HEAD # Revert multiple commits
git restore file.txt # Discard changes in working directorygit restore --staged file.txt # Unstage filegit restore --source=HEAD~1 file.txt # Restore from specific commit
git checkout -- file.txt # Discard changes (older syntax)git checkout commit-hash file.txt # Restore file from commit
git clean -n # Preview untracked files to removegit clean -f # Remove untracked filesgit clean -fd # Remove untracked files and directoriesgit clean -fdx # Remove untracked and ignored filesAdvanced Operations
Section titled “Advanced Operations”Stashing Changes
Section titled “Stashing Changes”Temporarily save uncommitted changes:2
git stash # Stash current changesgit stash save "Work in progress" # Stash with messagegit stash -u # Include untracked filesgit stash list # List all stashesgit stash show # Show latest stashgit stash show stash@{1} # Show specific stashgit stash pop # Apply and remove latest stashgit stash apply # Apply without removinggit stash apply stash@{1} # Apply specific stashgit stash drop # Delete latest stashgit stash drop stash@{1} # Delete specific stashgit stash clear # Delete all stashesgit stash branch new-branch # Create branch from stashRebasing
Section titled “Rebasing”Rewrite commit history by replaying commits:4
git rebase main # Rebase current branch onto maingit rebase -i HEAD~3 # Interactive rebase last 3 commitsgit rebase --continue # Continue after resolving conflictsgit rebase --abort # Abort rebasegit rebase --skip # Skip current commit during rebasegit rebase origin/main # Rebase onto remote branchCherry-picking
Section titled “Cherry-picking”Apply specific commits from one branch to another:4
git cherry-pick commit-hash # Apply specific commitgit cherry-pick commit1 commit2 # Apply multiple commitsgit cherry-pick --continue # Continue after resolving conflictsgit cherry-pick --abort # Abort cherry-pickWorking with Submodules
Section titled “Working with Submodules”Manage repositories within repositories:
git submodule add <repo-url> path/to/sub # Add submodulegit submodule init # Initialize submodulesgit submodule update # Update submodulesgit submodule update --init --recursive # Initialize and update recursivelygit clone --recurse-submodules <repo-url> # Clone with submodulesSearching and Finding
Section titled “Searching and Finding”Search through repository content:
git grep "search term" # Search in working directorygit grep "search term" v1.0.0 # Search in specific versiongit blame file.txt # Show who changed each linegit blame -L 10,20 file.txt # Blame specific linesgit bisect start # Start binary search for buggit bisect bad # Mark current commit as badgit bisect good v1.0.0 # Mark commit as goodgit bisect reset # End bisect session