What is Git?#
A version control system that helps track changes in code. Two main benefits:
- Track full history of your project
- Collaborate on large projects with others
Initial Configuration#
git config --global user.name "your-username"
git config --global user.email "your-email"
git config --list # verify configurationTwo types of configuration: local (one repo) and global (all repos). set global config so you dont have authenicate everytime.
Core Commands#
git clone "repo-link" # copy a remote repo locally
git status # check current state of files
git add <filename> # stage a specific file
git add . # stage all files
git commit -m "message" # save staged changes
git push -u origin main # upload changes to remote repoDifferent File Status Types#
| Status | Meaning |
|---|---|
| Untracked | New file Git doesn’t know about |
| Modified | File has been changed |
| Staged | Ready to be committed |
| Unmodified | No changes |
Starting From Scratch (No Remote Repo)#
git init # make folder a git repo
git remote add origin "repo-link" # connect to remote repo
git remote -v # verify remote connection
git branch -M main # rename branch to main
git push -u origin main # first pushBranching#
git branch # check current branch
git branch -M main # rename branch
git checkout <branch> # switch to branch
git checkout -b <new-branch> # create and switch to new branch
git branch -d <branch> # delete a branch
git push origin <branch> # push branch to remotePull Requests#
A PR lets you tell others about changes you’ve pushed to a branch. Then someone like senior manager or admin reviews your changes before merging into main. GitHub automatically checks for conflicts between branches and tell you if there is any conflicts. Conflict: It occurs when there is different content on same file in different branches.
Undoing Changes#
# Case 1 — Unstage a file
git reset <filename>
# Case 2 — Undo last commit (keep changes)
git reset HEAD~1
# Case 3 — Undo multiple commits
git reset <commit-hash> # keep changes
git reset --hard <commit-hash> # discard changes permanentlyForking#
A fork is a copy of someone else’s repo on your own GitHub account. You make changes, add features, fix bugs — then submit a Pull Request to contribute back to the original project.
Quick Reference Card#
| Command | What it does |
|---|---|
git status | Check file states |
git add . | Stage everything |
git commit -m "" | Save changes |
git push | Upload to remote |
git pull | Download from remote |
git log | View commit history |
git diff | See unstaged changes |
