Skip to content

Git Tutorial

danzio19 edited this page Feb 26, 2024 · 2 revisions

Introduction

Git is a distributed version management system that helps developers track changes to their codebase, collaborate with team members, and work on a project simultaneously.

Installation

For installation, go through this link to the official Git website and download the appropriate installer for your operating system. Run the installer and follow the instructions to complete the installation process. Once it is finished, you can confirm the installation with the command: git --version. You should see the installed Git version displayed in the output.

Configuration

Before you start using Git, you need to configure your name and email address so that Git can associate your commits with your specific username. Run the following commands in your terminal or command prompt:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Git Workflow

Git follows a simple workflow to manage project changes. Here's a basic overview:

  • Initialize a Repository: Use the command git init to initialize a new Git repository in your project directory.
  • Add Files: Use git add to stage files for the next commit. For example, git add filename.txt stages filename.txt for the next commit.
  • Commit Changes: Use git commit to commit staged changes to the repository. A commit records a snapshot of the project at a specific point in time. It includes the changes made to the files and a descriptive message explaining the purpose of the commit. Commit messages help maintain a clear and organized history of the project, facilitating collaboration and tracking the evolution of the project's codebase over time. You can commit the changes made with the following command: git commit -m "Commit message here".
  • View History: Use git log to view the commit history of your repository.

Branches

In Git, a branch is a movable pointer to a commit. It allows you to work on different features or versions of your project simultaneously without affecting the main codebase. Branches serve several purposes in Git development workflows: isolation of work, collabration, and version management. Some common branch-related commands are:

  • Create a Branch: Use git branch branch-name to create a new branch.
  • Switch Branches: Use git checkout branch-name to switch to an existing branch.
  • Merge Branches: Use git merge branch-name to merge changes from one branch into another.

Remote Repositories

Git allows you to collaborate with your teammates by working with remote repositories hosted on platforms such as GitHub. Some remote repository related commands are:

  • Clone a Repository: Use the git clone command to create a local copy of a remote repository and esthablish a connection to it on your local machine: git clone <remote-repository-url>
  • Push Changes: After committing your changes locally, you can push them to the remote repository using git push. For example, git push origin bugfix pushes your committed changes from the bugfix branch to the remote repository.
  • Pull Changes: To fetch and merge changes from the remote repository to your local repository, use git pull. This command combines git fetch (downloads changes from the remote repository) and git merge (integrates the changes into your local branch).
Clone this wiki locally