Written by dmarakom6 and assuming that you have installed git.
# Configure your identity
git config --global user.name "Giannis Giannakis"
git config --global user.email "[email protected]"
# Initialize a new repository
git init
# Clone existing repository
git clone https://github.com/user/repo.git
# Check status of changes
git status
# Add files to staging area
git add filename.java # Specific file
git add . # All changes
# Commit changes
git commit -m "Descriptive message"
# View commit history
git log
Branches let you try new ideas (like implementing a new class or algorithm) without breaking the working version of your code. If your experiment fails, just delete the branch.
- Multiple team members can work on different tasks simultaneously:
- One branch for UI improvements 🎨
- Another branch for bug fixes 🐛
- Main branch stays stable ✅
Each new feature (e.g., adding file I/O functionality) gets its own branch. This keeps changes organized and makes debugging easier.
Maintain separate branches for:
- Production-ready code (
main
) - Development work (
dev
) - Hotfixes for urgent issues (
hotfix-xxx
)
Key Benefit: Your main project stays functional even while adding complex new features.
# Create new branch
git branch branch-name
# Switch branches
git checkout branch-name
# Create and switch to new branch
git checkout -b new-branch
# Merge branches
git merge branch-name
# Connect to remote repository
git remote add origin https://github.com/user/repo.git
# Push changes to GitHub
git push origin main
# Pull latest changes
git pull origin main
# Fetch changes without merging
git fetch
# Unstage a file
git reset HEAD filename.java
# Amend last commit
git commit --amend
# Revert a commit
git revert commit-id
# Discard local changes
git checkout -- filename.java
- Fork a repository on GitHub
- Clone your forked repository
- Create a pull request after pushing changes
- Sync your fork with original repo (over https):
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git merge upstream/main
- Use
.gitignore
file to exclude files (e.g.,.class
,bin/
) - Commit often with meaningful messages, please.
- Pull before pushing to avoid conflicts (or just do
git push --force
) - Use
git diff
orgit status
to see changes before committing
Thanks for reading!