View on GitHub

Git-GitHub-Workshop

This is a tutorial repo, used for PD Club's STW-PDW Workshop 2020.

Creating Repo Using Git Bash

Here we face 2 scenarios.

  1. Creating/Using locally created new/existing Repository.
  2. 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. Creating Repo using Git Bash

Pro tip - Open the folder in File Explorer & right Click on empty space and click on “Git Bash Here”

Creating Repo using Right click

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. Copying GitHub Repo WebURL

And use the command

git clone <url>

Cloning repo via Git Bash

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.

Empty GitBash Repo

Staging the Files

After Adding a sample text file -
GitBash Repo with sample txt 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. GitBash Staging Area

To commit the changes use this command -

git commit -m "<type a meaningful message here>"

For example -

git commit -m "Added sample file"

GitBash Commiting Files

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.

  1. Add new file.
  2. Modify existing file.

Here are the commands which I executed.

Demo-Repo part 1

Demo-Repo part 2

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.

GitBash having no remote Repository

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

To get the URL, refer “Creating new GitHub Repository” section. Once you copy the URL execute the command.

I will demonstrate using Gitbash Repo

  1. First I create a new GitHub repository here

Creating Remote Repository
After creating the repo, GitHub will show a quick setup page. Quick Setup Page

  1. To make things easier, copy the commands present at
    …or push an existing repository from the command line

And execute it in Git Bash. setting remote repository

  1. 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 -
GitBash Repo on GitHub

That’s all!