- VSCODE SHORTCUTS
- SET VSCODE AS DEFAULT EDITOR
- WORKFLOW
- COMMANDS
- GITHUB GIST
- MULTIPLE GITHUB ACCOUNT
- HEROKU
CMD+Shift+V
: open README Preview in VSCode
Ctrl+Alt+V
: open README Preview in VSCode
-
If you manually install Visual Studio Code, rather than using Homebrew, you will need to add the code executable to your PATH.
brew cask install visual-studio-code
-
In terminal
- Type:
open ~/.bash_profile
- Delete everything and insert:
export EDITOR="code -w"
- Type:
-
In visual studio code
- Press:
CMD + SHIFT + P
- Insert: install code and select from autocomplete menu shell command:
Install 'code'
in command PATH
- Press:
-
GitHub Workflow
-
The idea is to have the following branches
production
, real production workload (live code)hotfixes
, branches that are quick fixes on production
stating (release branches)
, blue / green server, duplicate of theproduction
environment (testing)- If all tests pass, we will swap it with production
main (development)
, where we are going to merge all featuresfeatures
qa
, perform tests before merging intostaging
-
-
On
Terminal
, create remote branchesgit checkout -b production git push git push --set-upstream origin production git checkout -b hotfixes git push git push --set-upstream origin hotfixes git checkout -b staging git push git push --set-upstream origin staging git checkout -b main git push git push --set-upstream origin main
-
On your repo
-
On
repo > Issues Tab > New issue
-
After creating a new issue, it will generate a issue number (ticket number, in this case
#1
)
-
New Ticket
- On
GitHub > Issues
- Click on New Issue
- Title:
Update README file
- Description:
I need to add GitHub workflow.
- Sidebar:
- Assignees:
Roger-Takeshita
- Labels:
feature
- Assignees:
- Click on Submit new issue
- Title:
- Click on New Issue
- On
-
Feature Branch
-
Create new feature branch (
feature/ticket-number-branch-name
) -
On
Terminal
:# From the main branch git checkout main # Create a new feature branch git checkout -b feature/1-update-readme-github-workflow # update README git add README.md git commit -m "#1 - should add github workflow" # Your commit message should follow # #1 -> Ticket number (don't forget the #) # - -> optional # msg git push git push --set-upstream origin feature/1-update-readme-github-workflow
-
-
Main Branch
-
Create a new Pull Request (
main
<-feature
) -
On
GitHub > Pull requests
- Click on New pull request
- Compare Changes
- base:
main
<- compare:feature/1-update-readme-github-workflow
- Click on Create pull request
- Description:
I've updated README file with github workflow
- Click on Create pull request
- Description:
- base:
- Compare Changes
- Click on New pull request
-
-
Staging Branch
-
After someone merge your PR into the main branch, we need to send to staging branch
-
On
Terminal
# Update the main branch with the merged modifications git checkout main git pull # Change to staging branch git checkout staging git merge main git tag "v1.0.0" # ^ ^ ^ # | | └── Hotfixes # | └── Feature/Minor updates # └── Major updates git push --tags
-
Create a new Pull Request (
staging
<-main
) -
On
GitHub > Pull requests
- Click on New pull request
- Compare Changes
- base:
staging
<- compare:main
- Click on Create pull request
- Title:
Latest README update - Add GitHub Workflow
- Description:
Added GitHub workflow
- Click on Create pull request
- Title:
- base:
- Compare Changes
- Click on New pull request
-
-
Production Branch
-
Create a new Pull Request (
production
<-staging
) -
On
GitHub > Pull requests
- Click on New pull request
- Compare Changes
- base:
production
<- compare:staging
- Click on Create pull request
- Title:
Staging to Production - Add GitHub Workflow
- Description:
Adding GitHub Workflow into production
- Click on Create pull request
- Title:
- base:
- Compare Changes
- Click on New pull request
-
-
New Ticket
- On
GitHub > Issues
- Click on New Issue
- Title:
Update README file immediately add hotfix
- Description:
I forgot to add hotfix doc
- Sidebar:
- Assignees:
Roger-Takeshita
- Labels:
hotfix
- Assignees:
- Click on Submit new issue
- Title:
- Click on New Issue
- On
-
Hotfix Branch
-
Create new bug branch (
hotfixes
) -
On
Terminal
:git checkout main git pull # From the production branch git checkout hotfixes git merge production # update README git add README.md git commit -m "#5 - should add hotfix doc" # Your commit message should follow # #5 -> Ticket number (don't forget the #) # - -> optional # msg git push git tag "v1.0.1" # ^ ^ ^ # | | └── Hotfixes # | └── Feature/Minor updates # └── Major updates git push --tags
-
-
Production Branch
-
Create a new Pull Request (
production
<-hotfixes
) -
On
GitHub > Pull requests
- Click on New pull request
- Compare Changes
- base:
production
<- compare:hotfixes
- Click on Create pull request
- Title:
Updated readme with hotfix doc
- Description:
Updated readme with hotfix documentation
- Click on Create pull request
- Title:
- base:
- Compare Changes
- Click on New pull request
-
git remote set-url origin <url>
git remote add upstream <url>
git remote -v
-
It often happens that while working on one project, you need to use another project from within it. Perhaps it’s a library that a third party developed or that you’re developing separately and using in multiple parent projects. A common issue arises in these scenarios: you want to be able to treat the two projects as separate yet still be able to use one from within the other.
-
More information how to clone a project with submodules Official Docs
git submodule add <repo_url>
-
Disable git from pushing to origin
git remote set-url --push origin no_push
git remote add upstream https://github.com/Roger-Takeshita/GitHub.git
git fetch upstream
git merge upstream/master master
# or
git rebase upstream/master
-
Fetch all the remote files that have been changed (just the paths)
-
It doesn't download the modifications
git fetch
-
Pull all the modified files
git pull origin master/branch
-
Download all modifications from upstream (a forked repo) to your local machine (master)
git pull upstream master
-
Sometimes we pull the modifications from origin/upstream but we change our mind, and we don't want to merge the modifications on our master branch. But we already pulled.
-
This will return to the state before we started the merge at any time.
git merge --abort
-
List all the file names that have conflicts + the line and highlight as a conflict
git diff --check | grep -i conflict
d2bs/kolbot/tools/ToolsThread.js:786: leftover conflict marker d2bs/kolbot/tools/ToolsThread.js:788: leftover conflict marker d2bs/kolbot/tools/ToolsThread.js:791: leftover conflict marker
-
List all the file names that have conflicts
git ls-files -u | cut -f 2 | sort -u
d2bs/kolbot/tools/ToolsThread.js
-
List all commits in one line, useful to get
hash keys
git log --oneline
git log -n --pretty=format:%s $hash
-
Option 1) If you want to view the last message, you can just add the
-n
= number of past commit(s), whitout$hash
. Example:git log -1 --pretty=format:%s
-
Option 2) IF you want to view a specific commit, you use
-n
=1
and$hash
=hash key
git log -n 1--pretty=format:%s a63ef55
a63ef55
is the hash key
-
To stash all the changes without the need to commit/push
git stash save "Your Message Here" git stash save "first stashed files" # Saved working directory and index state On master: first stashed files git stash list # stash@{0}: On master: first stashed files git stash save "second stashed files" # Saved working directory and index state On master: second stashed files git stash list # stash@{0}: On master: second stashed files # stash@{1}: On master: first stashed files
- Add a message to easily find what is all about that stash
-
To apply back the changes
- If we don't specify the stash number, git will apply the last stashed files (
stash{0}
)
git stash apply
- If we don't specify the stash number, git will apply the last stashed files (
-
To apply back a specific stash
# git stash apply stash@{stash_number_here} git stash apply stash@{0} # On branch master # Your branch is up to date with 'origin/master'. # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git restore <file>..." to discard changes in working directory) # modified: README.md # no changes added to commit (use "git add" and/or "git commit -a")
-
Show all the files that you have stashed in your last stash
git stash show # Config.md | 33 ++++++++++++++++++++++++----- # 1 file changed, 33 insertions(+), 5 deletions(-)
-
Show all the files that you have stashed of a specific stash
# git stash show -p stash@{stash_number_here} git stash show -p stash@{1} # README.md | 24 +++++++++++++++++++++--- # 1 file changed, 21 insertions(+), 3 deletions(-)
-
This command will create a new stash with all the stashed files
git stash store -m "your descriptive message here" stash@{1}
-
Discard all the stashed files/changes in your last stash
git stash drop
-
Discard all the stashed files/changes of a specific stash
# git stash drop stash@{stash_number_here} git stash drop stash@{1} # Dropped stash@{1} (043540bb638a411a785ae34fb7a700ec08686611)
-
There are often times when you want to modify a file but not commit the changes, for example changing the database configuration to run on your local machine.
-
Adding the file to .gitignore doesn’t work, because the file is already tracked. Luckily, Git will allow you to manually “ignore” changes to a file or directory.
git update-index --assume-unchanged <filename>
git update-index --no-assume-unchanged <filename>
- If you forgot what file did you
--assume-unchanged
, you can call the list using the following command:
git ls-files -v | findstr /B h
git ls-files -v | grep '^h'
- At some point you’ll find yourself in a situation where you need edit a commit message. That commit might already be pushed or not, be the most recent or buried below 10 other commits
-
This will open your $EDITOR and let you change the message. Continue with your usual git push origin master.
git commit --amend
-
We edit the message like just above. But need to
--force
the push to update the remote history. -
⚠️ But! Force pushing your commit after changing it will very likely prevent others to sync with the repo, if they already pulled a copy. You should first check with them.git commit --amend git push origin master --force
-
Rebase opened your history and let you pick what to change. With edit you tell you want to change the message. Git moves you to a new branch to let you
--amend
the message. git rebase--continue
puts you back in your previous branch with the message changed.git rebase -i HEAD~X # X is the number of commits to go back # Move to the line of your commit, change pick into edit git commit --amend # Change your commit message git rebase --continue # Finish the rebase
-
Edit your message with the same 3 steps process as above (
rebase -i, commit --amend, rebase --continue
). Then force push the commit:git push origin master --force
-
Find a commit that you want to edit
git rebase --interactive '3b2155d^'
-
Select the commit that you want to edit (
3b2155d
) -
Update the message
git commit --amend -m "Should add new file + configuration" # [detached HEAD 17f6a6d] Should add new file + configuration # Date: Mon Jun 28 18:52:09 2021 -0400 # 1 file changed, 0 insertions(+), 0 deletions(-) # create mode 100644 test.txt
-
After updating the message, continue rebase
git rebase --continue # Successfully rebased and updated refs/heads/main.
-
Push force
git push -f
-
-
Change the last commit date before pushing to remote
git commit --amend --no-edit --date="Fri Nov 6 20:00:00 2015 -0500"
git branch <branch name>
git branch -a
git checkout <branch name>
git checkout -b <branch name>
-
After You've Made the Changes on the Branch
-
Add and commit
git add -A git commit -m "message"
-
After Commit, Push Branch to Remote (Origin/Branch)
git push origin <branch name>
-
Merge a Branch to Local HEAD (Master) and Push to Master to Remote (Origin)
git checkout master # to change to master branch git pull origin master # just to be sure that local master is up to date git branch --merged # to check if the branch was merged, right now is just "master" git merge <branch name> # to merge the changes to local master git branch --merged # to check if the branch was merged git push origin master # to push this changes to remote master
git branch -d <branch name> # to delete local branch
git push origin --delete <branch name> # to delete remote branch
-
Switch to the local branch which you want to rename:
git checkout <old_name>
-
Rename the local branch
git branch -m <new_name>
If you already pushed the branch to remote
-
Rename your local branch
-
Push the the
<new_name>
local branch and reset the upstream branchgit push origin -u <new_name>
-
Delete the
<old_name>
remote branchgit push origin --delete <old_name>
-
To revert the file back to the state it was in before the changes. This will put your local git (HEAD) on your last commit and will erase all your modifications.
git checkout -- <filename>
- Remove from stage (after
git add -A
,git add <file>
orgit add .
) - NOT COMMITTED FILES
-
To remove files from stage use
reset HEAD
. This will unstage the file(s) and KEEP all the modifications.git reset #or git reset HEAD # unstage all files
-
Remove from stage (after
git add -A
orgit add <filename>
). This will unstage the file and KEEP all the modifications.git reset <filename> # unstage a specific file
-
This command will delete your last commit (not pushed) and all modification will be not staged, so you have to manually
git add
them back to stage.git reset HEAD~1
~1
is the number of commit(s)- ~ vs ^
-
This command will delete your last commit (not pushed) and all modification will be in stage
git reset --soft HEAD~1
~1
is the number of commit(s)- ~ vs ^
-
To remove files from stage use
--hard reset HEAD
. This will unstage the file(s) and DISCARD all the modifications.git --hard reset HEAD # unstage all files
-
This will DISCARD all the modifications and will set the HEAD to your previous commit(s).
git reset --hard HEAD~1 # reset last commit
~1
is the number of commit(s)- ~ vs ^
-
- Log all the pushed/committed files:
git log --oneline
268186e (HEAD -> master, origin/master, origin/HEAD) remove 3bdb527 Week 4, Day 1 - Exercise 2 - Express 1002de7 Week 4, Day 1 - Exercise 1 - Node 5ed3cc5 rename folders bec7979 rename folders a6f5fe2 Week 4, Day 1 - Exercise 3 - Lab Express b707a70 Week 4, Day 1 - Exercise 2 - Express e10d893 Week 4, Day 1 - Exercise 1 - Node b92f8b6 Week 4, Day 1 - Exercise 3 - Lab Express 1ad7b97 Week 4, Day 1 - Exercise 2 - Express 05694f2 Week 4, Day 1 - Exercise 1 - Node
-
- Copy all the hashes that you want to delete from github
268186e 3bdb527 1002de7 5ed3cc5 bec7979 a6f5fe2 b707a70 e10d893 b92f8b6 1ad7b97 05694f2
-
- Revert the local HEAD as many times you need:
git reset HEAD~1 #this will revert the HEAD 1 commit and keep the modifications In this example, we are going to use git reset HEAD~11 #this will revert the HEAD 11 commits
-
- Delete from GitHub
git push origin +268186e^:master git push origin +3bdb527^:master git push origin +1002de7^:master git push origin +5ed3cc5^:master git push origin +bec7979^:master git push origin +a6f5fe2^:master git push origin +b707a70^:master git push origin +e10d893^:master git push origin +b92f8b6^:master git push origin +1ad7b97^:master git push origin +05694f2^:master
-
- Double check if everything went all right:
git pull origin master git status
-
Using
filter-branch
- WARNING: If you run
git filter-branch
after stashing changes, you won't be able to retrieve your changes with other stash commands. Before running gitfilter-branch
, we recommend unstashing any changes you've made. To unstash the last set of changes you've stashed, rungit stash show -p | git apply -R
- WARNING: If you run
-
Run the following command, replacing
PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
with the path to the file you - want to remove, not just its filename. These arguments will:- Force Git to process, but not check out, the entire history of every branch and tag
- Remove the specified file, as well as any empty commits generated as a result
- Overwrite your existing tags
git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \ --prune-empty --tag-name-filter cat -- --all
# Example git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch 3-Taks-Manager/env/dev.env" \ --prune-empty --tag-name-filter cat -- --all # Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266) # Ref 'refs/heads/master' was rewritten
-
Add your file with sensitive data to
.gitignore
to ensure that you don't accidentally commit it again.# Example echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore git add .gitignore git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore" # [master 051452f] Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore # 1 files changed, 1 insertions(+), 0 deletions(-)
-
Once you're happy with the state of your repository, force-push your local changes to overwrite your GitHub repository, as well as all the branches you've pushed up:
git push origin --force --all
# Example git push origin --force --all # Counting objects: 1074, done. # Delta compression using 2 threads. # Compressing objects: 100% (677/677), done. # Writing objects: 100% (1058/1058), 148.85 KiB, done. # Total 1058 (delta 590), reused 602 (delta 378) # To https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git # + 48dc599...051452f master -> master (forced update)
-
In order to remove the sensitive file from your tagged releases, you'll also need to force-push against your Git tags:
git push origin --force --tags
# Example git push origin --force --tags # Counting objects: 321, done. # Delta compression using up to 8 threads. # Compressing objects: 100% (166/166), done. # Writing objects: 100% (321/321), 331.74 KiB | 0 bytes/s, done. # Total 321 (delta 124), reused 269 (delta 108) # To https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git # + 48dc599...051452f master -> master (forced update)
-
Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it.
git revert {hash key}
https://gist.github.com/Roger-Takeshita
-
Gist description: A brief description about your gist
-
File: Create any file just to GitHub let you create your gist
-
Generate a new ssh key using your new email
-
On
Terminal
:ssh-keygen -t rsa -C "your_new_email@gmail.com" # Generating public/private rsa key pair. # Enter file in which to save the key (/Users/roger-that/.ssh/id_rsa): /Users/roger-that/.ssh/id_rsa_dev # Enter passphrase (empty for no passphrase): your_password # Enter same passphrase again: your_password # Your identification has been saved in /Users/roger-that/.ssh/id_rsa_dev. # Your public key has been saved in /Users/roger-that/.ssh/id_rsa_dev.pub. # The key fingerprint is: # SHA256:I60nfahisdhfiahsidfhiasdifhiashyH4 your_new_email@gmail.com # The key's randomart image is: # +---[RSA 3072]----+ # | | # | | # | .| # | . ..| # | k S oo1.| # | o +.. .d%+=| # |. . . =.c+ .-+*.| # | p D =a*+.o o...| # |... +Ffff +*f | # +----[SHA256]-----+
-
Copy your new public SSH key
cat /Users/roger-that/.ssh/id_rsa_dev.pub # ssh-rsa AAAAB3NzafskdlfajsdjflajsdlfjalsdfeqlZwxFV4kMKsc9t8lAyS3DKWfahsidfhahsdifhaids/IDY+kfhakjsdhfkahsdkfhakshdfkaksdfoausdofuaosdufoausdofuoausdofuaosdufoausodfoausdofuaosdufoausdofuasdaosudfoasidfouaosdufoausdofuasdou/fa9sdf89as7d9f7a9sdf9as7df97as9df7a0s9df7a09sdf78asd98bxchvbixcvbkhxckvbxkchvlbxjcvklbjxlcjvb;xjcvblkxjcvb;jx;cvbj;xclvjb;xcv;lxkcjvb/26/oKMPWEZdoR7wvLVmjORn10ZQsIvI3swnwyxB7pxkaj;lsdfjka;sldjf;aklsjdf;alksjdf;lajsd;lfja;sdjfk;lajsdfl;kajs;dfja;lsdjf;alksdjf;ajsYhyZkz1XjaCobqTN+asdfkljasldfjalsjdflajsldkfjlajsd;g1uo1/qUg/DM= your_new_email@gmail.com
-
Go to
Settings
-
Click on
SSH and GPG keys > New SSH key
-
On
SSH keys / Add new
page
-
Add the the new ssh key to our known key list
ssh-add /Users/roger-that/.ssh/id_rsa_dev # Enter passphrase for /Users/roger-that/.ssh/id_rsa_dev: your_password # Identity added: /Users/roger-that/.ssh/id_rsa_dev (your_new_email@gmail.com)
-
In
/Users/roger-that/.ssh/config
(create one if file doesn't exist)Host unique_name HostName github.com User your_new_github_user IdentityFile /Users/roger-that/.ssh/id_rsa_dev
-
We need to set our remote origin slightly different
# If you don't have set the origin git remote add origin git@unique_name:/roger-takeshita-dev/codebase.git # OR # If you cloned from github git remote set-url origin git@unique_name:/roger-takeshita-dev/codebase.git
heroku login
heroku create <app_name>
heroku git:remote -a <app_name>
git push heroku master
git subtree push --prefix path/to/subdirectory heroku master
- where
path/to/subdirectory
is the path to the project that you want to deploy to heroku - for example we have this repo
- Inside we have a folder called
2_GraphQL_Prisma
(we want to deploy this folder)
- Inside we have a folder called
git subtree push --prefix 2_GraphQL_Prisma heroku master