Git Reference Guide

Git is a powerful and flexible version control system that is widely used in software development. This guide provides an overview of common Git commands and their usage.

Table of Contents

  1. Git Basics
    1. Installing Git
      Configuring Git
      Creating a Repository
      Cloning a Repository
      Basic Git Workflow
  2. Branching and Merging
    1. Creating and Switching Branches
      Merging Branches
      Resolving Merge Conflicts
      Rebasing Branches
  3. Remote Repositories
    1. Adding a Remote
      Fetching and Pulling from Remotes
      Pushing to Remotes
      Working with Tags
  4. Advanced Git
    1. Stashing Changes
      Cherry-Picking Commits
      Reverting Commits
      Resetting Commits
  5. Git Tools
    1. Git Log
      Git Diff
      Git Blame
      Git Bisect
  6. Git Tips and Best Practices

Git Basics

Installing Git

To install Git on your system, follow the instructions for your operating system:

  • Windows: Download the installer from the [official Git website](https://git-scm.com/download/win) and follow the installation instructions.
  • macOS: Use Homebrew to install Git with the command: `brew install git`
  • Linux: Use the package manager for your distribution. For example, on Debian-based systems: `sudo apt-get install git`

Configuring Git

After installing Git, configure your user information:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
				  

To verify your configuration, use:

git config --list
				  

Creating a Repository

To create a new Git repository, navigate to your project directory and initialize the repository:

git init
				  

Cloning a Repository

To clone an existing repository, use the `git clone` command followed by the repository URL:

git clone https://github.com/user/repository.git
				  

Basic Git Workflow

  1. Check Status: Check the status of your working directory and staging area.
  2. git status
    				    
  3. Add Changes: Stage changes for commit.
  4. git add <file>          # Stage specific file
    git add .               # Stage all changes
    				    
  5. Commit Changes: Commit your staged changes with a message.
  6. git commit -m "Commit message"
    				    
  7. Push Changes: Push your changes to the remote repository.
  8. git push origin main    # Push to the main branch
    				    

Branching and Merging

Creating and Switching Branches

Create a new branch and switch to it:

git checkout -b new-branch
				  

To switch to an existing branch:

git checkout existing-branch
				  

Merging Branches

To merge changes from one branch into another, first switch to the target branch and then merge:

git checkout main
git merge feature-branch
				  

Resolving Merge Conflicts

If Git encounters conflicts during a merge, it will mark the files with conflicts and stop the merge. Resolve the conflicts manually, then stage and commit the resolved files:

git add <resolved-file>
git commit -m "Resolved merge conflict in <file>"
				  

Rebasing Branches

Rebasing allows you to integrate changes from one branch into another in a linear fashion. To rebase a branch:

git checkout feature-branch
git rebase main
				  

Remote Repositories

Adding a Remote

To add a new remote repository, use:

git remote add origin https://github.com/user/repository.git
				  

Fetching and Pulling from Remotes

To fetch changes from a remote repository without merging:

git fetch origin
				  

To fetch and merge changes from a remote branch:

git pull origin main
				  

Pushing to Remotes

To push changes to a remote repository:

git push origin main
				  

Working with Tags

Create a new tag:

git tag -a v1.0 -m "Version 1.0"
				  

Push tags to the remote repository:

git push origin --tags
				  

Advanced Git

Stashing Changes

To temporarily save changes without committing them, use the stash command:

git stash
				  

To apply stashed changes:

git stash apply
				  

Cherry-Picking Commits

To apply changes from a specific commit to your current branch:

git cherry-pick <commit-hash>
				  

Reverting Commits

To create a new commit that undoes changes from a previous commit:

git revert <commit-hash>
				  

Resetting Commits

To reset your current branch to a previous state:

git reset --hard <commit-hash>  # Discards all changes after the specified commit
git reset --soft <commit-hash>  # Keeps changes in the working directory
				  

Git Tools

Git Log

View the commit history:

git log
				  

View a simplified commit history:

git log --oneline
				  

Git Diff

Compare changes between commits, branches, or the working directory:

git diff                # Show unstaged changes
git diff --staged       # Show staged changes
git diff <branch>       # Compare current branch with another branch
				  

Git Blame

Show what revision and author last modified each line of a file:

git blame <file>
				  

Git Bisect

Use binary search to find the commit that introduced a bug:

git bisect start
git bisect bad          # Mark the current commit as bad
git bisect good <commit-hash>  # Mark an older commit as good
				  

Git Tips and Best Practices

  1. Write meaningful commit messages: Use clear and concise commit messages that describe what changes were made and why.
  2. Commit often: Make small, frequent commits to make it easier to track changes and revert if necessary.
  3. Use branches for features and bug fixes: Keep your main branch clean by using separate branches for different features or bug fixes.
  4. Regularly sync with remote repositories: Pull changes from the remote repository frequently to stay up-to-date with the latest changes.
  5. Review changes before committing: Use `git diff` to review changes before adding and committing them.
  6. Avoid committing sensitive information: Make sure not to commit passwords, API keys, or other sensitive information.

This guide covers the essential commands and concepts for using Git effectively. For more detailed information, refer to the official Git documentation