Amina
Amina's Blog

lorem ipsum

May 28, 2024 7 minute read

Git Intro

Table of Contents

  1. What is Git?
  2. Benefits of Git
  3. Getting started with Git

What is Git?

Git is an open source version control system (VCS), which tracks changes in files over a period of time.

Benefits of Git

Using Git offers a wide range of benefits for developers and teams working on software projects.

Here are some of the key advantages:

  1. Version Control:

    Git keeps a detailed history of every change made to a project, allowing developers to understand the evolution of the code. You can revert to previous states of the project if something goes wrong.

  2. Collaboration:

    Multiple developers can work on the same project simultaneously without interfering with each other’s work.

  3. Distributed System:

    Each developer’s copy of the repository is a complete backup of the entire project, reducing the risk of data loss. You can work offline and sync changes later, improving productivity in areas with limited connectivity.

  4. Speed and Performance:

    Git is designed to be fast and efficient, handling large projects and numerous changes with ease. Most Git operations are performed locally, which speeds up the workflow since you don’t need to interact with a remote server for most tasks.

  5. Community and Support:

    Git is widely used in the industry, meaning a large community, extensive documentation, and numerous tutorials are available. It also works well with any programming language and on various operating systems, making it a versatile choice.

Getting started with Git

Terms to know

  • Working directory: this is your workspace where you make changes to files. It holds the current state of your project that Git isn't tracking yet.
  • Staging area: this is where you save information about what will go into your next commit. It's like a draft, which allows you to review and adjust changes before they become part of the project's history.
  • Local repository: this is your project's history, which is stored on your computer.
  • Remote repository: a remote repository is a version of your project hosted on the internet. It allows multiple people to collaborate by pushing to and pulling from the repository.
  • Branches: branches are parallel versions of your project. They allow you to work on features, issues and bugs without affecting the main branch until you are ready to merge them together.
  • Pull request: a pull request is a way to propose changes being added from one branch to another. Here you can review, discuss and then merge the changes into the target branch.
  • Merge: merging combines the history of two branches, creating one single branch.
  • Commit: a commit takes a snapshot of the project's staged changes.

Commit message

After you have added your changes into the staged area, you can commit your code. Each commit is considered a change point or "save point", so you can revert the code back to any version/commit. When you commit, you should always include a message. Clear messages for each commit, make it easy for yourself and others to see which changes were made and when. Usually, the commit message is written in the imperative and present tense.

git commit -m "Initial commit"

Starting a project

You can start a Git project either locally on your computer or remotely on a platform like GitHub.

Creating a new local repository in the current directory.

git init [project name]

Cloning a project from a remote repository.

git clone <project url>

Day-to-day work

Here are a few commands which are used on a daily basis.

For displaying the status of the working area. Output options include new, staged and modified files.

git status

For adding changed files into the staging area.

git add

For creating a new commit (from changes added into the staging area).

git commit -m "Commit message"

For pushing local changes to the remote repository.

git push

For listing the commit history of the current branch.

git log

For giving an overview with reference labels and history graph.

git log --oneline --graph

Branches

Think of the main branch as the trunk of a tree. The trunk is the main part of your project, where the most reliable and ready-to-use code is kept. From this trunk, branches sprout out like the branches of a tree. Each branch represents a separate line of development, allowing you to work on new features, issues and bug fixes independently. These branches can grow, much like how tree branches extend and develop leaves. When the work on a branch is complete and stable, it can be merged back into the trunk.

This tree-like structure in Git keeps the code organized and allows multiple people to work on different parts at the same time, making sure the main code stays strong and reliable.

Git Branches — The Turing Way

Example Workflow

  1. Creating a Feature Branch:

    git checkout -b feature-branch
    
  2. Working on the Feature:

  • Make changes, commit them:

    git add .
    git commit -m "Add new feature"
    
  1. Keeping the Feature Branch Updated:

    git checkout main
    git pull origin main
    git checkout feature-branch
    git merge main
    
  2. Merging the Feature Branch Back to Main:

    git checkout main
    git merge feature-branch
    git push origin main
    
  3. Deleting the Feature Branch:

  • After the feature has been successfully merged and pushed, you can delete the feature branch locally:

    git branch -d feature-branch
    

Storing work

git stash is a command in Git used to temporarily save changes that are not ready to be committed. This allows you to clean your working directory without losing your progress on the changes you're currently making. Here's a breakdown of a few commands and what they do:

Putting current changes from your working directory into stash for later use.

git stash

Displaying all stash entries that are currently available.

git stash list

There are two ways to apply a stashed entry. The difference lies in what happens to the stash after it is applied:

  1. applying stash; entry is deleted from the list

    git stash pop
    
  2. applying stash; entry remains in the list

    git stash apply
    

Deleting the most recent entry from the list, unless there is an identifier as to which stash should be dropped.

git stash drop

Reverting changes

git reset is a command used to undo changes in your Git repository. It has different modes, with --hard and --soft being two of the most common.

Throws away all changes in the working directory and staging area, resetting everything to a specified commit.

git reset --hard <commit>

Resets the current branch to the specified commit but leaves changes in the working directory and staging area as they are. Only the commit history is modified.

git reset --soft [reference]

Displaying changes

These commands are essential to inspect changes and differences in your repository.

Compares changes between commits, branches, or the working directory and the staging area.

git diff

View the details of a commit, including its changes, author, timestamp, and commit message.

git show
Amina