Git-GitHub Basics.

Neha SawantNeha Sawant
8 min read

Git

What is Git:

Git is a distributed version control system (VCS) used to track changes in files. It allows multiple people to collaborate on a project, keeps a history of all changes made to the project, and ensures that all modifications are properly recorded.

Git allows you to track and manage changes to files over time. This makes it easy to revert back to previous versions of your project if needed, compare different versions, and collaborate with others.

Git is distributed, meaning every contributor has a full copy of the project’s history on their local machine. This allows for offline work, and contributors can work independently without relying on a central server for most operations.

Steps to access git on windows

On Windows: Download git bash → cmd/vs code.

On Mac: on terminal type #brew install git

On linux: on terminal type apt-get install git

#Git init:

  • used to initialize a new Git repository in the current directory. It sets up the necessary files and directories that Git needs to track your project’s changes. Essentially, it's the first step when you want to start version control for an existing project.

  • If you're working on a project and want to start using Git to track changes, git init turns your project folder into a Git repository. This means you can start using all Git features like commits, branches, and version history.

  • This repository will store all the history of changes made to the project, including added, modified, and deleted files.

How Does git init Work?

When you run git init in a directory, it does the following:

  • It creates a .git directory inside your project. This is where Git stores all the metadata and history of the repository.

Note: you can check using ls command

#Git Status:

  • It shows you the current state of your working directory and staging area. In other words, it helps you see which files have been changed, which are staged for commit, and which are untracked (not yet added to Git)

  • You have to run inside repo.

Syntax:

git status

Key Sections of git status Output:

  1. Branch Information: Shows which branch you're working on.

    • Example: On branch main
  2. Changes Not Staged for Commit: Lists files that have been modified but are not yet staged for commit.

    • Example: modified: index.html
  3. Changes to be Committed: Shows files that have been modified and added to the staging area.

    • Example: modified: index.html
  4. Untracked Files: Lists files that are not being tracked by Git and are not yet part of the repository.

    • Example: new-file.txt

#Git Branch:

  • It is used to manage branches in Git. A branch in Git is essentially a pointer to a specific commit. It allows you to isolate your work, such as when developing a new feature or fixing a bug, without affecting the main codebase.

Basic Git Branch Concepts:

  • main or master: The default branch where the stable code lives.

  • Feature branch: A branch created to develop a specific feature.

  • Hotfix branch: A branch created to quickly fix a bug in production.

Notes: when you craete folder in local it will take by default branch as master and if we have created folder/file on github it will take branch as main.

All Common Commands related branch:

git branch → List All Local Branches

git branch -a → List All Branches (Local + Remote)

git branch new-feature (New branch) → Create a New Branch

git checkout -b new-feature → Create and Switch to a New Branch (Shortcut)

git checkout feature-login → Switch to an Existing Branch (Switches to the feature-login branch.)

git branch -m old-name new-name → Rename a Branch

git branch -d feature-login → Delete a Local Branch

git push origin --delete feature-login → Delete a Remote Branch (Deletes the branch feature-login from the remote repository.)

git branch --no-merged → List Branches Not Yet Merged

git diff branch1..branch2 → Compare Two Branches

#Git add:

  • It is used to stage changes in your working directory for the next commit.

What is the "Staging Area"?

Git has three key areas:

  1. Working Directory – Where you make changes (edit files).

  2. Staging Area (Index) – Where you prepare changes before committing.

  3. Repository – Where committed changes are stored permanently.

git add moves changes from the working directory to the staging area.

Syntax:

git add → Add a specific file

git add . → Add all changes in the current directory

git add -A → Add all changes (tracked & untracked, including deletions)

git add *.html → Add all .html files

git add *.js → Stage all JavaScript files

#Git Commit:

is used to save your staged changes to the local Git repository. It creates a snapshot of your project at that moment, allowing you to track and revisit changes later.

What Happens When You git commit?

When you run git commit, Git:

  1. Takes all the files you've staged using git add.

  2. Stores them in the repository's history.

  3. Records metadata: author, timestamp, and message.

Syntax:

git commit -m "A meaningful commit message"

Git Workflow: Tracked vs Untracked Files-

