How to Commit and Push a Single File to the Remote in Git

John Wachira Feb 02, 2024
How to Commit and Push a Single File to the Remote in Git

This article outlines the steps to push a single file to the remote repository. You might find yourself with dozens of changed files on your working tree and only need to push a single file to the remote.

If you are unsure how to do this, this is the right place.

Commit and Push a Single File to the Remote

For easier context, let’s take the example below.

Working tree

Assuming the image above represents our working space’s state, how do we push only the untracked myfile.js file?

First, we will have to commit the file. We will start by adding the file to the index with the git add command, as shown below.

$ git add myfile.js

We can then commit the file.

$ git commit -m "Push single file"

What’s left is to push the file to the remote repository. We will use the git push command.

$ git push origin master

Note that Git will only push committed changes.

You must commit the file first to push a single file to a remote repository. Keep in mind that Git only pushes committed changes to the remote.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Push