pre-requisites:

1. Create a new repository

Sign into your GitHub account and on the left hand menu find and click “New” button, this will navigate you to “Create a new repository” page.

  • Add the details for your repository – name and description about your project/repository
  • Select repo type – public or private (private is only you can see the repo, public is accessible by anyone on the internet)
  • Optional – You can initialize the repository with a README file
  • Once done click on “Create repository“.

The above step will create a new repository and take you to the repository page.

2. Cloning a repository

Now on to cloning the repository that is created in the first step (In most cases, we will be cloning repositories and not create them newly) .
If the repository already exists you can skip step 1.

From the repository page click on the green button “Code” and copy the repository link that is shown. In your system terminal run the command as below.

git clone <Your copied repo link>
example:
git clone https://github.com/devarajbh/createRepoTest.git

After running the command, it’ll prompt you for username and password (These are the GitHub credentials that you used to create the GitHub account), enter the details and repo should be cloned to your system.

3. Modifying the repository

In step 2, a new directory/folder will be created with the repository name and this copy of the repository is commonly referred to as local (local repo). This is the folder that will be used to add any new changes that are required.

Move to the newly created directory/folder and create a new file. Once a new file is added, run the below command.

git status

The above command should show you a new file in red letters, status command shows current state of your local repo. In other words, the files listed in red colour after running the above command are the files that were modified.

To select the file that needs to added to remote repository (origin repository which was cloned to our system in step 2). You can run the below command to move the files to staging area. Staging usually refers to selecting the changes that needs to be added to your remote repository.

git add <filename/foldername>
#If you want to stage all the files that were modified you can run
git add .

Run the git status command again, now the staged files should turn green. These are the files that will be committed and pushed to remote repository.

4. Committing the changes

Committing the changes usually mean writing the changes to your repository (Your local repository) and these will still be local changes and are not visible in the remote repository yet. To commit your changes run the below command

git commit -m "Your commit message"    # -m here refers to the commit message - Usually add one liner about the changes 

You can run the below command to double check the commit that you made – it will show you a unique commit id and your commit message that you added. This is just to confirm that changes have been committed (it’s an optional step).

git log

5. Pushing the changes

This is the final step to see your changes in your remote repository – Until this point, no one will be able to see your changes that you have made in your local repository.

To push your changes run the below command.

git push

After this, if you go to browser and refresh the repository page – Your newly added file should be visible in the remote repository. These changes are now visible to anyone who has access to the repository.

One thought on “Basic git commands for creating a repository and pushing changes”

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights