Git
Git is a distributed version control system that helps you track changes to your code and collaborate with others.
Installation
Section titled “Installation”Windows
Section titled “Windows”- Download Git from git-scm.com
- Run the installer with default options
- Verify installation:
git --version
- Install with Homebrew:
brew install git - Or download from git-scm.com
- Verify installation:
git --version
# Debian/Ubuntusudo apt updatesudo apt install git
# Fedorasudo dnf install git
# Verifygit --versionInitial Configuration
Section titled “Initial Configuration”Set up your identity:
git config --global user.name "Your Name"git config --global user.email "your.email@example.com"Basic Git Commands
Section titled “Basic Git Commands”Creating Repositories
Section titled “Creating Repositories”# Initialize a new repositorygit init
# Clone an existing repositorygit clone https://github.com/username/repository.gitBasic Workflow
Section titled “Basic Workflow”# Check statusgit status
# Add files to staginggit add filename.java # Single filegit add . # All files
# Commit changesgit commit -m "Descriptive message about changes"
# View commit historygit logWorking with Branches
Section titled “Working with Branches”# Create a new branchgit branch feature-branch
# Switch to a branchgit checkout feature-branch# ORgit switch feature-branch
# Create and switch in one commandgit checkout -b new-feature
# List all branchesgit branch
# Merge a branch into current branchgit merge feature-branchRemote Repositories
Section titled “Remote Repositories”# Add a remotegit remote add origin https://github.com/username/repository.git
# Push changes to remotegit push origin main
# Pull changes from remotegit pull origin main
# Fetch changes without merginggit fetch origin.gitignore
Section titled “.gitignore”Create a .gitignore file to exclude build files and IDE configurations:
.gitignore
# Compiled class files*.class
# Log files*.log
# BlueJ files*.ctxt
# Mobile Tools for Java (J2ME).mtj.tmp/
# Package Files*.jar*.war*.nar*.ear*.zip*.tar.gz*.rar
# Virtual machine crash logshs_err_pid*replay_pid*
# IDE specific files.idea/.vscode/*.iml*.iws.classpath.project.settings/
# Build directoriestarget/build/out/bin/Workflow
Section titled “Workflow”-
Create a feature branch for new work:
Terminal window git checkout -b feat/user-authentication -
Write and test your code locally
-
Commit changes with descriptive messages:
Terminal window git add src/main/java/com/example/UserAuth.javagit commit -m "Implement user authentication with password hashing" -
Push changes to remote repository:
Terminal window git push origin feat/user-authentication -
Create a pull request (on GitHub/GitLab/etc.)
-
Review and merge after approval
Best Practices
Section titled “Best Practices”- Commit early and often
- Write meaningful commit messages
- Use branches for features and fixes
- Pull changes frequently to avoid merge conflicts
- Review changes before committing
- Don’t commit generated files or credentials