Git / GitHub
Git is the engine under the hood, and GitHub is the shiny showroom where everyone sees your work. Mastering a handful of core commands will handle 90% of your daily workflow.
Installation
pacman -S gitSSH Key
You will need an SSH key so that your computer and GitHub will have a "secret handshake" that identifies you automatically, without needing any further authentication.
1. Generate the key
The following command will ask "Enter file in which to save the key." Just hit Enter to accept the default location.
ssh-keygen -t ed25519 -C "your_email@example.com"2. Copy the Public Key
cat ~/.ssh/id_ed25519.pub(then manually highlight and copy the text)
3. Add the Key to GitHub
Login to GitHub, and go to Settings. In the left sidebar, click SSH and GPG keys. Then, click the green New SSH key button. Give it a title, paste your key and click Add SSH key.
4. Test the Connection
ssh -T git@github.comIf you see "Hi [YourUsername]! You've successfully authenticated", everything is set and you are ready to go.
Repository Initialization
git clone <URL>: Download an existing GitHub repository to your computer
You can get the required URL pushing the green "Code" button and copying the SSH URL.
git init: Git initialization in current foldergit remote add origin <URL>: Connect the local repository to the GitHub one
Core Workflow
Think of Git as a three-stage process for saving your work. You move files from your folder to a "staging area" before finally sealing them into the permanent history (the repository).
git add <file>: Move changes to the Staging Area. Usegit add .to stage everything instead.git commit -m "Your message": Record the staged changes permanently. Remember, keep messages descriptive (e.g., "Fix login button styling").
However, you would normally like to work on your, personal branch, unless stated otherwise. Branches allow you to work on new features without breaking the "Main" (stable) version of the project.
git checkout -b <branch_name>: Create and switch to a new branch.
In VSCode, you can monitor the branch you're working in by looking at the bottom left of the screen.
git status: A very helpful command you can always run to check files status and the current branch.
Pushing to GitHub
To share your code, you need to link your local folder to the remote repository on GitHub.
git push -u origin main: Send your local commits to GitHub. The-uflag remembers your preferences for next time.git pull: Grab the latest changes from GitHub and merge them into your local files. Use this often to stay up to date with workmates.
WARNING
Instead of doing git pull directly, it is wisely recommended, instead, to git fetch <branch_name> first - to download new commits without touching working files - and then git rebase <branch_name> to move your local, unpushed commits to sit "on top" of the fetched commits from the server.
Pull Requests
A Pull Request (PR) isn't a Git command; it’s a GitHub feature. It’s a formal request to merge your branch into the main project and you can do it by going to the GitHub repository page. It allows for:
- Code Review: Others can comment on specific lines.
- Testing: Automated tools can check if your code breaks anything.
- Discussion: A place to talk about why the changes were made.
