Skip to content

Sde103 git

Introduction

Git is a widely used version control system that allows developers to track changes in their codebase, collaborate with others, and manage project versions efficiently. This tutorial will introduce you to the basics of Git, from installation to common commands.

Prerequisites

Before you begin, make sure you have Git installed on your system. You can download and install Git from here.

Getting Started

Configuring Git

After installing Git, you need to configure it with your name and email address. Open a terminal or command prompt and run the following commands:

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

Replace "Your Name" with your actual name and "your.email@example.com" with your email address.

Initializing a Repository

To start version controlling a project, you need to initialize a Git repository. Navigate to your project directory in the terminal and run:

1
git init

This command initializes a new Git repository in the current directory.

Basic Git Commands

Adding Files

To start tracking changes to files, you need to add them to the staging area. Use the following command to add files:

1
git add <file_name>

Replace <file_name> with the name of the file you want to add. You can also use git add . to add all files in the current directory.

Committing Changes

Once you've added files to the staging area, you can commit them to the repository. Commits are snapshots of your project at a specific point in time. Use the following command to commit changes:

1
git commit -m "Commit message"

Replace "Commit message" with a brief description of the changes you've made.

Checking Status

To check the status of files in your repository and see which ones are staged for commit, use the following command:

1
git status

This command provides information about modified, staged, and untracked files.

Viewing Commit History

You can view a log of all commits in your repository along with their details using the following command:

1
git log

This command displays a list of commits, showing the commit hash, author, date, and commit message.

Working with Remotes

Adding a Remote Repository

To collaborate with others or backup your code, you can push your local repository to a remote repository hosted on platforms like GitHub or GitLab. Use the following command to add a remote repository:

1
git remote add origin <remote_url>

Replace <remote_url> with the URL of your remote repository.

Pushing Changes

Once you've added a remote repository, you can push your commits to it using the following command:

1
git push -u origin master

This command pushes your local commits to the remote repository's master branch.

Pulling Changes

To fetch changes from a remote repository and merge them into your local branch, use the following command:

1
git pull origin master

This command pulls changes from the remote repository's master branch and merges them into your current branch.

Amending the Previous Commit

To add the left-out file to the previous commit without changing its commit message, you can follow these steps using git add . && git commit --amend --no-edit:

Add the Left-out File: First, add the file you left out to the staging area using the git add command:

1
git add the_left_out_file

Amend the Previous Commit: Next, use the git commit --amend --no-edit command to amend the previous commit without changing its commit message:

1
git commit --amend --no-edit

The --amend option allows you to modify the previous commit, and --no-edit ensures that the commit message remains unchanged.

Branching in Git

Branching is a powerful feature in Git that allows developers to work on multiple independent lines of development within the same repository. It enables you to isolate changes, experiment with new features, and collaborate effectively with team members. Let's explore some common branching operations:

Creating a Branch

To create a new branch in Git, you can use the git branch command followed by the desired branch name:

1
git branch <branch_name>

This command creates a new branch with the specified name based on the current commit.

Switching Branches

You can switch between branches using the git checkout command followed by the name of the branch you want to switch to:

1
git checkout <branch_name>

This command allows you to navigate between different branches within your repository.

Creating and Switching to a New Branch

To create a new branch and switch to it in a single step, you can use the -b option with the git checkout command:

1
git checkout -b <new_branch_name>

This command creates a new branch with the specified name and switches to it immediately.

Viewing Branches

To view all branches in your repository and see which branch you're currently on, you can use the git branch command:

1
git branch

The branch with an asterisk (*) next to it indicates the current active branch.

Merging Branches

Once you've completed work on a feature or fix in a separate branch, you can merge it back into the main branch (e.g., master) using the git merge command:

1
2
git checkout master
git merge <branch_name>

This command merges the changes from the specified branch into the current branch (in this case, master).

Deleting a Branch

After merging a branch into another branch, you may want to delete the feature branch. You can do this using the git branch -d command:

1
git branch -d <branch_name>

This command deletes the specified branch. Use caution when deleting branches, as it's permanent.

Branching is a fundamental aspect of Git that empowers developers to work efficiently on collaborative projects. By understanding how to create, switch, merge, and delete branches, you can effectively manage your codebase and streamline your development workflow.

Conclusion

Congratulations! You've learned the basics of Git, including initializing a repository, committing changes, working with remotes, branching, and more. Git offers a powerful set of tools for version control and collaboration, making it an essential tool for developers. Practice using Git regularly