Post

Git Push Cheatsheet

Basic Push:

This command pushes changes from your local branch to the corresponding remote branch.

1
git push

Push Specific Branch:

Replace <remote_name> with the name of the remote repository (usually origin) and <branch_name> with the name of the branch you want to push.

1
git push <remote_name> <branch_name>

Push All Branches:

Pushes all branches to the remote repository.

1
git push --all

Push Tags:

Pushes all tags to the remote repository.

1
2
git push --tags

Force Push:

Forces the push operation, overwriting remote changes with your local changes. Use with caution.

1
git push --force

Push a New Branch:

1
git push -u <remote_name> <branch_name>

Pushes the specified branch to the remote repository and sets it to track the upstream branch.

Push with Force and Lease:

A safer alternative to –force, which checks if the remote ref is updated. Helps prevent unintentional overwriting of changes.

1
git push --force-with-lease

Push a Commit to Different Branch:

Pushes a specific commit to the specified branch on the remote repository.

1
git push <remote_name> <commit_hash>:<branch_name>

Push to Multiple Repositories:

Adds multiple push URLs for a remote repository. Helpful for pushing to multiple remotes simultaneously.

1
git remote set-url --add --push <remote_name> <url>

Pushing to Multiple Repositories

Suppose you have a repository hosted on both GitHub and GitLab, and you want to push changes to both:

1
2
3
git remote set-url --add --push origin git@github.com:username/repository.git
git remote set-url --add --push origin git@gitlab.com:username/repository.git
git push origin master

This configures your origin remote to have two push URLs, one for GitHub and one for GitLab. Then, when you push changes to origin master, Git pushes to both repositories.

Remember to replace placeholders like <remote_name>, <branch_name>, and <url> with actual names and values relevant to your Git repository setup. Always ensure you understand the consequences of your push actions, especially when using force options.

Pushing all branches

If you want to push all branches to each repository, you can use the --all flag:

1
git push --all repo_name
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.