Comprehensive Git Workflows: From Repository Setup to Advanced History Management
Repository Initialization and Cloning
Initialize a new Git repository in the current directorry:
git init
Clone a remote repository to your local machine:
git clone https://gitlab.com/acme-team/project-beta.git
cd project-beta
Daily Development Cycle
Check the current state of your working directory and staging area:
git status
Stage specific files for the next commit:
git add src/components/Header.vue
git add docs/api-reference.md
Record staged changes with a descriptive message:
git commit -m "Add responsive navigation and update API docs"
View unstaged modifications in your working tree:
git diff
Branching and Navigation
Display existing branches with the current branch highlighted:
git branch --list
Create a new isolated development context:
git branch feature/oauth-integration
Switch to a specific branch to begin work:
git checkout feature/oauth-integration
# Alternative modern syntax:
git switch feature/oauth-integration
Combine branch histories using a merge commit:
git checkout main
git merge feature/oauth-integration
Remote Collaboration
Download changes from a remote server and integrate them immediately:
git pull upstream develop
Upload local commits to a remote repository:
git push upstream develop
Retrieve remote changes without automatically merging:
git fetch upstream
Correcting Mistakes
Remove files from the staging area while preserving working directory modifications:
git restore --staged config/database.yml
# Legacy syntax:
git reset HEAD config/database.yml
Create a new commit that reverses the changes from a specific previous commit:
git revert 9f4e2d1
Advanced Repository Configuration
Modify the URL of an existing remote repository after migration:
git remote -v
git remote set-url origin https://github.com/corporate-platform/api-gateway.git
git remote -v
Selective History Editing
Apply a specific commit from another branch onto your current branch:
git cherry-pick 8a3b2c1
Condense multiple consecutive commits into a single cohesive commit using interactive rebase:
git rebase -i HEAD~3
In the text editor that opens, change the command from pick to squash (or s) for commits you want to absorb into the previous one:
pick 3f4a5b2 Initialize database schema
squash 7c8d9e0 Add performance indexes
squash 1a2b3c4 Fix foreign key constraints
If conflicts arise during the rebase process, resolve them manually and continue:
git rebase --continue
To abort the rebase and return to the original state:
git rebase --abort
Release Management with Tags
Create a lightweight tag serving as a simple reference to a commit:
git tag v3.5.0
Create an annotated tag containnig metadata (author, date, message):
git tag -a v3.5.0 -m "Production release with search optimization"
Remove tags from local and remote repositories:
git tag -d v3.5.0
git push origin --delete v3.5.0
# Alternative syntax:
git push origin :refs/tags/v3.5.0
Advanced History Visualization
View compact one-line-per-commit history:
git log --oneline -15
Display a decorated graph with custom formatting and color coding:
git log --all --graph --pretty=format:'%C(cyan)%h%Creset -%C(magenta)%d%Creset %s %Cgreen(%ci) %C(bold yellow)<%an>%Creset' --abbrev-commit
Filter commits by specific criteria:
git log --author="dev@company.com" --since="2 weeks ago" --grep="refactor" --no-merges
Show detailed file statistics and patch content for each commit:
git log --stat -p