Privacy Policy
© 2025 linux101.dev

Git: Commit and Push

Committing saves a snapshot of your staged changes to your local repository. Pushing sends those committed changes to a remote repository.

Commit Changes

Commit staged changes

git commit -m "commit message"

Commits the staged changes with a descriptive message.

Push Changes

Basic Push

Push changes to remote

git push [remote] [branch]

Pushes committed changes to a remote repository. If you don't specify [remote] and [branch], Git pushes to the **upstream branch** configured for your current local branch.

Push to set upstream branch

If your local branch is not yet linked to a remote branch, you can set the upstream branch while pushing:

git push -u origin <branch-name>

Replace <branch-name> with the name of your local branch. This command pushes the branch to the remote named origin and sets it as the upstream branch.

Force Push

A force push is used to overwrite changes on the remote branch with your local branch's changes. This can be dangerous as it can erase others' work.

git push --force [remote] [branch]

Use this command with caution. It is recommended to only force push branches that are not shared with others.

Example:

git push --force origin feature/branch-name

Force Push with Lease

A safer alternative to a regular force push is to use the --force-with-lease option. This option ensures that you only force push if the remote branch hasn't been updated since your last fetch.

git push --force-with-lease [remote] [branch]