What is Git?
Git is a version control system that lets you track changes in your code over time. It's like "Ctrl+Z" on steroids - you can always go back to previous versions. GitHub, GitLab, and Bitbucket use Git to store and share code.
Learn Git - the tool that lets you track changes and collaborate with others
Git is a version control system that lets you track changes in your code over time. It's like "Ctrl+Z" on steroids - you can always go back to previous versions. GitHub, GitLab, and Bitbucket use Git to store and share code.
git initStarts a new Git repository in the current folder. This is the first thing you do when you want to start using Git in a project.
$ cd my-project
$ git init
Initialized empty Git repository in /Users/name/my-project/.git/
git statusShows you the status of your repository - which files have been modified, which are staged, and which branch you're on.
$ git status
On branch main
Changes not staged for commit:
modified: index.html
modified: style.css
Untracked files:
script.js
git status often! It's the command you'll use the most.
git addAdds files to the "staging area" - making them ready to be committed. You must always "add" before you "commit".
$ git add index.html
# Adds one specific file
$ git add .
# Adds ALL changed files (most commonly used)
$ git add *.css
# Adds all CSS files
git add . - Add all files in current directorygit add -A - Add all files in entire projectgit add -p - Interactive mode (choose what to add)git commitSaves your staged changes with a descriptive message. This is a "checkpoint" in your project's history.
$ git commit -m "Add contact form"
# -m lets you write the message directly
$ git commit -m "Fix: Correct navigation bug"
# Good practice: start with what type of change it is
$ git commit -am "Update README"
# -a adds and commits in one operation
git pushUploads your local commits to a remote repository (like GitHub). This shares your code with others.
$ git push origin main
# Pushes main branch to GitHub
$ git push -u origin new-feature
# Pushes and sets up tracking for a new branch
$ git push
# If tracking is already set up
git pullFetches and merges changes from the remote repository to your local copy. Important when working in teams.
$ git pull origin main
# Fetches latest changes from GitHub
$ git pull
# If tracking is set up
git pull before you start working, especially in team projects!
git cloneDownloads a complete copy of a repository from GitHub to your machine.
$ git clone https://github.com/user/repo.git
# Downloads repository from GitHub
$ git clone https://github.com/user/repo.git my-project
# Downloads to a specific folder
$ git status # 1. See what has changed
$ git add . # 2. Add all changes
$ git commit -m "Description" # 3. Commit with message
$ git push origin main # 4. Push to GitHub
git diff - See what has changed since last commitgit log --oneline - View commit historygit branch - List all branchesgit checkout -b new-branch - Create and switch to new branchgit stash - Temporarily store changesgit remote -v - See which GitHub repo you're connected to