What is Git?

GIT was created in 2005 by Linus Torvalds who also created Linux.

It's a decentralized version control system. Many people can work on one single project without being on the same network. It keeps track of every changes in every versions of the same project.

When you work on git you will have a repository on your local machine to which you make changes then you push it to the remote repository such as github, bitbucket etc.

Basic Git Commands(For local repository):

$ git init - To initialize the staging area

$ git add <file> - To add files to staging area

$ git status - To check status of your staging area

$ touch <filename> - To create a file with name <filename>

$ git commit - To commit changes to index

Basic Github Commands(Commands needed to work with remote repository): 

$ git push - To push to remote repository

$ git pull - To pull from github

$ git clone - To clone one repository from remote repo to your local 

Installation:



To install Git run the Git installer for windows. It will also give you 'git bash' which is a command line environment similar to linux. To learn how git works its better to learn the command line.

Get the windows installer from git website as follows.



How to use gitbash:

To use it create a folder. Say the name is "myapp". Now right click on myapp folder and hit gitbash here. Git command prompt will begin from that folder. Hold ctrl + scroll to make it look bigger or smaller.

Once your files are in the folder type the command - git init

This will initialize this folder myapp & if 'show hidden files' is enabled you will also see a .git folder created in myapp. 


You need to have your usernme and email linked to local git. So type - 

git config --global user.name 'StupidUni'

Likewise for email address type -

git config --global user.email 'myemailid@stupiduniversity.xyz'



Once this is done. Let's stage one file from myapp. Say we have a file named index.html from myapp to be staged. For that 

git add index.html

When we hit that it doesn't show us anything so if we want to check if index.html is now moved to our staging area we can check by typing git status

To remove a file (index.html in our case) from staging area we type 

git rm --cached index.html

Now if we check using git status it will show all files within myapp are untracked. 👍

To add all files with .html extension to staging area use - git add *.html

To add everything - git add . (Note that a . not * will add everything to the staging area.)

Note that if we make changes to any of our files (say we edited code in index.html) then git will show us (through git status) that this file has been changed while in staging area & hence we need commit again. We can use 'git add .' to commit it all again.


To commit:

When we are ready we can commit using git commit

Notice that it will take you to a window. In order to type you need to hit "i" to get in insert mode. There all # are comment lines. All files are committed now. To get out hit ESC & type ":wq".

That is how you can commit. Now if you check using git status it will show nothing to commit as everything in myapp has already been committed. 

If you wish to commit but want to skip that editor screen, use 

git commit -m 'write whatever comment to display post commit here'

$ clear to clear the screen.




If we want git to ignore a certain file & don't want it to commit,

$ touch .gitignore

this will create a file named ".gitignore" in myapp folder. Now open it with any text editor and add the file names you want to be ignored If you write index.html in .gitignore and saved it, while committing it will ignore that.



Working with remote repository:


Go to github and create a new repository as shown above.

Give a name & description as you wish. Readme file is optional to include. Hit create.


Now head over to your git commandline and check if you already have any remote repo.

$ git remote

To add your new repo type:

$ git remote add origin https://github.com/StupidUni/SRC.git

$ git remote

This may return origin now as we added.

$ git push -u origin master

This will ask you to log in. Enter your github username & password and it will push to remote repo.

If you want to make changes do touch <filename.extension>, then edit the file with any text editor & then do git push. the updated file will be pushed to github.


To clone other's project:

$ git clone https://github.com/StupidUni/SRC.git

To pull:

$ git pull

This is when multiple people are working on the same repository and someone made any changes and you want to go back to the original folder.

2 Comments

  1. This has been helpful. Thanks for publishing it. I am a fan of your writing now. Totally no BS articles.

    ReplyDelete
  2. Awesome read on basics of Git. Am learning gitbash & it helps. Cheers from Minsk.🥂

    ReplyDelete

BRING THIS DEAD POST TO LIFE