Skip to content

Latest commit

 

History

History
319 lines (271 loc) · 5.84 KB

File metadata and controls

319 lines (271 loc) · 5.84 KB

Quick git Commands and Info

git is a distributed version control system for tracking changes in source code during software development.


Installing git

For Windows

  1. Download the installer from the official Git website.
  2. Run the installer and follow the setup instructions.
  3. Open a new terminal and verify the installation:
    git --version

For macOS

  1. Install via Homebrew:
    brew install git
  2. Or download from the official Git website.
  3. Verify the installation:
    git --version

For Linux

  1. Install via package manager:
    # Ubuntu/Debian
    sudo apt update && sudo apt install git
    
    # CentOS/RHEL/Fedora
    sudo yum install git
    # or
    sudo dnf install git
  2. Verify the installation:
    git --version

Initial Setup

Configure Git

  • Set your name:
    git config --global user.name "Your Name"
  • Set your email:
    git config --global user.email "your.email@example.com"
  • Check current configuration:
    git config --list

Repository Management

Creating & Cloning

  • Initialize a new repository:
    git init
  • Clone an existing repository:
    git clone <repository-url>
  • Clone to a specific folder:
    git clone <repository-url> <folder-name>

Repository Status

  • Check repository status:
    git status
  • View commit history:
    git log
  • View compact log:
    git log --oneline

Working with Changes

Staging & Committing

  • Add files to staging area:
    git add <file-name>
  • Add all files:
    git add .
  • Commit staged changes:
    git commit -m "Your commit message"
  • Add and commit in one step:
    git commit -am "Your commit message"

Viewing Changes

  • See differences in working directory:
    git diff
  • See differences in staged files:
    git diff --staged
  • Show changes in specific commit:
    git show <commit-hash>

Branch Management

Basic Branch Operations

  • List all branches:
    git branch
  • Create a new branch:
    git branch <branch-name>
  • Switch to a branch:
    git checkout <branch-name>
  • Create and switch to new branch:
    git checkout -b <branch-name>
  • Delete a branch:
    git branch -d <branch-name>

Modern Branch Commands (Git 2.23+)

  • Switch to a branch:
    git switch <branch-name>
  • Create and switch to new branch:
    git switch -c <branch-name>

Remote Repositories

Remote Management

  • List remote repositories:
    git remote -v
  • Add a remote:
    git remote add <name> <url>
  • Remove a remote:
    git remote remove <name>

Synchronizing

  • Fetch changes from remote:
    git fetch
  • Pull changes from remote:
    git pull
  • Push changes to remote:
    git push
  • Push new branch to remote:
    git push -u origin <branch-name>

Merging & Rebasing

Merging

  • Merge a branch:
    git merge <branch-name>
  • Merge with no fast-forward:
    git merge --no-ff <branch-name>

Rebasing

  • Rebase current branch:
    git rebase <branch-name>
  • Interactive rebase:
    git rebase -i <commit-hash>

Undoing Changes

Unstaging & Reverting

  • Unstage a file:
    git reset <file-name>
  • Discard changes in working directory:
    git checkout -- <file-name>
  • Reset to previous commit (keep changes):
    git reset --soft HEAD~1
  • Reset to previous commit (discard changes):
    git reset --hard HEAD~1

Reverting Commits

  • Revert a commit:
    git revert <commit-hash>
  • Revert multiple commits:
    git revert <commit-hash1>..<commit-hash2>

Useful Shortcuts & Tips

Aliases

  • Create useful aliases:
    git config --global alias.st status
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.cm commit
    git config --global alias.lg "log --oneline --graph --all"

Stashing

  • Stash current changes:
    git stash
  • Apply stashed changes:
    git stash pop
  • List stashes:
    git stash list

Example Workflow

# Clone a repository
git clone https://github.com/username/repo.git
cd repo

# Create and switch to feature branch
git checkout -b feature/new-feature

# Make changes and commit
git add .
git commit -m "Add new feature"

# Push to remote
git push -u origin feature/new-feature

# Switch back to main and merge
git checkout main
git pull
git merge feature/new-feature
git push

Official Documentation

For more details, visit the official Git documentation: