Git Workflow Guide: From Project Setup to Feature Development
(needs updating with 2. Authenticate with github-cli)
This guide will walk you through the complete workflow of using Git for feature development, from initializing a new repository to successfully merging features into your main project.
Table of Contents
- Initializing Git and GitHub Setup
- Starting Work on a New Feature
- Handling Failed Features
- Merging Successful Features
- Post-Merge Steps
- CLI Reference
Initializing Git and GitHub Setup
Local Git Setup
# Initialize Git in your project directory
cd your-project-directory
git init
# Add all your files to Git
git add .
# Create initial commit
git commit -m "Initial commit"GitHub Repository Setup
- Go to GitHub.com and create a new repository
- Authenticate with github-cli
- Link your local repository to GitHub:
# Add the remote repository
git remote add origin https://github.com/username/repository-name.git
# Push your code to GitHub
git push -u origin mainStarting Work on a New Feature
Always start new features from your main branch:
# Ensure you're on main branch and it's up to date
git checkout main
git pull origin main
# Create and switch to a new feature branch
git checkout -b feature/your-feature-nameBest practices for feature development:
- Use descriptive branch names (e.g.,
feature/user-authentication) - Make regular commits with clear messages
- Push your branch to GitHub regularly:
git push origin feature/your-feature-nameHandling Failed Features
If your feature development isn't going well and you want to start over:
# Discard all changes and return to main branch
git checkout main
git branch -D feature/your-feature-name
# If you've pushed to GitHub, optionally remove remote branch
git push origin --delete feature/your-feature-nameMerging Successful Features
When your feature is complete and tested:
- Update your feature branch with latest main:
git checkout main
git pull origin main
git checkout feature/your-feature-name
git merge mainResolve any conflicts if they occur
Create a Pull Request (PR):
- Push your changes:
git push origin feature/your-feature-name - Go to GitHub and create a PR
- Add description of changes
- Request code review if working in a team
- Push your changes:
Merge the PR:
git checkout main
git pull origin mainPost-Merge Steps
After successfully merging your feature:
A. Clean up local branches:
# Delete local feature branch
git branch -d feature/your-feature-name
# Delete remote feature branch (if needed)
git push origin --delete feature/your-feature-nameB. Update your local main branch:
git checkout main
git pull origin mainBest Practices
- Always create feature branches from an up-to-date main branch
- Make regular, small commits with clear messages
- Test your changes thoroughly before merging
- Keep feature branches short-lived
- Delete branches after merging to keep repository clean
Commit Message Format
Follow conventional commits for clear history:
<type>(<scope>): <description>
[optional body]
[optional footer]Types:
- feat: New feature
- fix: Bug fix
- docs: Documentation changes
- style: Formatting changes
- refactor: Code restructuring
- test: Adding tests
- chore: Maintenance tasks
Example:
feat(auth): implement user authentication
- Add login form component
- Implement JWT token handling
- Add protected route middlewareThis structured approach helps maintain a clean and organized development workflow while ensuring your project's history remains clear and manageable.
CLI Reference
This section explains all the command-line interface (CLI) commands used in this guide.
Basic Terminal Commands
cd <directory>: Change Directory- Usage: Navigate between folders
- Example:
cd your-project-directorymoves into the specified directory - Example:
cd ..moves up one directory level
Git Commands
Repository Setup and Management
git init- Purpose: Initialize a new Git repository
- Creates a hidden
.gitfolder to track changes
git remote add origin <url>- Purpose: Link local repository to a remote repository (usually GitHub)
originis the conventional name for the primary remote repository- Example:
git remote add origin https://github.com/username/repo.git
Basic Git Operations
git add <file or directory>- Purpose: Stage changes for commit
git add .stages all changes in current directorygit add filenamestages specific filegit add directory/stages all changes in specific directory
git commit -m "<message>"- Purpose: Create a commit with staged changes
-mflag specifies a commit message- Example:
git commit -m "Initial commit"
git push <remote> <branch>- Purpose: Upload local commits to remote repository
- Example:
git push origin main -uflag (as ingit push -u origin main) sets up tracking, linking local and remote branches
Branch Operations
git checkout <branch>- Purpose: Switch to a different branch
- Example:
git checkout main
git checkout -b <new-branch>- Purpose: Create and switch to a new branch
-bflag creates a new branch- Example:
git checkout -b feature/user-auth
git branch- Purpose: List, create, or delete branches
git branchalone lists all local branchesgit branch -d <branch>deletes a branch (safe)git branch -D <branch>forces branch deletion (unsafe)- Example:
git branch -D feature/failed-experiment
Syncing and Merging
git pull <remote> <branch>- Purpose: Fetch and merge changes from remote repository
- Example:
git pull origin main - Combines
git fetchandgit mergeinto one command
git merge <branch>- Purpose: Merge changes from specified branch into current branch
- Example:
git merge feature/completed-feature
Status and Information
git status- Purpose: Show working tree status
- Displays changed files, staged changes, and branch information
git diff- Purpose: Show changes between commits, commit and working tree, etc.
- Shows exact lines that were changed
git diff filenameshows changes in specific file
Remote Operations
git push origin --delete <branch>- Purpose: Delete a remote branch
- Example:
git push origin --delete feature/old-feature
Common Flags Explained
-b: Create a new branch (used withcheckout)-d: Safely delete a branch (only if merged)-D: Forcefully delete a branch (even if not merged)-m: Specify a message (used withcommit)-u: Set up tracking between local and remote branches--delete: Remove a remote branch
Tips for Command Usage
- Use tab completion in terminal to avoid typing full paths/names
- Use up arrow to recall previous commands
- Add
--helpto any command for detailed documentation- Example:
git commit --help
- Example:
- Use
git statusfrequently to check your repository state - When in doubt about a destructive command (like branch deletion), use safer versions first:
- Try
git branch -dbeforegit branch -D - Try merging branches locally before pushing to remote
- Try
General Tips
- Always use a correctly formatted .gitignore file before adding and committing changes to the repository branch. Example: https://github.com/github/gitignore/blob/main/Node.gitignore
- If needed increase buffer sizes by
git config http.postBuffer 524288000 - Remove accidentally added files and folders by
git rm --cachedor for foldersgit rm -r --cachedthen commit and push if needed