Skip to content

Introduction to Git

genomewalker edited this page Oct 1, 2023 · 1 revision

Git is a distributed version control system that allows you to keep track of changes to your code and collaborate with others on projects. It was created by Linus Torvalds in 2005 to manage the development of the Linux kernel.

In this tutorial, I'll walk you through the basics of using Git to manage your code.

Creating a Git Repository

To start using Git with a project, you need to create a Git repository. A repository is a directory that Git tracks for changes.

To create a new repository, navigate to the directory where you want to store your code and run the following command:

mkdir myrepo
cd myrepo
git init

This will create a new repository in the current directory.

Cloning a Repository

Cloning is the process of creating a copy of an existing Git repository on your local machine. This can be useful if you want to work on an existing project or collaborate with others who have already created a Git repository.

To clone a repository, you'll need the URL of the repository. This can typically be found on the repository's website or in an email invitation from someone who has shared the repository with you.

To clone a repository, navigate to the directory where you want to store the copy of the repository and use the following command:

git clone <repository URL>

This will create a copy of the repository in a new directory with the same name as the repository.

If you want to clone a specific branch of the repository, you can specify the branch name after the URL:

git clone <repository URL> -b <branchname>

Replace <repository URL> with the URL of the repository you want to clone, and <branchname> with the name of the branch you want to clone.

Once you've cloned a repository, you can use the other Git commands we've discussed to make changes, commit them, and collaborate with others on the project.

Adding Files to the Repository

Once you've created a repository, you can start adding files to it. To add a file to the repository, use the following command:

git add <filename>

This will stage the file for the next commit. You can use git add multiple times to stage multiple files.

Making Commits

A commit is a snapshot of the current state of the repository. When you make a commit, you're creating a new version of the code that you can refer to later.

To make a commit, use the following command:

git commit -m "Commit message"

Replace "Commit message" with a short description of what you're committing.

Viewing the History

You can view the history of your commits using the following command:

git log

This will show a list of all the commits in the repository, with the most recent one at the top.

Branching and Merging

One of the most powerful features of Git is its ability to handle branching and merging. Branching allows you to create a new version of the code that's separate from the main branch. You can use this to experiment with new features or make changes without affecting the main codebase.

To create a new branch, use the following command:

git branch <branchname>

Replace <branchname> with the name of the new branch.

To switch to a branch, use the following command:

git checkout <branchname>

To merge a branch into the main codebase, switch to the main branch and use the following command:

git merge <branchname>

Git is a distributed version control system, which means that changes can be made to the codebase by multiple users in multiple locations. In order to collaborate effectively, Git provides a way to connect to remote repositories hosted on other servers, which can be used to share changes between team members.

Adding a Remote

To add a remote repository to your local Git repository, use the following command:

git remote add <name> <repository URL>

Replace with a name you choose for the remote, such as origin, and <repository URL> with the URL of the remote repository.

You can add multiple remote repositories to your local repository by repeating this command with different names and URLs.

Pushing Changes

To push changes from your local repository to a remote repository, use the following command:

git push <remote name> <branch name>

Replace <remote name> with the name of the remote repository you want to push to, and <branch name> with the name of the branch you want to push.

For example, to push changes to the main branch of the origin remote, use the following command:

git push origin main

If you've made changes to multiple branches, you can push them all at once by using the --all flag:

git push <remote name> --all

This will push all the branches in your local repository to the specified remote repository.

Pulling Changes

To pull changes from a remote repository into your local repository, use the following command:

git pull <remote name> <branch name>

Replace <remote name> with the name of the remote repository you want to pull from, and <branch name> with the name of the branch you want to pull.

For example, to pull changes from the main branch of the origin remote, use the following command:

git pull origin main

If you want to pull changes from all branches in the remote repository, use the --all flag:

git pull <remote name> --all

This will pull changes from all branches in the specified remote repository.

Fetching Changes

git fetch is a Git command that is used to download the latest changes from a remote repository without merging them into your local branch. This command updates your local copy of the remote branches and commits, but does not modify your working directory or local branch.

Here's how to use git fetch:

git fetch <remote name>

Replace <remote name> with the name of the remote repository you want to fetch from, such as origin. If you don't specify a remote name, Git will use the default remote repository.

By default, git fetch fetches all branches from the remote repository. However, you can also specify a specific branch to fetch using the following command:

git fetch <remote name> <branch name>

Replace <branch name> with the name of the branch you want to fetch.

After running git fetch, you can review the changes that were made to the remote branch using the following command:

git log <remote name>/<branch name>

This will display the commit history of the specified branch in the remote repository. You can review the changes and decide whether to merge them into your local branch using git merge.

Alternatively, you can also create a new branch based on the fetched changes using the following command:

git checkout -b <new branch name> <remote name>/<branch name>

This will create a new branch with the specified name and set it to the same commit as the remote branch. You can then review the changes and make any necessary modifications before merging them into your local branch.

In summary, git fetch is a useful command for updating your local repository with changes from a remote repository, without automatically merging them into your local branch. This can help prevent conflicts and ensure that you are aware of all changes made to the remote repository.

Difference Between git fetch and git pull

git pull and git fetch are two Git commands that are used to update your local repository with changes from a remote repository. While they are similar in some ways, there are some key differences between them.

git fetch downloads the latest changes from the remote repository, but does not apply those changes to your local branch. Instead, it updates your local copy of the remote branches and commits. This means that you can review the changes before merging them into your local branch.

git pull, on the other hand, downloads the latest changes from the remote repository and applies those changes to your local branch immediately. This means that any changes you have made locally will be overwritten by the changes from the remote branch.

The difference between the two commands can be illustrated by the following scenario: suppose you are working on a local branch and want to update it with changes made to the remote branch by another team member. If you use git fetch, you can see the changes that were made to the remote branch, review them, and then merge them into your local branch manually using git merge. If you use git pull, the changes will be applied automatically, potentially overwriting your own changes.

In general, it's a good idea to use git fetch to update your local repository and review changes before merging them into your local branch. This can help prevent conflicts and ensure that you are aware of all changes made to the remote repository. However, if you are working on a branch that is not being shared with other team members, git pull can be a quick and convenient way to update your local repository with the latest changes.

This is just a brief introduction to Git, but it should be enough to get you started with using it for your projects. As you become more comfortable with Git, you can explore its many other features and commands to improve your workflow and collaborate with others more effectively.