Node.js & Git Cheat Sheet
$git config --global init.defaultBranch main
Start a new Node.js project
$mkdir <project dir>
$cd <project dir>
$git init
$git remote add origin git@github.com:<user>/<repo>.git
$vim .gitignore
node_modules/
<server dir>/secrets
<server dir>/data
<server dir>/logs
.work
$mkdir <project dir>
$npm init -y
$npm install <packages>
$vim package.json
{
"name": "<project name>",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "copy(l)eft"
}
$git add .
$git reset
$git commit -m "Initial commit"
$git push -u origin main
Install a small HTTP server using built-in http module
const http = require('http');
const server = http.createServer((req, res) => {
res.write('Hello, World!');
res.end();
});
server.listen(<port number>, () => {
console.log('Server running at http://localhost:3000/');
});
Use GIT
Initialize Git & Connect to GitHub
$git init
$git remote add origin https://github.com/username/repository-name.git
$git add .
$git commit -m "Initial commit"
$git push -u origin main
Commit & Push to GitHub
$git add .
$git commit -m "your commit message"
$git push origin main
Branching (to try out new changes)
$git checkout -b new-branch-name
Make new changes in the new branch, then either delete or merge:
To discard the branch (revert all changes):
$git checkout main
$git branch -D new-branch-name
$git push origin --delete new-branch-name
To merge the branch and make changes permanent (no conflicts):
$git checkout main
$git merge new-branch-name
$git push origin main
Additional Tips for Conflict-Free Merging:
Make sure your branch is up-to-date with the main branch before merging:
$git checkout main
$git pull origin main
$git checkout new-branch-name
$git rebase main