my-notes

git_push_to_remotes

1 min read

git push to remotes

Push changes to remote repositories.


⬆️ Basic Push

Push Current Branch

git push

Pushes to upstream tracking branch.


Push to Specific Remote

git push origin main

Pushes main to origin remote.


Push and Set Upstream

git push -u origin feature-branch

Pushes and sets upstream. Future pushes only need git push.


🔀 Push Options

Push All Branches

git push --all origin

Pushes all local branches to origin.


Push Tags

git push --tags

Pushes all tags to remote.


Push Single Tag

git push origin v1.0.0

Pushes specific tag.


Push with Follow Tags

git push --follow-tags

Pushes commits and any related annotated tags.


📊 Push Flow

graph LR
    A[Local Commits] -->|git push| B[Remote Repo]
    B --> C[origin/main updated]
    C --> D[Others can pull]

⚠️ Force Push

Force Push

git push --force origin main

⚠️ DANGEROUS: Overwrites remote history completely.


Safe Force with Lease

git push --force-with-lease

Force pushes but fails if someone else pushed first. Much safer.


Force Push Specific Branch

git push --force-with-lease origin feature-branch

Force push only your feature branch.


🗑️ Delete Remote Branch

Delete Remote Branch

git push origin --delete feature-branch

Removes branch from remote.


Alternative Syntax

git push origin :feature-branch

Alternative way to delete remote branch.


⚙️ Configure Push

Set Default Push Behavior

git config --global push.default current

Push current branch to same-named remote branch.


Push Behavior Options

Option Behavior
current Push current to same name
simple Push to upstream (default)
matching Push all matching branches
nothing Must specify explicitly

🔍 Check Before Push

What Will Be Pushed

git log origin/main..HEAD --oneline

Shows commits that will be pushed.


Dry Run

git push --dry-run origin main

Shows what would be pushed without pushing.


🔄 Push to Multiple Remotes

Add URL to Existing Remote

git remote set-url --add origin git@gitlab.com:user/repo.git

Adds second URL. Push goes to both.


Push to Specific Remote

git push github main

Pushes to remote named "github".

git push gitlab main

Pushes to remote named "gitlab".


💡 Tips

Always Pull Before Push

Avoids push rejection due to remote changes.

Never Force Push to Shared Branches

This rewrites history and breaks others' work.

Use -u First Time
git push -u origin branch

Then future pushes just need git push.



#git #push #remote #upload

Built with mdgarden