Learn Git to GitHub with me

Easy to follow steps to create your first git repository.

ยท

5 min read

Beginners only

Sometimes I forget what are the commands that I used to create a git initialization and other basic git operations. So I thought of putting a post for myself and others who have the been looking for a shortcut or cheat-sheets, To get things done in git or github.

  • For git or github, all you need this link
                https://git-scm.com/doc
    
  • Direct git cheat-sheet link
               https://ndpsoftware.com/git-cheatsheet.html#loc=index;
    
  • Direct github cheatsheet link
               https://training.github.com/downloads/github-git-cheat-sheet/
    

Getting started with git

if you are starting a git initialization after you have successfully installed git in your ubuntu start terminal or for windows you have to use git bash terminal. copy and paste this one

Step.1:

Check if git is installed :-

$ git --version

output in the terminal shows the current version if git is already installed else install git using this post git-scm.com/book/en/v2/Getting-Started-Inst.. git version 2.25.1

$ git config --list --show-origin

This above command will show if git is already configured with username and email details if not , do it now

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

replace "John Doe" with your desired name.

After that create a folder using mkdir folder name to create a new empty folder and then use git cmd to initialize git.

Step.2 :

Initialize git

$ git init

Initialized empty Git repository in /home/username/urfoldername/.git/ To check it if .git exists we use

$ ls -la

this command prints all the files , including hidden ones as a list , here is the output of the terminal

total 12
drwxrwxr-x 3 user user1 4096 Jun  2 20:31 .
drwxrwxr-x 5 user user1 4096 Jun  2 20:31 ..
drwxrwxr-x 7 user user1 4096 Jun  2 20:31 .git

note : if you really want to know whats inside the .git file then use this command

$ ls -l .git/

this command prints all the .git files present inside it , here is the output of the terminal

total 32
drwxrwxr-x 2 user user1  4096 Jun  2 20:31 branches
-rw-rw-r--  1  user user1     92 Jun  2 20:31 config
-rw-rw-r--  1  user user1     73 Jun  2 20:31 description
-rw-rw-r--  1  user user1     23 Jun  2 20:31 HEAD
drwxrwxr-x 2 user user1  4096 Jun  2 20:31 hooks
drwxrwxr-x 2 user user1  4096 Jun  2 20:31 info
drwxrwxr-x 4 user user1  4096 Jun  2 20:31 objects
drwxrwxr-x 4 user user1  4096 Jun  2 20:31 refs

Try these commands in your system , first create a empty folder then start executing these above commands in your terminals.

Now we are going to create a file named sample.py, using linux touch command .

$ touch sample.py

This will create the file sample.py in your current directory. lets check if its there

$ ls
sample.py

Now lets add this file into git , first check if there is a file detected in git. use git status command to check if there is any changes in our working tree. we can see the file sample.py in our current local directory or folder and this is our working area. A term that is used in git to identify the different stages of the file. we start with working area where we work on the file if we need to track any changes then we add these files to staging area where it gets tracked for any changes.

Step.3:

Check the status

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    sample.py

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

As show in the terminal use git add filename to add the sample.py file to track from working tree to the staging area.

note : if you use git add . this will add all files into the staging area.

###Step.4 : Add the file

$ git add sample.py

if you use git status again we can see

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   sample.py

now you can see new file : sample.py in green letters , its successful.

###Step.5 : Commit the file

$ git commit -m "added new file in git"
[master (root-commit) de96e01] added new file to git
 1 file changed, 3 insertions(+)
 create mode 100644 sample.py

The file sample.py is successfully committed to the .git local repository. There's also single command that will do this ,

git commit -a -m "added new file in git"

Now check again

$ git status
On branch master
nothing to commit, working tree clean

This tells us the file is ready to be pushed into the a global repository or GitHub.

Step.6 :

Push it to the GitHub repository or in other words to the git cloud.

git remote add <name> <your github url>

get the url from github repository after creating a new one in GitHub.

$ git remote add <name> <your github url> 
$ git push --set-upstream <name> master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 293.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:

do a compare and pull request on GitHub

ย