Workflow Steps:

  1. 📄 New File (untracked)
    → 🔄 git status
    → Shows as: untracked file

    (untracked files - are files that exist in your local repository but have not been added.)

  2. 🟩 git add <file>
    → 📄 File becomes staged

    (tracked -they are already added to version control and will be included in future commits.)

  3. 🟩 git commit -m "message"
    → 📄 File becomes committed (tracked in repo history)

  4. 📄 Modify file (changes been made)
    → 🔄 git status
    → Shows as: modified

  5. 🟩 git add <file>
    → 📄 Back to staged

  6. 🟩 git commit
    → 📄 Changes are committed

  7. 🟩 git push
    → Changes sent to GitHub

Git log:

  • To check all modification we have done.

Github:

  • GitHub is a web-based platform that hosts Git repositories and provides a variety of features for version control, collaboration, and project management. It allows developers to store, manage, and share code with others in a collaborative environment.

Practical 1: Initialise Repo with git init.

  1. Git init to initialise repo. but you should initialise inside repo.

  2. Create file inside folder. (text.txt)

Practical 2: Check branch location, check tracked/utracked files.

  1. Inside folder enter git status

  2. we can see here in which branch we are in.

Practical 3: how to add file and commit the file on remote.

Filename: test.txt

  1. inside folder do git add test.txt

  2. git commit -m “file added“

  3. you will see 1 file is inserted.

  4. after doing git status you can see nothing to commit

Note: If you modify the test.txt then you have to add and commit the file using git add and git commit command. And if you modify changes and you commit the file. and you want the redo the data like before one and save it. then you don’t need to commit the file.

Practical 4: Staging and upstaging files:

  1. Create three files inside repo. I have created file1.txt, file2.txt, file3.txt

  2. check though git status.

  3. add all three files.

  4. Use below command to unstage file3.txt

    Syntax: git restore —staged filename

  1. Now commit both the file. And you are ready to push to remote.

Practical 5: You committed files and you want to undo commit you can use below command:

  1. You can check what you have commited till now using below command:

    Syntax: git log —stat

    we have committed file1.txt, file2.txt, file3.txt

  1. Now to undo commit. you can use below command:

    Syntax: git reset HEAD~1

    files are showing untracked.

Practical 6: Delete any file from git and restore it.

  1. delete any file from demo repo.

  2. do git status. you can see it is showing file is deleted.

  3. to restore that deleted file use git restore test.txt command.

Practical 7: You want to delete permanently which file committed already.

  1. Create any file (delete.txt)

  1. Check status of file using git status command.

  2. add and commit the files.

  3. Delete the file from location.

  4. Do add and commit to delete file permanently.

Note: You can only use restore command if your deleted file commited then only. if you have not added and committed that file and you deleted you won’t get that file.

Steps: create file→ add and commit file → delete file → add file → commit file so you can remove it permanently.

Practical 8: Add local file to git hub (remotely).

  1. Create folder on github eg. Git-Github.

  2. Copy the URL.

  3. Go to vscode or cmd or terminal on local. On terminal enter below command:

    Syntax: Git clone <url_of_folder>

  4. Your github remote repo is copied on local you can check files under that repo using ls command.

    (I already made one file i.e. gitnotes)

  5. Now create new file on local inside Git-Github folder. (thanks.txt). To check untracked/tracked file use git status command. To add and commit comand to track files.

  6. You have committed file in the local and to send this newly created file from local to remote/github. You have to use push command as below:

    Syntax: git push origin main

  7. Now check the github repo. You can see thanks.txt is there.

  8. If you create file in github.

  1. send that file from github to local. You have to use below command.

    git pull origin main.

#from_remote: created new file.

#in_local: use pull command

Output:

Practical 9: Delete any file from github.

  1. Check al files from repo.

  2. Remove thanks.txt using below command:

    Syntax:

    git rm filename

    git commit -m “removed“

    git push

Practical 10: If you delete file from local still you can see in github/remotely. How to remove those also.

  1. see deleted files using git status. restore those file using below command:

    git restore —stage filename

  1. after restoring, add and commit those files.

  1. They will get deleted and these changes should push to github by using git push.

0
Subscribe to my newsletter

Read articles from Neha Sawant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Neha Sawant
Neha Sawant