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
- Git Basics
- Branching and Merging
- Remote Repositories
- Advanced Git
- Git Tools
- 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
- Check Status: Check the status of your working directory and staging area.
- Add Changes: Stage changes for commit.
- Commit Changes: Commit your staged changes with a message.
- Push Changes: Push your changes to the remote repository.
git status
git add <file> # Stage specific file git add . # Stage all changes
git commit -m "Commit message"
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
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
- Write meaningful commit messages: Use clear and concise commit messages that describe what changes were made and why.
- Commit often: Make small, frequent commits to make it easier to track changes and revert if necessary.
- Use branches for features and bug fixes: Keep your main branch clean by using separate branches for different features or bug fixes.
- Regularly sync with remote repositories: Pull changes from the remote repository frequently to stay up-to-date with the latest changes.
- Review changes before committing: Use `git diff` to review changes before adding and committing them.
- 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