How to Make a Folder a Git Repo and Push to Remote
This article shows how to turn your local folders into Git repositories and push them to the remote repository.
The Git version control and GitHub will provide us with all the tools we need in our quest. Without further ado, let’s dive into today’s agenda.
Make a Folder a Git Repo and Push to Remote
The first step is setting up an empty remote repository. In this case, we’ve decided to use GitHub.
If you are unsure how to set up a remote, follow these steps.
- Go to your GitHub account and navigate the
Repositories
tab. - Click
New
to create a new repository. Give your repo a name and clickCreate repository
.
Do not include a README.md
file. Copy the link to the repository; we will need it in the next step.
Open Git Bash and cd
into the directory you wish to make a Git repository and initialize a Git repository with the git init
command.
Here is an example.
Note:
If you’re not using GitHub as your remote, use the git init --bare
command.
Now that our directory has been converted to a Git working directory, we can stage all the files for commit. Use the git add .
command to add all the files and folders to the index.
Before staging the files, let’s take a quick peep at our working directory.
$ git status
We can see that Git is not tracking the files and folders present. Let’s add them to the index.
$ git add .
Let’s check our index.
Our files are now under changes to be committed. We can run the command below to commit the changes and feed in a commit message.
$ git commit -m "Initial Commit"
Next, we will link the local repository with the remote GitHub repository. Remember the link we copied; it comes into play here.
Here is how you link the two.
$ git remote add origin https://github.com/Wachira11ke/Git-Tutorials.git
This will allow us to push and fetch changes from the remote. We can confirm this by running the git remote -v
command.
Now we can push changes to the remote repository. Since we are pushing for the first time, we will use the git push
command with the -u
flag.
This will instruct Git to push our changes to the remote and set up our branch to track the remote branch.
$ git push -u origin master
Output:
What does set up to track
mean?
When we set up a branch to track a remote branch, we can push and pull automatically without specifying the remote branch. You can use the git remote -vv
to check your remote-tracking branches.
We can convert non-empty local directories to Git repositories and push changes to the remote. Be sure to use the git init --bare
command if you do not plan on using GitHub as your remote.
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