| name | remote-git-operations |
| description | Best practices for remote Git operations including GitHub, Azure DevOps, pull requests, CI/CD integration, and branch protection rules. |
Remote Git Repository Operations
Purpose: Best practices for working with remote Git repositories, including GitHub, Azure DevOps, GitLab, and Bitbucket.
Remote Repository Setup
Adding and Managing Remotes
git remote -v
git remote add origin https://github.com/username/repo.git
git remote add upstream https://github.com/original/repo.git
git remote set-url origin https://github.com/username/new-repo.git
git remote remove upstream
git remote rename origin main-repo
git remote show origin
Clone Strategies
git clone https://github.com/username/repo.git
git clone https://github.com/username/repo.git my-project
git clone -b develop https://github.com/username/repo.git
git clone --depth 1 https://github.com/username/repo.git
git clone --recursive https://github.com/username/repo.git
git clone git@github.com:username/repo.git
Authentication Methods
HTTPS Authentication
git config --global credential.helper manager-core
git config --global credential.helper 'cache --timeout=3600'
git config --global credential.helper store
SSH Authentication (Recommended)
ssh-keygen -t ed25519 -C "your.email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
clip < ~/.ssh/id_ed25519.pub
ssh -T git@github.com
ssh -T git@ssh.dev.azure.com
git remote set-url origin git@github.com:username/repo.git
Azure DevOps Authentication
az login
git config --global credential.helper "!az account get-access-token --query accessToken -o tsv"
Fetching and Pulling
Fetch Updates
git fetch origin
git fetch --all
git fetch origin --prune
git fetch origin main:main
git log HEAD..origin/main
git diff HEAD origin/main
Pull Changes
git pull origin main
git pull --rebase origin main
git pull --force origin main
git config --global pull.rebase true
git pull --all
Pushing Changes
Basic Push Operations
git push origin main
git push -u origin feature-branch
git push --all origin
git push origin --tags
git push origin v1.0.0
git push origin --delete feature-branch
git push origin :feature-branch
git push --force origin main
git push --force-with-lease origin feature-branch
Push Best Practices
git status
git log origin/main..HEAD
dotnet test
npm test
git branch --show-current
git push --force-with-lease origin feature-branch
dotnet test --no-build
exit $?
Branch Management
Creating and Managing Remote Branches
git checkout -b feature/new-feature
git push -u origin feature/new-feature
git checkout --track origin/feature-branch
git checkout feature-branch
git branch -r
git branch -a
git push origin --delete old-feature
git fetch --prune
git remote prune origin
Syncing Fork with Upstream
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
git rebase upstream/main
git push --force-with-lease origin main
Pull Requests and Code Review
Preparing Pull Requests
git checkout -b feature/add-user-auth
git add .
git commit -m "feat(auth): Add user authentication"
git push -u origin feature/add-user-auth
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature/add-user-auth
git rebase -i HEAD~3
git push --force-with-lease origin feature/add-user-auth
Addressing PR Feedback
git add .
git commit -m "fix: Address PR feedback - update validation"
git add .
git commit --amend --no-edit
git push --force-with-lease origin feature/add-user-auth
git rebase -i origin/main
git push --force-with-lease origin feature/add-user-auth
Conflict Resolution with Remote
Handling Merge Conflicts
git pull origin main
git status
git add conflicted-file.cs
git commit -m "Merge: Resolve conflicts with main"
git push origin feature-branch
Rebase Conflicts
git rebase origin/main
git add .
git rebase --continue
git rebase --abort
git push --force-with-lease origin feature-branch
Working with Large Files (Git LFS)
git lfs install
git lfs track "*.psd"
git lfs track "*.mp4"
git lfs track "*.zip"
git add .gitattributes
git commit -m "Add Git LFS tracking"
git add large-file.zip
git commit -m "Add large binary file"
git push origin main
git clone https://github.com/username/repo.git
cd repo
git lfs pull
git lfs fetch origin main
git lfs checkout
CI/CD Integration
GitHub Actions Example
name: CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- name: Publish
run: dotnet publish -c Release -o ./publish
Azure Pipelines Example
trigger:
- main
- develop
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- task: UseDotNet@2
inputs:
version: '8.0.x'
- task: DotNetCoreCLI@2
displayName: 'Restore'
inputs:
command: 'restore'
- task: DotNetCoreCLI@2
displayName: 'Build'
inputs:
command: 'build'
arguments: '--configuration $(buildConfiguration)'
- task: DotNetCoreCLI@2
displayName: 'Test'
inputs:
command: 'test'
arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
Repository Maintenance
Cleaning Up
git clean -fd
git branch --merged main | grep -v "^\* main" | xargs -n 1 git branch -d
git gc --aggressive --prune=now
git fsck --full
git reflog expire --expire=now --all
git gc --prune=now --aggressive
Repository Statistics
git count-objects -vH
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sed -n 's/^blob //p' |
sort --numeric-sort --key=2 |
tail -n 10
git shortlog -s -n --all
git log --all --pretty=format:'%h %ad %s' --date=short | wc -l
Security Best Practices
Protecting Sensitive Data
appsettings.Development.json
appsettings.Local.json
*.secrets.json
.env
.env.local
*.key
*.pem
*.pfx
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch secrets.json" \
--prune-empty --tag-name-filter cat -- --all
bfg --delete-files secrets.json
git reflog expire --expire=now --all && git gc --prune=now --aggressive
Signed Commits
gpg --full-generate-key
gpg --list-secret-keys --keyid-format=long
git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgsign true
git commit -S -m "feat: Add feature"
git log --show-signature
gpg --armor --export YOUR_KEY_ID
Troubleshooting Common Issues
"Your branch is ahead/behind origin"
git log origin/main..HEAD
git log HEAD..origin/main
git pull --rebase origin main
git push origin main
"Failed to push: rejected"
git fetch origin
git merge origin/main
git push origin main
git rebase origin/main
git push origin main
git push --force-with-lease origin main
"Authentication failed"
git credential reject
git pull
git remote set-url origin git@github.com:username/repo.git
git config --global credential.helper
Large Push Failures
git config --global http.postBuffer 524288000
git remote set-url origin git@github.com:username/repo.git
git push origin main~10:main
git push origin main~5:main
git push origin main
Multi-Remote Workflows
Maintaining Multiple Remotes
git remote add origin https://github.com/yourfork/repo.git
git remote add upstream https://github.com/original/repo.git
git remote add backup git@gitlab.com:user/repo.git
git fetch --all
git push origin main
git push backup main
git remote set-url --add --push origin git@github.com:yourfork/repo.git
git remote set-url --add --push origin git@gitlab.com:user/repo.git
git push origin main
Platform-Specific Features
GitHub
gh repo create my-project --public
gh pr create --title "Add feature" --body "Description"
gh pr list
gh pr checkout 123
gh repo clone username/repo
Azure DevOps
git clone https://dev.azure.com/organization/project/_git/repo
git clone git@ssh.dev.azure.com:v3/organization/project/repo
git commit -m "feat: Add login #123"
GitLab
git clone https://oauth2:YOUR_TOKEN@gitlab.com/user/repo.git
git push -o merge_request.create \
-o merge_request.target=main \
-o merge_request.title="Add feature"
Performance Optimization
git clone --depth 1 --single-branch --branch main https://github.com/user/repo.git
git clone --filter=blob:none https://github.com/user/repo.git
git clone --no-checkout https://github.com/user/repo.git
cd repo
git sparse-checkout init --cone
git sparse-checkout set src/ docs/
git checkout main
git config --global fetch.parallel 8
git config --global protocol.version 2
Best Practices Summary
✅ DO
- Use SSH authentication for security
- Fetch before pushing
- Use
--force-with-lease instead of --force
- Keep commits small and focused
- Write clear commit messages
- Pull with rebase for cleaner history
- Protect main branch with branch policies
- Sign commits for verification
- Use Git LFS for large binary files
- Run tests before pushing
❌ DON'T
- Commit sensitive data (keys, passwords)
- Force push to shared branches
- Push untested code
- Rewrite public history
- Use ambiguous commit messages
- Commit large binary files without LFS
- Push directly to main (use PRs)
- Leave merge conflicts unresolved
- Ignore .gitignore patterns
Related Skills: