Skip to content

Git

This content is for Java. Switch to the latest version for up-to-date documentation.

Git is a distributed version control system that helps you track changes to your code and collaborate with others.

  1. Download Git from git-scm.com
  2. Run the installer with default options
  3. Verify installation: git --version
  1. Install with Homebrew: brew install git
  2. Or download from git-scm.com
  3. Verify installation: git --version
Terminal window
# Debian/Ubuntu
sudo apt update
sudo apt install git
# Fedora
sudo dnf install git
# Verify
git --version

Set up your identity:

Terminal window
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Terminal window
# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/username/repository.git
Terminal window
# Check status
git status
# Add files to staging
git add filename.java # Single file
git add . # All files
# Commit changes
git commit -m "Descriptive message about changes"
# View commit history
git log
Terminal window
# Create a new branch
git branch feature-branch
# Switch to a branch
git checkout feature-branch
# OR
git switch feature-branch
# Create and switch in one command
git checkout -b new-feature
# List all branches
git branch
# Merge a branch into current branch
git merge feature-branch
Terminal window
# Add a remote
git remote add origin https://github.com/username/repository.git
# Push changes to remote
git push origin main
# Pull changes from remote
git pull origin main
# Fetch changes without merging
git fetch origin

Create a .gitignore file to exclude build files and IDE configurations:

.gitignore
Terminal window
# 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 logs
hs_err_pid*
replay_pid*
# IDE specific files
.idea/
.vscode/
*.iml
*.iws
.classpath
.project
.settings/
# Build directories
target/
build/
out/
bin/
  1. Create a feature branch for new work:

    Terminal window
    git checkout -b feat/user-authentication
  2. Write and test your code locally

  3. Commit changes with descriptive messages:

    Terminal window
    git add src/main/java/com/example/UserAuth.java
    git commit -m "Implement user authentication with password hashing"
  4. Push changes to remote repository:

    Terminal window
    git push origin feat/user-authentication
  5. Create a pull request (on GitHub/GitLab/etc.)

  6. Review and merge after approval

  • 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
Built with passion by Ngineer Lab