GitHub Logo
Github icons created by Dave Gandy - Flaticon

checkout to previous commit

git checkout HEAD~1

checkout past commit with hash

git checkout [hash]

and reset to that commit

git reset --hard [hash]

remove from staging area

git reset <file-path>

print origin repo to terminal

git remote get-url origin

disconnect from origin

git remote remove origin

switch from https (token) to ssh credentials

git remote set-url origin git@github.com:your-username/your-repo.git

Make new git repo

  1. Make a folder on local computer.
  2. include readme and .gitignore.
  3. git init
  4. git add .
  5. git commit -m'[note goes here]'
  6. Repo made locally with first commit

... and connect it to github

  1. Then go to github and make a new repo. Don't add anything.
    • I usually don't add a readme.md or .gitignore here. I do that after from the local git repo and push later.
  2. From the terminal in your local newly created repo: git remote add origin [url_provided when the repo was created in github.com] - example of this url might be: https://github.com/costa-rica/dashAndData.git
  3. git branch -M main
  4. git push -u origin main
  5. Go to your github repo site and make sure its update with your project.

Branching

https://www.youtube.com/watch?v=enSIyxqomHI

Merge branch

  1. Checkout master or in this case github-main
  2. git checkout github-main
  3. Then git merge file-restructrue
  4. git merge file-restructure

Merge Vim text editor

use :qa!: It stands for “quit all, forcefully” and will close Vim even if there are unsaved changes or multiple open files. It’s useful when you want to exit quickly without saving.

Stash

Stash with message

Stash with untracked files

apply a stash to a branch that is active

delete a stash

see all stashes

Squash last two commits

This is good if you wrote a print statement in a commit then do not need it any more and want to have a cleaner commit history. You can follow up that commit with another that removes the unneeded code. Then squashing will combine or merge the commits together. So you if you pull request the pull request approvers won’t see the unneeded change.

  1. Commit the changes that undo

  2. From terminal: git rebase -i HEAD~2

  3. This will lead to a screen that asks how to deal with the last two commits. See image below. Convert “pick” in the second line (3rd line in this terminal screenshot) to “squash” or “s”

  4. To exit this editor, + :wq + . notice the “~” at the bottom of the view. This means it is a in VIM. Nano uses “^”. REMEBER the “:” after pressing

  5. Immediately after exiting, if you squashed, you will be asked what you want the new commit message to be. 6.I simply delete the line that start with “revert”. Delete by pressing or “I”. Then delete. Then + :wq + . You can delete both lines that do not start with a “#” and create a new message entirely. Then <esc

  6. To push to remote git push --force-with-lease origin nav_menu_01. You could do --force instead of --force-with-lease, but --force-with-lease won’t overwrite other people’s changes.

Hard reset to latest remote commit

Assume you want to go back to the latest commit in your main branch.

  1. navigate to main branch
  2. make following commands:
git fetch origin
git reset --hard origin/main

reset to a certain commit

  1. git reset --hard cb37e7ca903814b05f5e1b76a14791d5a2e49450
  2. git push --force origin <branch-name>

More reset: go back two commit

[!! Warning this goes back two commits -- needs works!] To delete the last commit and go back to the previous commit in your Git history, you can follow these steps in the terminal:

  1. First, make sure you know what the last commit was. You can use:
  1. Reset to the previous commit
  1. Force push the changes to the remote repository.

Mac: save GitHub credentials locally

This will save locally permenantly git config --global credential.helper store

This creates a file locally for me it is: /Users/nickrodriguez/.git-credentials

Mac to use ssh instead:

  1. from terminal: ssh-keygen -t ed25519 -C "nrodrig1@gmail.com"
  2. start ssh-agent: eval "$(ssh-agent -s)"
  3. create or modify ~/.ssh/config file
Host github.com
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
  1. place your ssh key in the Github page: https://github.com/settings/keys