Skip to main content

Git


1. What is Git?

Git is a distributed version control system.
It tracks changes in your code and helps you collaborate with others.


3. Basic Git Terms (for Beginners)

TermMeaning
Repository (repo)The project folder tracked by Git. Can be local (your computer) or remote (GitHub).
CommitA snapshot of your changes, with a message describing what changed.
BranchA separate line of development for features or fixes.
MergeCombining changes from one branch into another.
CloneCopying a remote repository onto your computer.
PushUploading your commits from local to remote (e.g. GitHub).
PullDownloading new commits from remote to local.
ForkCreating your own copy of someone else’s repo (on GitHub).
Pull Request (PR)Asking to merge your changes into another repo; often used for code review.
Staging AreaWhere files go after git add—ready to be committed.
.gitignoreFile listing things Git should NOT track (like temp files).
ConflictWhen Git can’t automatically combine changes—needs manual fixing.
RemoteA version of the repo hosted on a server (like GitHub).
OriginThe default name for your main remote repo.
TagA label for specific commits, often used for releases (like v1.0).
DiffShows differences between files, commits, or branches.
StashTemporarily saves changes you’re not ready to commit.
Checkout/SwitchChange which branch or commit you’re working on.

3. Basic Workflow

# Set your name and email (first time only)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Create a new repo
git init

# Clone an existing repo
git clone https://github.com/user/repo.git

# See repo status (changed files)
git status

# Add files to staging
git add filename.txt # Add one file
git add . # Add all files

# Commit changes
git commit -m "Your message"

# See commit history
git log

# Push changes to remote (GitHub)
git push origin main # 'main' is the branch name

# Pull changes from remote
git pull origin main

# Check which branch you're on
git branch

# Create a new branch
git branch new-feature

# Switch to a branch
git checkout new-feature
# or (modern Git)
git switch new-feature

# Merge a branch into current
git merge new-feature

# Delete a branch
git branch -d new-feature

4. Remote Repositories

  • Add a remote:
    git remote add origin https://github.com/user/repo.git
  • List remotes:
    git remote -v
  • Change remote URL:
    git remote set-url origin NEW_URL

5. Undoing Changes

TaskCommandExplanation
Unstage filegit reset HEAD filename.txtTake out of staging
Discard changesgit checkout -- filename.txtUndo edits
Amend last commitgit commit --amendEdit last commit
Revert a commitgit revert <commit>Make undoing commit

6. Stashing (Temporary Changes)

git stash           # Save changes for later
git stash pop # Reapply stashed changes
git stash list # Show stashes

7. Tagging

git tag v1.0        # Create tag
git tag # List tags
git push origin v1.0 # Push tag

8. Viewing Differences

git diff            # See unstaged changes
git diff --staged # See staged changes
git diff branchA..branchB # Compare branches

9. Ignoring Files

  • Create a .gitignore file:
    node_modules/
    *.log
    .env

10. Useful Shortcuts

ShortcutCommandDescription
Show last commitgit showCommit details
See short loggit log --onelineCompact history
See branches graphgit log --graph --allVisual history
List files trackedgit ls-filesTracked files

11. Help

git help <command>
# Example:
git help log

12. Common GitHub Tasks

  • Fork: Copy someone’s repo to your account.
  • Pull Request: Propose your changes to a project.
  • Clone: Download a repo to your computer.
  • Push: Upload your changes to GitHub.

13. Tips

  • Commit often with meaningful messages.
  • Pull before you push, to avoid conflicts.
  • Use branches for new features/fixes.

14. Resources


Happy coding and collaborating! 🚀