Creating Repo Using Git Bash
Here we face 2 scenarios.
- Creating/Using locally created new/existing Repository.
- Creating/Using GitHub created new/existing Repository.
While Initial steps differ, but once the repo is initialized, steps are same for all given scenarios.
Scenario 1: Creating/Using locally created new/existing Repository.
Creating a local Repository
Open Git Bash. Navigate to a folder where you would want to create your repo.
Pro tip - Open the folder in File Explorer & right Click on empty space and click on “Git Bash Here”
To initialize the repo, run this command
git init
Using existing Repository
The method is same except the creation part. Navigate to the local Repo folder inside Git Bash.
Use this command to check current status.
git status
Scenario 2: Creating/Using GitHub created new/existing Repository.
Creating new GitHub Repository.
Click/Tap here to create a new GitHub Repository.
Or open your GitHub profile and create from there.
After creating, Copy the Web URL (ending with .git) In this picture, it is not showing completely. Just use that copy button.
And use the command
git clone <url>
Note: After cloning, you need to enter into the local repo folder. Use dir
command to list all the folders present. Use cd
command to enter inside your repo.
Using existing GitHub Repository.
Open your GitHub profile and Instead of creating a new Repo, open the repo where you wish to upload your code.
Rest of the steps are same as above.
Common steps
Adding files & other stuff
There are few Linux terminal commands which work in Git Bash. And some Windows CMD commands too. In the above screenshots you can observe that dir
is Windows CMD command & it works.
Equivalent Linux terminal command is ls
.
My point is, you can use the usual Linux terminal command to navigate & copy files.
For the sake of simplicity, I will not be using terminal/CMD commands to copy files. I will be using Windows File Explorer to copy the files, Atom Text editor to edit them & finally Git Bash to commit changes.
I will be using both the repos -
Demo-repo
(This one is hosted on GitHub)
Gitbash repo
(This one is locally created Repo)
To show how it is done.
Staging the Files
After Adding a sample text file -
The new files are currently unstaged. We need to move them to staging area where we can commit them & push the changes.
As you can see in the screenshot, the command for adding/Staging files for tracking is -
git add <complete file name>
The file name should be enclosed in quotation marks if it contains any spaces.
Simply putting the command yields this result-
Nothing specified, nothing added.
Pro tip - Use double quotes every time (even if there are no spaces in filename) to prevent any headaches & errors. In this way it is guaranteed that your file will be taken as input properly.
Repeat this command for all the files/folders you copied to the repo.
Pro tip - To stage all new files, folders & subfolders use the following.
git add -A
Or
git add -all
Committing the Files
After staging them, check using git status
to see how many files are being currently being staged.
In my case there’s only one file currently staged.
To commit the changes use this command -
git commit -m "<type a meaningful message here>"
For example -
git commit -m "Added sample file"
Pro tip - Incase you want to commit all new & modified files, use this command -
git commit -a -m "<type a meaningful message here>"
Pushing Commits to Remote Repository
Remote means situated away from local. In this context, a prime example would be GitHub Repository.
Here I will be using Demo-repo. Which is a GitHub Repo.
I have done 2 changes.
- Add new file.
- Modify existing file.
Here are the commands which I executed.
After making all commits, I have used this command to push the commits to remote Repository -
git push
As you can see in the screenshot, the remote branch was ahead of my local repo. So I have to pull all the commits/changes in to my local repo after which I can push my commits -
git pull
Setting Remote Repository for Local Repository
Focus: GitBash Repo
When you will try executing git push
command on locally created repo, Git Bash will report the following.
To set a remote Repository, we need name & URL.
And then we need to execute the following command
git remote add <name> <url>
The above command follows the generic syntax of git remote add
- Add: To add a new URL to the repository.
- Name: To give a name that you will use instead of the URL of the repository.
- URL: The URL of the repository.
To get the URL, refer “Creating new GitHub Repository” section. Once you copy the URL execute the command.
I will demonstrate using Gitbash Repo
- First I create a new GitHub repository here
After creating the repo, GitHub will show a quick setup page.
- To make things easier, copy the commands present at
…or push an existing repository from the command line
And execute it in Git Bash.
- To check whether the commits have been pushed successfully (even though Git Bash tells you though)
Open your GitHub repository and see for yourselves!
In my case, the repo had only 1 file - Sample File.txt
And on GitHub it shows like this -
That’s all!