Invalid Date

Git Basics Everyone Should Know

fnmalic

fnmalic

min read
Git Basics Everyone Should Know

Understanding Basic Commands

Git Configuration

Before diving into commands, set up your identity:

1git config --global user.name "Your Name" 2git config --global user.email "your.email@example.com"

Essential Commands Explained

  1. Repository Initialization
1git init

This creates a .git directory that stores all the version control information. After initialization, Git starts tracking all files in the directory.

  1. Staging Changes
1git add filename.txt # Stage specific file 2git add . # Stage all changes 3git add *.js # Stage all JavaScript files 4git add src/ # Stage entire directory

The staging area (or index) is like a preparation zone where you organize changes before committing.

  1. Checking Status
1git status 2git status -s # Short format status

Status flags:

  • M: Modified
  • A: Added
  • D: Deleted
  • ??: Untracked
  • R: Renamed
  1. Committing Changes
1git commit -m "Add login feature" 2git commit -am "Update styles" # Automatically stage tracked files and commit
  1. Viewing History
1git log 2git log --oneline # Compact view 3git log --graph # Visual representation 4git log --author="name" # Filter by author 5git log -p # Show patches

Advanced Branching Strategies

GitFlow Model

A robust branching strategy following Vincent Driessen's model:

  1. Main Branches

    • main: Production code only
    • develop: Integration branch
  2. Supporting Branches

    • Feature branches: feature/user-authentication
    • Release branches: release/1.0.0
    • Hotfix branches: hotfix/login-bug

Branch Management Commands

1# Create and switch to new branch 2git checkout -b feature/new-feature 3 4# List branches 5git branch # Local branches 6git branch -r # Remote branches 7git branch -a # All branches 8 9# Delete branch 10git branch -d branch-name # Safe delete 11git branch -D branch-name # Force delete 12 13# Rename branch 14git branch -m old-name new-name

Merging Strategies

  1. Fast-forward Merge
1git merge feature-branch
  1. No-fast-forward Merge
1git merge --no-ff feature-branch
  1. Squash Merge
1git merge --squash feature-branch

Comprehensive Commit Message Guidelines

Structure

<type>(<scope>): <subject>

<body>

<footer>

Types

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation
  • style: Formatting
  • refactor: Code restructuring
  • test: Adding tests
  • chore: Maintenance

Example

feat(auth): implement OAuth2 authentication

- Add OAuth2 client configuration
- Implement token refresh mechanism
- Add user session management

Closes #123

Best Practices

  1. Separate subject from body with blank line
  2. Limit subject line to 50 characters
  3. Capitalize the subject line
  4. Don't end subject line with period
  5. Use imperative mood in subject line
  6. Wrap body at 72 characters
  7. Use body to explain what and why vs. how

Working with Remotes in Detail

Remote Management

1# Add remote 2git remote add origin https://github.com/user/repo.git 3 4# View remotes 5git remote -v 6 7# Change remote URL 8git remote set-url origin new-url 9 10# Remove remote 11git remote remove origin

Push Operations

1# Push to remote 2git push origin branch-name 3 4# Push all branches 5git push --all origin 6 7# Force push (use carefully!) 8git push -f origin branch-name 9 10# Set upstream branch 11git push -u origin feature-branch

Pull Operations

1# Fetch remote changes 2git fetch origin 3 4# Fetch and merge 5git pull origin main 6 7# Pull with rebase 8git pull --rebase origin main

Advanced Git Operations

Stashing

1# Basic stash 2git stash 3 4# Stash with message 5git stash save "WIP: implementing login" 6 7# List stashes 8git stash list 9 10# Apply stash 11git stash apply stash@{0} 12 13# Pop stash 14git stash pop 15 16# Drop stash 17git stash drop stash@{0}

Recovery and Undo

1# Discard changes in working directory 2git checkout -- filename 3 4# Undo last commit (keep changes) 5git reset HEAD~1 6 7# Revert commit 8git revert commit-hash 9 10# Amend last commit 11git commit --amend

.gitignore Patterns

# Ignore specific file
secret.key

# Ignore directory
node_modules/

# Ignore by pattern
*.log
**/temp

Troubleshooting Common Issues

  1. Merge Conflicts
1# Abort merge 2git merge --abort 3 4# After resolving conflicts 5git add . 6git commit
  1. Detached HEAD State
1git checkout main 2git checkout -b new-branch # If you need to save changes
  1. Large File Issues
1# Clean repository 2git gc 3git prune 4 5# Remove file from history 6git filter-branch --force --index-filter \ 7 'git rm --cached --ignore-unmatch PATH-TO-FILE' \ 8 --prune-empty --tag-name-filter cat -- --all
#git#version-control#programming#developer-tools#software-development#coding-basics#git-commands#git-workflow#branching-strategy#commit-messages#remote-repository#technical-guide#development-basics#code-management#software-engineering

Enhanced Reading Experience

Explore this article with AI-powered features designed to enhance your understanding

AI Summary & Voice Narration

Get an AI-generated summary of this article that you can listen to

💬 Ask AI about this article

Ask me anything about this blog post! I'll answer based on the article content and provide sources.

Comments (0)

Please log in to leave a comment