GIT (Out of our syllabus but good to know)

 Git is a widely-used, open-source version control system that helps you track changes in your code, collaborate with others, and manage different versions of your project. Here’s a simple overview of the basic Git concepts and commands.


Key Concepts

  1. Repository (repo):
    A directory that contains your project and all the Git history for it.

  2. Commit:
    A snapshot of changes made to the files in your project.

  3. Branch:
    A separate line of development. The default branch is usually called main or master.

  4. Remote:
    A copy of the repository stored on a server (e.g., GitHub, GitLab, Bitbucket).

  5. Clone:
    A copy of a remote repository on your local machine.

  6. Staging Area (Index):
    A place where you prepare changes before committing.


Common Git Commands

1. Initialize a Repository

Start a new Git repository in your project directory.

git init

2. Check Repository Status

Shows the status of your working directory and staged changes.

git status

3. Add Changes to Staging Area

Add files to the staging area before committing.

git add <filename> # Add a specific file git add . # Add all changes

4. Commit Changes

Save your changes with a message describing the changes.

git commit -m "Your commit message"

5. View Commit History

Show a log of commits

git log

6. Create a New Branch

Create and switch to a new branch.

git branch <branch-name> # Create a new branch git checkout <branch-name> # Switch to an existing branch git switch <branch-name> # Alternative to 'checkout' for switching branches

7. Merge a Branch

Merge changes from another branch into the current branch.

git merge <branch-name>

8. Clone a Remote Repository

Copy a remote repository to your local machine.

git clone <repository-url>

9. Push Changes to Remote Repository

Upload your local commits to a remote repository.

git push origin <branch-name>

10. Pull Changes from Remote Repository

Fetch and merge changes from the remote repository.

git pull

11. Check Differences

Show changes that haven’t been staged or committed.

git diff # Show unstaged changes git diff --staged # Show staged changes

Simple Workflow Example

  1. Initialize a Repository

    git init
  2. Add Files and Commit

    git add . git commit -m "Initial commit"
  3. Create and Switch to a New Branch

    git branch feature-branch git checkout feature-branch
  4. Make Changes and Commit

    git add <filename> git commit -m "Add new feature"
  5. Merge the Branch Back to Main

    git checkout main git merge feature-branch
  6. Push Changes to Remote Repository

    git push origin main

Comments