Tutorial 6 : Push changes to GitHub

What you will Learn:

Git checkout (to discard the changes)

‘git checkout <filename>’ is used to discard the changes in a file

Push changes to GitHub

The status command output now shows that there is nothing to commit

Push changes to GitHub

The diff command output now shows that nothing has changed

Push changes to GitHub

We have seen enough of Git, let us now move on to Github.

Importance of Github

 

Remember that, so far, the entire code is present in our local machine’s working diretory. If our machine crashes, all of our work will get lost. So the solution would be to store our Git project in some remote server viz Github server using ‘git push’ command (re-visit previous tutorial’s ‘Git components’ section)

Push changes to GitHub

The ‘git push’ command takes copy of files from our local machine and it stores them into the remote Github server

Push changes to GitHub

The advantage here is that any developer from all around the world can contribute to the same Github repository

Push changes to GitHub

Please note: We have already created Git account in our previous tutorial

Also Read: Tutorial 4: Initialize GIT

Create Git repository on GitHub

Launch https://github.com/login and login to your GitHub account

Push changes to GitHub

The below page comes up

Push changes to GitHub

Click ‘New’

Enter desired repository name in the ‘Repository name’ field. Select ‘Public’ if you want to share your GitHub repository with anyone or else select private, see below

Push changes to GitHub

 

Push changes to GitHub

Click ‘Create repository’

The below page comes up, so the link of github repository is (in your case, the link might be different)

https://github.com/W2AGit/practice-git

Look at the highlighted red box below. We will be using these commands to push an existing local repository to GitHub

Push changes to GitHub

Also Read: Tutorial 3: Create Git Repository

Push local GIT repository to remote GitHub (git push)

Make sure that you are in your local working Git directory (gitrepo) and when you ‘ls’ you should see the 3 files and a folder

Push changes to GitHub

 

Let us go back to our GitHub repo page and copy the first line

Push changes to GitHub

 

Paste it and execute

Push changes to GitHub

The syntax for the above command:
git remote add origin https://github.com/<yourGITusername>/<yourGITrepositoryName>.git

Below is the snapshot that shows username and repository name

Push changes to GitHub

Now, before we execute the next command, there is one thing to notice here. When you execute ‘git branch’ command, you will see that we get ‘master’ in the output. This means that we are right now working on a master branch

Push changes to GitHub

Even the below line shows (master) inside brackets

Push changes to GitHub

Now the thing is that, GitHub has recently renamed the ‘master’ branch to ‘main’ branch, though both are same

So our next command switches the branch from ‘master’ to ‘main’

Push changes to GitHub

Let us execute this command. Now you can see that we have switched to ‘main’ branch

Push changes to GitHub

The below command outputs ‘main’ this time

Push changes to GitHub

Now let us copy the last command

Push changes to GitHub

Paste it and execute. Type username and password

Push changes to GitHub

Click Ok

Push changes to GitHub

Let us now launch our GitHub repo https://github.com/W2AGit/practice-git

notice below that we see the files and folder. The second column shows us the commit messages that we added

Push changes to GitHub

If we go inside the folder, we can see test.html

Push changes to GitHub

Also, you can see ‘2 commits’ link, see below

Push changes to GitHub

Click ‘2 commits’ link, you would see the respective commits, see below

Push changes to GitHub

If you click on ‘added h1 tag’ link, you can see the change (see + sign below)

Push changes to GitHub

If you now add a new file to local repository, you can perform the following steps to push the file to GitHub:
touch newfile.html
git add .
git commit –m “some description”
git push

Once you perform above steps, refresh the GitHub repo, you should see the new file. You should see “3 commits’ and when you click this link, you should see the new commit message.

Also Read: Tutorial 5: Commit files to GIT

This is how we push our files from local working directory to remote GitHub repo.

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024