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 br1
echo "Hello from br1" > file.txt
git add file.txt
git commit -m "br1: first commit"
# Repeat commit steps 3x
git checkout br2
echo "Hello from br2" > file.txt
git add file.txt
git commit -m "br2: first commit"
# Repeat commit steps 3x
git checkout main
git merge br1
git cherry-pick <commit-hash>
git rebase main
# Makes commit history linear
git revert <commit-hash>
git reset --soft HEAD~1 # Keep staged
git reset --mixed HEAD~1 # Keep unstaged
git reset --hard HEAD~1 # Discard all
git remote add origin git@github.com-account1:username/repo.git
git push -u origin main
git log --all --oneline
ssh-keygen -t rsa -b 4096 -C "email@example.com" -f ~/.ssh/id_rsa_account1
Repeat for each GitHub/GitLab account.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_account1
Edit ~/.ssh/config
:
# GitHub Account 1
Host github.com-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/config
git clone git@github.com-account1:username/repo.git
git clone git@gitlab.com-account2:username/repo.git
git init
git add .
git commit -m "Initial commit"
git remote add origin git@github.com-account1: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 0
grep -rnw 'src/' -e 'TODO\|FIXME' && echo "Remove TODOs before commit!" && exit 1
find . -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 br1
git clone git@github.com-account1:shariful-w3/microservices-config.git
git clone git@gitlab.com-account2:shaarifulz/portableportal.git
Step 3: Add or update remote repo using your SSH configurations:
git remote set-url origin git@github.com-account1:shariful-w3/DevOps1Jenkins.git
- Always check
.ssh/config
if clone fails. - Avoid
System.out.println
in 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-jdk
java -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_old
Generate 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