Git concepts (why you shouldn't code without version control)
1. Repository (repo)
think of a repo like a folder that tracks changes. but instead of just dumping files, git actually remembers what you've done (which is more than i can say for you).
example:
git init my-project cd my-project git status
now you have a git repo. congrats, you've done the bare minimum.
2. Commits (saving your screw-ups)
a commit is a snapshot of your code at a certain point in time. every time you commit, git stores what changed so you can undo things when (not if) you mess up.
example:
git add myfile.txt git commit -m "fixed a typo (probably added more)"
this saves your changes with a message explaining what you think you did.
3. Branches
branches let you experiment without destroying the main code. work on a new feature in a branch, and if itโs not completely awful, merge it back.
example:
git branch new-feature git checkout new-feature
now you're in a separate timeline where you can break things in peace.
4. Merging
once your feature is "done" (half-baked but youโre tired of it ๐ซ๐ซ๐ซ), you merge it back into the main branch.
example:
git checkout main git merge new-feature
expect conflicts. lots of them.
5. Remote repositories
Git works locally, but to collaborate, you push code to a remote repo like GitHub, GitLab, or Bitbucket.
example:
git remote add origin https://github.com/your-username/repo.git git push -u origin main
Now your bad code is everyoneโs problem. ๐๐๐
6. Pull requests ("please review my mess")
before merging, you open a pull request (PR) so others can tell you how bad your code is. once they approve, you merge.
example (on GitHub/GitLab):
push your branch:
git push origin new-feature
go to the repo online and click "Create Pull Request."
wait for your team to roast your code in the comments.
7. reverting mistakes (because you will mess up)
undo last commit (but keep changes):
git reset --soft HEAD~1
undo last commit (like it never happened):
git reset --hard HEAD~1
undo a commit properly (so history isnโt destroyed):
git revert <commit-hash>
Summary
git init
โ start a repogit add
โ stage changesgit commit
โ save changesgit branch
โ create/switch branchesgit merge
โ merge changesgit push
โ upload to a remote repogit pull
โ get updatesgit revert
โ undo mistakes