Git
My Favorite Git Commands
Section titled “My Favorite Git Commands”Setting Up SSH On MacOS/Linux
Section titled “Setting Up SSH On MacOS/Linux”-
Check if you already have an SSH key with
Terminal window ls -al ~/.ssh -
If you don’t, create a new key using
Terminal window ssh-keygen -t ed25519 -C ameliaq@email.com -
Start the SSH agent using
Terminal window eval "$(ssh-agent -s)" -
Add your new key using
Terminal window ssh-add ~/.ssh/id_ed25519 -
Copy your new key with
Terminal window pbcopy < ~/.ssh/id_ed25519.pubor by copying the output of
Terminal window cat ~/.ssh/id_ed25519.puband paste it into GitHub’s Add new SSH Key box
-
Test your new SSH key with
Terminal window ssh -T git@github.comFix complaints using
Terminal window chmod 700 ~/.sshchmod 600 ~/.ssh/id_ed25519chmod 644 ~/.ssh/id_ed25519.pub
Setting Up SSH On Windows
Section titled “Setting Up SSH On Windows”-
Check if you already have an SSH key with
Terminal window ls ~/.ssh -
If you don’t, create a new key using
Terminal window ssh-keygen.exe -t ed25519 -C ameliaq@email.com -
Start the SSH agent using
Terminal window Start-Service ssh-agent -
Add your new key using
Terminal window ssh-add.exe ~\.ssh\id_ed25519and by copying the output of
Terminal window cat ~\.ssh\id_ed25519.puband paste it into GitHub’s Add new SSH Key box
-
Test your new SSH key with
Terminal window ssh -T git@github.com
Basics
Section titled “Basics”Committing
Section titled “Committing”| Command | Meaning |
|---|---|
git pull | Pull |
git status | Check Status |
git add -A | Stage all changes |
git add file_path | Stage changes (specific) |
git commit -m "Message" | Commit |
git push | Push |
git add --patch | Stage in chunks |
git add -p | Stage in chunks |
Branching
Section titled “Branching”Checkout to a new branch
git checkout -b yourname/new_branch_namethen
git push --set-upstream origin new_branch_nameCheckout to an existing branch
git checkout yourname/branch_nameCheck your current branch
git branch -aDelete a branch
git branch -d branch_nameForce delete a branch (caution)
git branch -D branch_nameAfter a pull request to main:
git checkout maingit pull --prunegit checkout yourname/new_branch_namegit push --set-upstream origin new_branch_nameRebasing
Section titled “Rebasing”When rebasing, git essentially asks “What would have happened if everything that had happened during name_of_branch_to_rebase_onto had happened before whatever had happened before yourname/working_branch?” Because the past may have changes to code or assets that the present also interacts with, there may be conflicts. These are resolved during the rebase.
-
Terminal window git checkout yourname/working_branchgit fetch origingit rebase name_of_branch_to_rebase_onto -
If there are no conflicts, excellent.
-
If there are, modern IDEs try their best to help resolve them. After rebasing, the branch in the origin has to be updated. It seems as simple as
git add .,git commit -m "Rebase message", and thengit push. -
Calling
git pushwill most likely lead to Git saying that there’s a mismatch with the origin and thatgit pull --rebasemust be called, but calling it will just repeat the process above a second time. This is becasue the origin is out of sync with the current, (assumedly) accurate rebased branch. Instead of callinggit pull --rebase, it’s better to either check out to a new branch and push the changes there, or callgit push -fto force push the changes. The latter is riskier, and force pushes should only ever be used with confidence.
Rebasing a Downstream Repo’s main Onto Its Upstream’s main
Section titled “Rebasing a Downstream Repo’s main Onto Its Upstream’s main”-
First, make sure that both the upstream and the downstream are clean
-
Checkout to
mainusinggit checkout main -
Fetch the upstream’s content
git fetch upstream -
Merge with the upstream’s content
git merge upstream/main -
Settle any merge conflicts, if applicable
-
All branches will be behind
main, so for each branch,git checkout branch_name, thengit rebase main -
Settle any merge conflicts, if applicable
-
Every branch you rebase onto
mainwill now have content that the origin doesn’t know about. Runninggit pushwill result in an error, so you must either force push usinggit push -f(be very careful with this command) or create a new branch and delete the old one.
Good To Know
Section titled “Good To Know”Pretty Log
Section titled “Pretty Log”git log --all --decorate --oneline --graphDelete From Git Without Deleting Locally
Section titled “Delete From Git Without Deleting Locally”This is useful after updating the .gitignore.
-
Remove a file using
git rm --cached path/to/file -
Remove a directory recursively using
git rm --cached -r path/to/directory
Amending Commits
Section titled “Amending Commits”Amend the last commit using git commit --amend (This will bring up the commit editor. Commands are listed at the bottom, but Ctrl+O, Enter, Ctrl+X to write and exit.)
Amend the last commit instantly using git commit --amend -m "New message"
You’ll have to force push (or create a one-off branch and make a pull request) after amending the last commit: git push -f
Co-Authoring
Section titled “Co-Authoring”Co-author people by adding Co-authored-by: Amelia <ameliaq@email.com> to the end of any commit message
This is what it should look like:
Commit message
Co-authored-by: Amelia <ameliaq@email.com>You can add multiple co-authors:
Commit message
Co-authored-by: Amelia <ameliaq@email.com>Co-authored-by: Ellen <eripley@weylandyutani.org>Etiquette
Section titled “Etiquette”Branch names should be easy to read and easy to type. While naming a branch alexz/gar is fun, it also doesn’t convey what that branch is responsible for. Unless there’s an understanding that gar is a version with certain features, a better branch name would be alexz/catalogue_integration.
Why append your name to every branch you create?
Section titled “Why append your name to every branch you create?”First of all, appending your name to a branch isn’t a declaration that nobody else can work on it. It is, however, difficult to simultaneously work on the same branch. Branches are really convenient because they provide the opportunity to work without worry of overlap. If two people want to work on the same code in the same files, the work probably hasn’t been divided correctly. Pair programming can be done with one person typing and the other sitting next to the first making comments with both switching positions every once in a while. There are also programs that allow multiple to work on the same file at once. If multiple people want to work on the exact same code, try and avoid merge conflicts as much as possible by doing one of the aforementioned things. Naming a branch this way gives two important pieces of information: who should be contated about the code in that branch and what purpose it serves.
alexz tells someone checking into that branch that Alex Zorzella wrote that code, but doesn’t offer any other information. squirrel_gui tells someone checking into that branch that there’s going to be Squirrel GUI code in that branch, but they’ll have to read the commit history to find the person to contact. Yes, someone looking at a branch can always check the commit history for the author, but naming the branch well will lead to good info when someone calls git branch -a. alexz/squirrel_gui tells someone checking into that branch a lot of good information: both what is being worked on and who’s working on it.
ameliaq@meow:~/Documents/GitHub/repo $ git branch -amonal/squirrel_guildavinci/flying_machinekathyj/propellant_driver *remotes/origin/HEAD -> origin/mainremotes/origin/mainremotes/origin/mlisa/squirrel_guiremotes/origin/ldavinci/flying_machineremotes/origin/kathyj/propellant_driver