Github icons created by Dave Gandy - Flaticon
checkout to previous commit
git checkout HEAD~1
- from here you can go back one more commit by typing
git checkout HEAD~1again - to go back to the latest commit, type
git checkout main
checkout past commit with hash
git checkout [hash]
- this will checkout the commit with the hash
- to go back to the latest commit, type
git checkout main
and reset to that commit
git reset --hard [hash]
- this will reset the repo to that commit
- to push to remote:
git push origin main --force
remove from staging area
git reset <file-path>
- Ensure the file is no longer tracked:
git rm --cached <file-path> - remove all files from staging:
git reset . - use case: this is helpful if I did "git add ." and realized I did not place the .env file in my .gitignore.
print origin repo to terminal
git remote get-url origin
disconnect from origin
git remote remove origin
- this can be done instead of just deleteing the .git file.
- this will keep all the git commit history of the repo
- after this command you can create a new remote repo and connect it as you would a completely new repo.
switch from https (token) to ssh credentials
git remote set-url origin git@github.com:your-username/your-repo.git
Make new git repo
- Make a folder on local computer.
- include readme and .gitignore.
git initgit add .git commit -m'[note goes here]'- Repo made locally with first commit
... and connect it to github
- 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.
- 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 git branch -M maingit push -u origin main- Go to your github repo site and make sure its update with your project.
Branching
Determine which branch you’re currently in
git branch- shows all branches and star next to current branch
Create and check into new branch
git checkout -b [new_branch_name]
Switch into a branch -
git checkout [branch_name]Push branch to GitHub (remote?) :
git branch --set-upstream-to=origin/main main
Delete Branch Local
- Delete local branch or use “-D” to force delete even unmerged changes
git branch -d [branch_name]
Delete from origin (my github repo)
git push origin --delete [branch_name]
https://www.youtube.com/watch?v=enSIyxqomHI
Merge branch
- Checkout master or in this case github-main
git checkout github-main- Then git merge file-restructrue
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.
- reccomended by Joakim
- I have used
:qin the past and this seems to only work when there are no changes to the Vim merge text - in other words, if we don't add any text to the merge message this works ok. - To add merge message:
i- write message
:wqwrite/saves and quites.
Stash
Stash with message
git stash save “your message here”
Stash with untracked files
git stash -u
apply a stash to a branch that is active
git stash apply stash@{#}
delete a stash
git stash drop stash@{#}
see all stashes
git stash list
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.
Commit the changes that undo
From terminal:
git rebase -i HEAD~2This 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”
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 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 To push to remote git push --force-with-lease origin nav_menu_01. You could do
--forceinstead of--force-with-lease, but--force-with-leasewon’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.
- navigate to main branch
- make following commands:
git fetch origin
git reset --hard origin/main
reset to a certain commit
git reset --hard cb37e7ca903814b05f5e1b76a14791d5a2e49450git 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:
- First, make sure you know what the last commit was. You can use:
git log --oneline
- Reset to the previous commit
- Soft Reset (if you want to keep your changes but remove the commit):
git reset --soft HEAD~1
- Hard Reset (if you want to discard the changes as well):
git reset --hard HEAD~1
- Force push the changes to the remote repository.
- This example was created based on a situation where I’ve already pushed the commit to the remote, so I need to force push the changes to remove the commit from the remote branch:
git push --force-with-lease origin main
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:
- from terminal:
ssh-keygen -t ed25519 -C "nrodrig1@gmail.com" - start ssh-agent:
eval "$(ssh-agent -s)" - create or modify ~/.ssh/config file
touch ~/.ssh/config
Host github.com
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
- place your ssh key in the Github page: https://github.com/settings/keys
- copy SSH public key from terminal command:
cat ~/.ssh/id_ed25519.pub