This documentation covers essential Git workflows, configurations (PAT & SSH), and advanced usage tips including hooks and multi-account SSH setups.
- Working Directory: Your local files.
- Staging Area (Index): Files prepared for the next commit.
- Commit History: Saved snapshots of your work.
- Stash: Temporary store of changes.
- Remote: Central repo (like GitHub/GitLab).
git init
git branch br1
git branch br2
git checkout br1echo "Hello from br1" > file.txt
git add file.txt
git commit -m "br1: first commit"
# Repeat commit steps 3xgit checkout br2
echo "Hello from br2" > file.txt
git add file.txt
git commit -m "br2: first commit"
# Repeat commit steps 3xgit checkout main
git merge br1git cherry-pick <commit-hash>git rebase main
# Makes commit history lineargit revert <commit-hash>git reset --soft HEAD~1 # Keep staged
git reset --mixed HEAD~1 # Keep unstaged
git reset --hard HEAD~1 # Discard allgit remote add origin [email protected]:username/repo.git
git push -u origin maingit log --all --onelinessh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_account1Repeat for each GitHub/GitLab account.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_account1Edit ~/.ssh/config:
# GitHub Account 1
Host github.202132.xyz-account1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_account1
IdentitiesOnly yes
# GitLab Account 2
Host gitlab.com-account2
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_account2
IdentitiesOnly yes
💡 Don’t forget:
chmod 600 ~/.ssh/configgit clone [email protected]:username/repo.git
git clone [email protected]:username/repo.gitgit init
git add .
git commit -m "Initial commit"
git remote add origin [email protected]:username/repo.git
git push -u origin main- Create the hook:
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit- Script content:
#!/bin/bash
echo "🔍 Running pre-commit checks..."
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.java$')
for file in $files; do
if grep -q "System.out.println" "$file"; then
echo "🚫 Error: 'System.out.println' found in $file"
exit 1
fi
done
echo "✅ Pre-commit checks passed."
exit 0grep -rnw 'src/' -e 'TODO\|FIXME' && echo "Remove TODOs before commit!" && exit 1find . -size +5M | grep -q . && echo "Too large files found!" && exit 1- Create
.git/hooks/commit-msg
touch .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg- Add script:
#!/bin/bash
pattern="^(feat|fix|docs|style|refactor|test|chore)\([a-z]+\): .{10,}$"
if ! grep -qE "$pattern" "$1"; then
echo "❌ Commit message format is invalid!"
exit 1
fi- Set default branch to
main:
git config --global init.defaultBranch main- Checkout existing branch:
git checkout br1git clone [email protected]:shariful-w3/microservices-config.git
git clone [email protected]:shaarifulz/portableportal.gitStep 3: Add or update remote repo using your SSH configurations:
git remote set-url origin [email protected]:shariful-w3/DevOps1Jenkins.git- Always check
.ssh/configif clone fails. - Avoid
System.out.printlnin production code. - Use hooks to enforce standards.
- Prefer SSH for secure and multi-account Git access.
To install Java on a Debian/Ubuntu-based system:
sudo apt update
sudo apt install openjdk-17-jdkjava -jar cismiddleware-0.0.1-SNAPSHOT.jar --spring.config.location=file:/root/cis-backend/executables/conf/Navigate to your certificate directory:
cd /etc/letsencrypt/live/example.com/Backup the existing .p12 certificate:
mv springboot.p12 springboot.p12_oldGenerate a new PKCS#12 (.p12) file from PEM certificates:
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out springboot.p12 -name springboot -CAfile chain.pem -caname root