In this tutorial, we will learn How to create your own local Git repository on Windows. Instead of using an online Git Repository like GitHub, BitBucket, and DevOps Azure, we will use a local folder and initialize it as our local code storage. Using a Git Repository we’ll help us share our code with our fellow developers. This is commonly used in development with more than 1 developer working on the same project.
What is Git repository used for?
A Git repository contains all of the project files and the entire revision history. This creates a . git subfolder, which contains all of the Git metadata for tracking changes
Before we proceed to the next steps, please make sure that you have a Git installed on your machine. You may also visit this link to download the latest Git for windows. link
I. Create a folder
Create a folder where you want to put your local Repository. In my case, I create a folder named Test Git.
II. Create and initialize a Repository
Now, let’s create the actual repository. To do that, cd to the Test Git folder and use the code to create and initialize a repository.
git init --bare test.git
- git init » this command creates a new Git repository
- –bare » this creates a repository that doesn’t have a working directory, making it impossible to edit files and commit changes in that repository. You would create a bare repository to git push and git pull from, but never directly commit to it.
To check if the initialization is successful, you can check using ‘ls’ command to view if the test.git already exists on the Test Git directory. You may also open the actual folder directory as shown below.
Now, cd to the test.git repository you will see a ‘(BARE:master)’ this will confirm that our repository is working.
III. Clone local Repository using Visual Studio
Now, that you have successfully created your local repository, let’s add some files and push using Visual Studio. In this tutorial below is the URL/path of my repository.
We will clone our repository using Visual Studio and add some files. To do that, follow the steps below.
If you prefer to clone your repository using a command prompt you may use the code snippet below. Choose a location from your local directory and execute this code.
git clone D:\Test Git\test.git
Cloning using Visual Studio 2019
- Open Visual studio and then select Clone Repository.
2. Input Repository path like shown below.
3. You will be prompted with a black solution since we don’t have any files yet. Let’s add a sample file to the repository. In my case, I added a test File. See the image below.
IV. Push File to the Repository using Visual Studio.
Now, that we have the file commit, and push the changes to our local repository.
- Open Git Changes Tab. Commit changes.
2. Push your committed changes.
3. Now, once you have done the steps above, you can check your changes by viewing the branch commit history. You can also view even your remotes repository. See the image below.
V. Git Bash Command
- Git Status
This can give you information such as:
- Your current branch
- Whether your current branch is up to date
- If there’s anything in the branch that needs to be committed, pushed, or pulled.
- If you have any files that are either staged or not staged.
- And if you have any files that are created, modified, or deleted.
$ git status
2. Git Add
This command will tell GIt to add a file to track it and add it to the staging file.
$ git add <file-name>
This command adds a specific file.
$ git add .
This will add all the changes to the staging area.
3. Git Commit
This command will save the changes made from your local repository.
$ git commit -a
This will commit all the changes in the directory you’re working in. Once this command is run, you’ll be prompted to enter a commit message.
$ git commit -m 'commit message'
3. Git Push
To make all your committed changes available to your teammates, you’ll have to push them to the remote origin.
$ git push <remote> <branch-name>
Note: This will only push all the committed changes.
In this tutorial, we have learned how to create your own local Git repository on Windows. To share the local repository that we create on this tutorial, we just need to share the folder where we place the repository. By using your local IP (//126.96.36.199/Test Git/test.git) you can share your changes with your teammates. Using a repository was also made easy by using Visual Studio since it has built-in functionality that adds, commits, and pushes changes without using a command prompt. Hopefully, this tutorial helps you collaborate with your team on your future projects.