How
git
April 14, 20205 min read

How to push code to Github?

Git is one of the most popular Source Code Management(SCM) system. Git in itself is a huge topic and would take a lot of time and effort to master. This post is clearly for users to just get started, to push their first code/content to github.

Basic Git Commands

  • git config - Configure Git Project
  • git init - Initialize Git repository
  • git remote add origin - Link your local project folder to remote repository
  • git status - Check the status of a Git repository
  • git add - Track files
  • git commit - Commit tracked files
  • git push - Upload files
  • git pull - Download files

git config

First step is to signup on GitHub. You should now have a custom username and a dedicated GitHub page. To access your page, try - https://github.com/username

Let us say you have a folder on your computer which you want to upload(push in terms of git) to github. First you will need to configure your global git account.

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email username@email.com

git init

Now, open your project folder.

Initialize Git repository.

$ git init

Initialized empty Git repository in your folder path.

Great! Now you have an empty Git repo on your local computer.

git remote add origin

Now, link the folder on the local laptop to the github repository. This can be done by adding a remote origin to a git repository.

$ git remote add origin https://github.com/username/projectname.git

Nice! Now the remote origin is added to your local repository.

Considering you have couple of files in the local folder, let us say it is a website or a README.md file or a pdf or anything that you want to share on github, now it is time to check the status of the local repository. git status gives the list of changes that is not committed yet.

git status

To check the status of your local repository, enter the command -

$ git status
On branch master
Initial commit

Untracked files:
(use "git add ..." to include in what will be committed)

  index.html
  README.md
  documentation.pdf

nothing added to commit but untracked files present (use "git add" to track)

git status

You now see all the files that are new and untracked. Untracked files are the files that are new or modified and not yet tracked or added to the git repository. For the git repository to track the file, you need to add the file or files to it.

To add all the files in the folder do-

$ git add .

Otherwise, if you want to add the files individually to the repository you can specify the names of the files-

$ git add index.html README.md documentation.pdf

Since you have added the files, now let's check the status again with git status command.

$ git status
On branch master
Initial commit

Changes to be committed:
(use "git rm --cached ..." to unstage)

  new file: index.html
  new file: README.md
  new file: documentation.pdf

Earlier the files were in red color on the terminal, remember that? Now, since you have added the files, all the files are now green in color. They are now tracked. You see that in the terminal Changes to be committed.

git commit

Commit tracked files to the master branch

$ git commit -m "Initial Commit"
[master (root-commit)] Initial
  3 files changed, 334 insertions(+)

  create mode index.html
  create mode README.md
  create mode documentation.pdf

With this command, I commit the files that I added earlier with a message (-m) which we can recognise ("Initial Commit").

The commit text (Ex- "Initial Commit") is very important. As and when your code repository or your content repository grows, you track the changes you made historically using the commit message. Let us say if you need to revert the change you made earlier, commit message is something that you can relate to.

Write meaningful commit messages!

Example - "Changed the button color to yellow", "Added margin spacing for the table component", "Feature : Sorting of list", etc.,

git push

Here we go, you are just one step away from seeing your code on github!

Let's go ahead and do it

$ git push -u origin master

Terminal will prompt you to enter your GitHub username and password. After you enter and submit, the code gets pushed and you should see something like this on your terminal

Username for 'https://github.com': username
Password for 'https://username@github.com':
Counting objects: 7, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 16.51 KiB | 0 bytes/s, done.
Total 7 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
To https://github.com/username/projectname.git
   77061c7..1a436a1  master -> master

Now refresh your GitHub page https://github.com/username/projectname. You should now see all your files on Github website!

Congratulations! It was simple wasn't it?

git pull

Let us say you are on another computer or you want to pull the latest changes from the remote github repository to keep your local changes updated, you can pull the changes from github using the command-

$ git pull origin master

That's about it! :) Check out more git or github related content.

Share this blog
Tagged in :
git
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
April 14, 20205 min read
Next Article