Table of Contents
Github
GitHub is a cloud-based platform where you can store, share, and work together with others to write code.
Storing your code in a “repository” on GitHub allows you to:
- Showcase or share your work.
- Track and manage changes to your code over time.
- Let others review your code, and make suggestions to improve it.
- Collaborate on a shared project, without worrying that your changes will impact the work of your collaborators before you’re ready to integrate them.
Collaborative working, one of GitHub’s fundamental features, is made possible by the open-source software, Git, upon which GitHub is built.
understandable comparison example: Microsoft’s Word’s.
About Git
Git is a version control system that intelligently tracks changes in files. Git is particularly useful when you and a group of people are all making changes to the same files at the same time.
Typically, to do this in a Git-based workflow, you would:
- Create a branch off from the main copy of files that you (and your collaborators) are working on.
- Make edits to the files independently and safely on your own personal branch.
- Let Git intelligently merge your specific changes back into the main copy of files, so that your changes don’t impact other people’s updates.
- Let Git keep track of your and other people’s changes, so you all stay working on the most up-to-date version of the project.
understandable comparison example: Google Doc’s.
How do Git and GitHub Work Together?
When you upload files to GitHub, you’ll store them in a “Git repository.” This means that when you make changes (or “commits”) to your files in GitHub, Git will automatically start to track and manage your changes.
There are plenty of Git-related actions that you can complete on GitHub directly in your browser, such as creating a Git repository, creating branches, and uploading and editing files.
However, most people work on their files locally (on their own computer), then continually sync these local changes—and all the related Git data—with the central “remote” repository on GitHub. There are plenty of tools that you can use to do this, such as GitHub Desktop.
Once you start to collaborate with others and all need to work on the same repository at the same time, you’ll continually:
- Pull all the latest changes made by your collaborators from the remote repository on GitHub.
- Push back your own changes to the same remote repository on GitHub.
Git figures out how to intelligently merge this flow of changes, and GitHub helps you manage the flow through features such as “pull requests.
Git Concepts
What is Git? Git is a distributed version control system that tracks changes in files and coordinates work among multiple people. It maintains a complete history of your project and allows you to revert to previous versions.
Core Concepts:
- Repository (Repo): A directory containing your project files and Git’s tracking information
- Working Directory: The current state of files you’re editing
- Staging Area: A temporary area where changes are prepared before committing
- Commit: A snapshot of your project at a specific point in time
- Branch: An independent line of development
- HEAD: A pointer to the current branch/commit you’re working on
Essential Commands:
- `git init`: Initialize a new Git repository- `git add <file>`: Add files to staging area- `git commit -m "message"`: Save changes with a descriptive message- `git status`: Check current state of files- `git log`: View commit history- `git diff`: Show changes between versionsIntermediate Git Concepts
Branching and Merging:
- `git branch <name>`: Create a new branch- `git checkout <branch>`: Switch to a branch- `git merge <branch>`: Merge changes from another branch- `git branch -d <branch>`: Delete a branchRemote Repositories:
- `git remote add origin <url>`: Connect to a remote repository- `git push origin <branch>`: Upload changes to remote- `git pull origin <branch>`: Download and merge changes from remote- `git clone <url>`: Copy a remote repository locallyUndoing Changes:
- `git reset <file>`: Unstage a file- `git reset --hard <commit>`: Reset to a specific commit- `git revert <commit>`: Create a new commit that undoes changesAdvanced Git Concepts
Rebasing
Git rebase is a powerful command that integrates changes from one branch into another by moving or combining a sequence of commits. It rewrites the commit history to create a cleaner, more linear history, especially useful when working with feature branches. Unlike git merge, which creates a merge commit, git rebase applies changes directly to the target branch, resulting in a linear history that is easier to understand and debug.
git rebase <branch>: Reapply commits on top of another branch- Interactive rebasing (
git rebase -i): Modify, combine, or reorder commits - Difference between merge and rebase: Merge preserves history, rebase creates a linear history
Stashing
Stashing is like having a temporary clipboard for your uncommitted changes. When you’re in the middle of working on something but need to quickly switch contexts, stashing saves your current work without creating a commit.
- `git stash`: Temporarily save uncommitted changes- `git stash pop`: Restore stashed changes- `git stash list`: View all stashesAdvanced Branching
Cherry-picking allows you to select specific commits from one branch and apply them to another. This is useful when you need a particular fix or feature from a development branch but don’t want to merge the entire branch.
Git bisect uses a binary search algorithm to help you find the commit that introduced a bug. You tell Git about a “good” commit (where the bug didn’t exist) and a “bad” commit (where the bug exists), and Git automatically checks out commits in between for you to test.
Git blame is a command that shows you who last modified each line of a file, when they made the change, and which commit introduced that change. It’s called “blame” because it helps you identify who is “responsible” for each line of code, though it’s used more for understanding code history than assigning fault.
- `git cherry-pick <commit>`: Apply specific commits to current branch- `git bisect`: Binary search to find bug-introducing commits- `git blame`: Show who modified each line of a file