my-notes

git_push_and_pull

2 min read

git push & pull

Sync your local repository with remote repositories.


⬆️ git push

Push Current Branch

git push

Pushes current branch to its tracking remote branch.


Push to Specific Remote

git push origin main

Pushes main branch to origin remote.


Push and Set Upstream

git push -u origin feature-branch

Pushes and sets upstream tracking. Future pushes just need git push.


Push All Branches

git push --all

Pushes all local branches to remote.


Push Tags

git push --tags

Pushes all tags to remote.


Push Single Tag

git push origin v1.0.0

Pushes a specific tag to remote.


Force Push (Careful!)

git push --force

⚠️ Overwrites remote history. Use with caution!


Safe Force Push

git push --force-with-lease

Force push, but fails if someone else pushed first. Safer than --force.


Delete Remote Branch

git push origin --delete feature-branch

Deletes a branch on the remote repository.


📊 Push Flow

graph LR
    A[Local Commits] -->|git push| B[Remote Repository]
    B --> C[Other developers can pull]

⬇️ git pull

Pull Current Branch

git pull

Fetches and merges changes from remote tracking branch.


Pull from Specific Remote

git pull origin main

Pulls main branch from origin remote.


Pull with Rebase

git pull --rebase

Rebases local commits on top of remote changes instead of merging.


Pull Specific Branch

git pull origin feature-branch

Pulls a specific branch from remote.


Fetch Only (No Merge)

git fetch

Downloads changes but doesn't merge them.


Fetch All Remotes

git fetch --all

Fetches from all configured remotes.


Fetch and Prune

git fetch --prune

Fetches and removes local tracking branches that no longer exist on remote.


📊 Pull Flow

graph LR
    A[Remote Changes] -->|git fetch| B[Local Remote-tracking]
    B -->|git merge| C[Working Branch]
    A -->|git pull| C

⚙️ Configure Pull Behavior

Set Pull to Always Rebase

git config --global pull.rebase true

All git pull commands will rebase instead of merge.


Set Pull to Fast-Forward Only

git config --global pull.ff only

Pull will fail if fast-forward is not possible.


🔧 Handling Conflicts

When Pull Has Conflicts

git pull

If conflicts occur:

# 1. Fix conflicts in files
# 2. Stage fixed files
git add .

# 3. Complete merge
git commit -m "Merge remote changes"

Abort Pull Merge

git merge --abort

Aborts the merge and returns to state before pull.


💡 Tips

Always Pull Before Push
git pull --rebase && git push
Check What Will Be Pushed
git log origin/main..HEAD --oneline
Never Force Push to Shared Branches

Force pushing rewrites history and breaks other developers' work.



#git #push #pull #remote #basics

Built with mdgarden