Common Push Scenarios
Push Workflow Overview
graph TD
A[Local Changes] --> B[Stage Changes]
B --> C[Commit Changes]
C --> D[Push to Remote]
1. Basic Branch Push
Pushing changes to the main branch:
## Stage changes
git add .
## Commit changes
git commit -m "Update project features"
## Push to main branch
git push origin main
2. Pushing to Different Branches
Pushing to a specific feature branch:
## Create and switch to feature branch
git checkout -b feature/new-login
## Make changes and stage
git add login.py
## Commit changes
git commit -m "Implement new login system"
## Push feature branch
git push -u origin feature/new-login
3. First-Time Remote Push
Initial push to a new remote repository:
## Add remote repository
git remote add origin https://github.com/username/repo.git
## Push and set upstream
git push -u origin main
4. Force Push Scenario
Overwriting remote branch (use with caution):
## Force push changes
git push -f origin main
Push Scenario Comparison
Scenario |
Command |
Risk Level |
Use Case |
Normal Push |
git push |
Low |
Regular updates |
First Push |
git push -u |
Low |
Initial repository setup |
Force Push |
git push -f |
High |
Overwriting history |
Specific Branch |
git push origin branch |
Medium |
Feature development |
5. Pushing Multiple Branches
Pushing all local branches:
## Push all branches
git push --all origin
6. Handling Push Conflicts
Resolving push conflicts:
## Pull changes before push
git pull origin main
## Resolve merge conflicts
## Manually edit conflicting files
## Stage resolved files
git add .
## Commit merge
git commit -m "Merge and resolve conflicts"
## Push changes
git push origin main
Best Practices
- Always pull before pushing
- Use descriptive commit messages
- Avoid force pushing in shared branches
- Set up branch protection rules
At LabEx, we recommend understanding these push scenarios to improve your Git workflow and collaboration efficiency.