Git is a Graph: Understanding the Logic
Git is not just a list of commands; Git is a graph of commits. If you understand the logic behind it, everything becomes simple. Git is a graph of commits.
Starting a Project
There are two easy ways to do this:
1.Create a project folder from scratch:
git init
This creates a new Git repository locally. Use this when starting a project from your desktop.
2.Clone an existing repository:
If you want to download a ready-made repository and don't have a local folder yet, you can clone from GitHub.
cd Documents
git clone https://github.com/user/project.git
This connects your local project to the GitHub remote server automatically and creates a pointer called origin. (We will talk more about origin later).
What about Forking?
Forking is a GitHub feature that copies someone else's repository to your own account. If you don’t have access to a repo but want to contribute, you fork it, then clone your fork locally. After writing code, you create a Pull Request (PR)—essentially saying, "Hey, I improved the code, what do you think?"
Daily Workflow & Teamwork
When you work with a team, you want to save your progress and undo changes if things go bad. We use branches to keep work separated:
main *
└── dev
└── feature/task1 (You)
└── feature/task2 (Your teammate)
Working on feature/task1 means your changes won't affect your teammate’s code. Once you test your new features locally, you merge them into a shared codebase (like dev or main). You open a PR from your branch to dev, your teammates review it, and if it’s okay, it gets merged.
The Daily Commands:
git branch (See all local branches)
git checkout dev (Switch to the dev branch)
git checkout -b feature/task (The -b stands for a new branch)
git add . (Track your file changes)
git commit -m "first commit" (Create a checkpoint)
git push -u origin dev
-u stands for upstream. it creates a connection from your local branch to the remote branch. After this, you can just write git push without typing the rest.
The Three Areas
1.Working Directory: Your project as it sits on your laptop (e.g., a new file.txt you just created).
2.Staging Area: When you run git add ., Git starts "watching" those specific changes.
3.Repository: After you git commit, the changes are saved locally. Finally, git push sends them to the GitHub repository.
Advanced Survival Commands
git pull: This is fetch + merge. it brings all changes from your friends and updates your code.
git fetch: Safely brings remote branches for you to look at without merging them into your work.
git stash: If you need to change branches but aren't ready to commit, git stash hides your changes. Use git stash pop to bring them back when you return.
Merge vs. Rebase
Merge: Safe, but the history isn't linear because each merge is a new commit.
Rebase: Rewrites the commit history to make it linear. Be careful with a team; it can be confusing as it changes history!
Don't Fear Conflicts
Conflicts occur when two people edit the same line. Your editor will open and show you:
<<<<<<< HEAD
def calculate_total(price, tax):
return price + (price * tax)
=======
def calculate_total(price, tax_rate):
return price * (1 + tax_rate)
>>>>>>> feature-optimization
<<<<< HEAD is your local version; >>>>> feature-optimization is the incoming change. You can accept "current," "incoming," or "both."
Check Status: git status
Stage the Fix: git add <filename>
Complete: git commit (if merging) or git rebase --continue. (You can always use git rebase --abort to stop without breaking anything).
Security & Undoing Mistakes
The .env trick: If you forgot to add a .env file to your .gitignore and accidentally committed it, you can stop tracking it without deleting it from your computer using:
git rm --cached <file>
Going Back:
git checkout <commit-hash>: Moves your location. If you see "Detached HEAD," it just means your pointer isn't on the latest branch tip.
git reset --soft HEAD~1: Undoes the last commit but keeps changes in the staging area.
git reset --mixed: (Default) Undoes the commit and the stash (cache); code stays in your working directory.
git reset --hard: Dangerous. Reverts your directory to look exactly like an old commit; everything else is deleted.
The Secret Weapon: If you make a mistake with --hard, use git reflog. It’s like a recycle bin for your commits.
git revert: Creates a new commit that does the opposite of a previous commit to "undo" it safely.
"Finally figuring out that git commands are strangely named graph manipulation commands—creating/deleting nodes, moving pointers around." — Kent Beck


Top comments (0)