,

Introduction to Git

In this tutorial, you will learn the basics of using Git, a version control system for tracking changes to files and coordinating work on projects. You will learn how to initialize a repository, add files to the repository, commit changes, view the commit history, compare changes, and push and pull changes from a remote repository. You will also learn how to branch and merge changes, and how to resolve conflicts when merging.

What is Git and why is it useful

Git is a version control system that is widely used for tracking changes to files and coordinating work on projects by multiple people. It is an essential tool for software development and version control, and it is available on most operating systems, including Linux.

Here is a tutorial covering the basics of using the git command:

Initialising a repository with git init

To create a new Git repository, you can use the git init command. This command creates a new directory called .git in the current directory, which stores all the information about the repository, including the history of the changes made to the files.

To initialise a new repository, simply navigate to the directory you want to create the repository in, and type git init, like this:

$ git init

For example, to create a new repository in the /home/user/project directory, you would use the following command:

$ cd /home/user/project
$ git init

Adding files to the repository with git add

To add a file or a group of files to the repository, you can use the git add command. This command tells Git to track the specified files, and to include them in the next commit.

To add a single file to the repository, you can use the git add command followed by the name of the file, like this:

$ git add file.txt

To add multiple files to the repository, you can use the git add command followed by the names of the files, separated by a space, like this:

$ git add file1.txt file2.txt file3.txt

To add all the files in the current directory and its subdirectories to the repository, you can use the git add command followed by the . character, like this:

$ git add .

Committing changes to the repository with git commit

After you have added the files you want to track to the repository, you can commit the changes to the repository using the git commit command. A commit is a snapshot of the repository at a specific point in time, and it includes a message that describes the changes made to the files.

To commit the changes to the repository, you can use the git commit command followed by the -m option, which stands for “message,” and the commit message enclosed in quotation marks, like this:

$ git commit -m "Commit message"

For example, to commit the changes made to the files in the repository with the message “Added new files to the project,” you would use the following command:

$ git commit -m "Added new files to the project"

Viewing the commit history with git log

To view the history of the commits made to the repository, you can use the git log command. This command displays a list of all the commits made to the repository, along with the commit messages, the author, and the date.

To view the commit history, simply type git log and press enter:

$ git log

To display the commit history in a more concise format, you can use the --oneline option with the git log command. This option displays each commit on a single line, along with the abbreviated commit hash and the commit message.

To view the commit history in a concise format, you can use the git log command followed by the --oneline option, like this:

$ git log --oneline

For example, to view the commit history of the repository in a concise format, you would use the following command:

$ git log --oneline

This would display the commit history in the following format:

abbreviated_commit_hash commit_message

For example:

6d9dfb1 Added new files to the project
bc828b7 Initial commit

You can also use the --oneline option in combination with other options, such as --author or --grep, to filter the commit history based on the author or the commit message.

For example, to view the commit history in a concise format, filtered by the author “John Doe”, you would use the following command:

$ git log --oneline --author "John Doe"

Comparing changes between commits with git diff

To compare the changes made to a file between two commits, you can use the git diff command. This command displays the differences between the two commits, and shows which lines have been added, modified, or removed.

To compare the changes made to a file between two commits, you can use the git diff command followed by the commit hashes, like this:

$ git diff commit1 commit2

For example, to compare the changes made to a file called file.txt between the latest commit and the previous commit, you would use the following command:

$ git diff HEAD~1 HEAD

You can also use the git diff command to compare the changes made to a file between the repository and the working directory, by omitting the commit hashes.

For example, to compare the changes made to a file called file.txt between the repository and the working directory, you would use the following command:

$ git diff file.txt

Undoing changes

To undo the changes made to a file, you can use the git reset command. This command resets the file to the state it was in the last commit, and removes it from the staging area.

To undo the changes made to a file, you can use the git reset command followed by the name of the file, like this:

$ git reset file.txt

Checking out a previous commit

To temporarily switch to a previous commit, you can use the git checkout command. This command allows you to view and work with the repository as it was at the time of the specified commit, without modifying the commit history.

To check out a previous commit, you can use the git checkout command followed by the commit hash, like this:

$ git checkout commit

For example, to check out the second-to-last commit in the repository, you would use the following command:

$ git checkout HEAD~1

Branching

Branching is a way to create a separate line of development within the repository. It allows you to work on new features or fixes without affecting the main branch of the repository, and to easily switch between different branches.

To create a new branch, you can use the git branch command followed by the name of the branch, like this:

$ git branch branch_name

For example, to create a new branch called new_feature, you would use the following command:

$ git branch new_feature

To switch to an existing branch, you can use the git checkout command followed by the name of the branch, like this:

$ git checkout branch_name

For example, to switch to the new_feature branch, you would use the following command:

$ git checkout new_feature

Merging branches

To merge the changes made in one branch into another branch, you can use the git merge command. This command combines the changes from the two branches, and creates a new commit that reflects the merged changes.

To merge one branch into another, you can use the git merge command followed by the name of the branch you want to merge, like this:

$ git merge branch_name

For example, to merge the changes made in the new_feature branch into the master branch, you would use the following command:

$ git checkout master
$ git merge new_feature

I hope this tutorial on the git command has been helpful!