📝 BLOG

Git and Version Control: Basic Commands and Workflows

Git is the fundamental tool of modern software development. This guide covers Git's basic commands, branching strategies, and workflows.

Last updated:

Prepared by: Gsetsoft

Git and Version Control: Basic Commands and Workflows

What is Git and Why Use It?

Git is a distributed version control system. It tracks code changes, manages different versions, and facilitates team collaboration.

Distributed structure: Each developer has a complete copy of the repository. This enables offline work and fast operations.

Branching: Create separate branches for different features or fixes. This keeps the main code (main/master) always stable.

Merge and rebase: Merge branches into the main branch. Merge preserves commit history. Rebase creates a linear history.

Basic Git Commands

git init: Create a new repository. git clone <url>: Copy an existing repository.

git add <file>: Add changes to staging area. git add .: Add all changes.

git commit -m "message": Commit staged changes. Write meaningful commit messages.

git status: Check repository status. git log: View commit history.

git diff: See differences between working directory and staging area. git diff --staged: See differences between staging area and last commit.

Branching and Merging

git branch <name>: Create a new branch. git checkout <name> or git switch <name>: Switch to branch.

git merge <branch>: Merge branch into current branch. Uses fast-forward merge or three-way merge.

Merge conflict: Manual resolution required for conflicting changes. Edit conflicting files, mark with git add, complete with git commit.

git branch -d <name>: Delete branch. git branch -D <name>: Force delete unmerged branch.

Remote Repository Operations

git remote add origin <url>: Add remote repository. git remote -v: List remotes.

git push origin <branch>: Send local changes to remote. git push -u origin main: Set upstream branch.

git pull: Fetch and merge remote changes. git fetch: Fetch remote changes (without merge).

git push --force: Force update remote branch (use carefully). git push --force-with-lease: Safer force push.

Branching Strategies

Git Flow: main (production), develop (development), feature/* (new features), release/* (release preparation), hotfix/* (urgent fixes).

GitHub Flow: main branch + feature branches. Each feature branch merged to main via pull request. Simple and effective.

Trunk-Based Development: Single main branch, short-lived feature branches (1-2 days). Combined with continuous integration (CI/CD).

Selection criteria: Git Flow for large projects, GitHub Flow for small-medium projects, Trunk-Based Development for CI/CD mature teams.

Best Practices

Commit messages: Use imperative mood ('Add feature' not 'Added feature'). Limit first line to 50 chars, details to 72 chars.

Atomic commits: Each commit should contain a single change. Split large changes into small commits.

Branch naming: Use descriptive names like feature/user-authentication, bugfix/login-error, hotfix/security-patch.

.gitignore: Ignore unnecessary files (node_modules, .env, build/). Use GitHub's gitignore templates.

Pull request: Use pull requests for code review. Every change should be reviewed by at least one person.

Your privacy choice

Essential cookies support security and sessions. Basic visits are also counted; with your permission we enable additional usage analytics. Cookie Policy