Skip to content
Hasan-Arda-Gunes edited this page Feb 19, 2025 · 5 revisions

Introduction to Git

Git is a distributed version control system that allows developers to track changes in their code, collaborate with others, and manage projects efficiently. It was created by Linus Torvalds in 2005 and has become the industry standard for version control. Unlike centralized version control systems, Git allows every developer to have a full copy of the repository, making it highly reliable and robust.


How Git Works

Git operates on a distributed version control model, meaning each developer has a complete copy of the repository, including the entire history. This makes Git highly efficient and reliable. Here’s a simplified workflow of how Git works:

  1. Working Directory: The local folder where files are created, edited, and deleted.
  2. Staging Area: Files that are marked to be included in the next commit.
  3. Local Repository: Commits are stored locally in the .git directory, maintaining a history of changes.
  4. Remote Repository: The central repository (e.g., on GitHub, GitLab) where changes can be pushed and shared with others.

Basic Git Workflow:

  1. Modify files in the Working Directory.
  2. Add files to the Staging Area using git add.
  3. Commit changes to the Local Repository using git commit.
  4. Push changes to the Remote Repository using git push.
  5. Pull updates from the Remote Repository using git pull to keep the local copy up to date.

This decentralized approach allows for collaboration while maintaining version history and avoiding conflicts.


Why Use Git?

Git offers several advantages, including:

  • Version Control: Track changes and revert to previous versions if needed. Every change in the codebase is recorded, allowing developers to go back in time and review modifications.
  • Collaboration: Work on projects with multiple developers without conflicts. Git enables multiple contributors to work on the same codebase simultaneously while efficiently managing changes.
  • Branching & Merging: Create isolated branches for features, bug fixes, or experiments. This ensures that new features can be developed independently without affecting the main codebase until they are ready to be merged.
  • Distributed System: Every developer has a full copy of the repository, reducing dependency on a central server and ensuring data redundancy.
  • Efficiency: Optimized for speed and performance. Git performs operations such as commits, branching, and merging quickly, even with large projects.

Basic Git Commands

Here are some fundamental Git commands to get started:

1. Configuring Git

Before using Git, set up your identity:

 git config --global user.name "Your Name"
 git config --global user.email "[email protected]"

This ensures that all commits you make are associated with your identity.

2. Initializing a Repository

To start tracking a project with Git:

 git init

This creates a new Git repository in the current directory, allowing version control to begin.

3. Cloning a Repository

To copy an existing repository from GitHub:

 git clone https://github.com/user/repository.git

This command downloads the repository and all its history to your local machine.

4. Checking Repository Status

To check changes in your working directory:

 git status

This command shows which files have been modified, staged, or remain untracked.

5. Adding Files to Staging Area

To add specific files:

 git add filename

To add all changes:

 git add .

Adding files to the staging area prepares them to be included in the next commit.

6. Committing Changes

Save changes with a message:

 git commit -m "Your commit message"

Commits are like snapshots of the repository at a specific point in time, allowing easy rollback if needed.

7. Pushing Changes to Remote Repository

To send your local commits to GitHub:

 git push origin main

This uploads your changes to the remote repository, making them available to other contributors.

8. Pulling Latest Changes

To fetch and merge changes from the remote repository:

 git pull origin main

This command updates your local repository with the latest changes from the remote repository.

9. Creating a New Branch

To create and switch to a new branch:

 git checkout -b new-branch

Branches allow you to work on new features or fixes without affecting the main branch.

10. Merging Branches

To merge a branch into the main branch:

 git checkout main
 git merge new-branch

Merging incorporates changes from a separate branch into the main branch.

11. Viewing Commit History

To see the commit history of your repository:

 git log

This displays a list of all commits, including their authors, timestamps, and messages.

12. Undoing Changes

To undo modifications to a file before staging:

 git checkout -- filename

To remove a file from the staging area without deleting it:

 git reset filename

To revert the last commit while keeping changes in the working directory:

 git reset --soft HEAD~1

To completely undo the last commit and remove changes:

 git reset --hard HEAD~1

Additional Git Resources

Clone this wiki locally