Git Tutorial: Learn how to use Version Control

Updated onbyAlan Morel
Git Tutorial: Learn how to use Version Control

Git is an important tool to know and use as developers. Git offers us version control, which allows us to track changes to our code, share code with others, revert changes, and more. Git is designed to be a fast, scalable, and distributed across both local and remote machines. Git is open-source, free, and very popular. In this tutorial, we will learn how to use Git and how to get started with it, including how to create a repository, how to add, commit, and push to a repository, how to work with branches, and how to use the most popular commands.

Installing Git

Installing Git is easy. Here is how to install Git based on your operating system.

  • Windows: Download the Git for Windows installer and run it.
  • Mac: Download the Git for Mac and run it.
  • Linux: Use your distribution's package manager to install Git. For Ubuntu, run sudo apt-get install git.

Configuring Git

There are many ways to configure Git. A common way is to create a global configuration file called .gitconfig and store your configuration in that file. However, this is not the only way to configure Git. You can also configure Git using the command line. For example, to set the user name and email address, you can run the following command:

git config --global user.name "Your Name"
git config --global user.email "Your email address"

In doing this, every action we do using Git will be logged in the Git history using this information. This helps us to track our progress and identify who did what.

Creating a Repository

Now that we've installed and configured Git, we can create a repository. A repository is a directory that contains all of the files and folders that you want to track.

To create a repository, we can use the command git init. This command will create a directory called .git in the current directory. The .git directory will contain a everything that Git needs to function, including a file called HEAD that contains the current branch that you're on, the project-specific configuration file, and the history of all the commits.

Use the command git init to create a repository.

git init
Initialized empty Git repository in /projects/git/.git/

If you see that message, then you have successfully initialized a Git repository.

Checkout a Repository

If you don't want to create a repository, you can check out an existing one. This is useful if you want to work on a project that you already have a repository for. To do this, we can use the command git clone. This command will clone the repository into the current directory. You just need to specify the URL of the repository to clone. Here's an example:

git clone /path/to/repository.git

If you are cloning from a remote server, you can specify a username and hostname to authenticate with. Here's an example:

git clone username@host:/path/to/repository.git

Status

Now that you have a repository, you can use the command git status to see what files have been changed. This command will tell you what files have been changed, what files have been added, and what files have been deleted. It is useful to know what files have been changed, so that you can decide what to commit.

Here's an example output of using git status:

On branch master

No commits yet

As you make changes to your files, Git will show you what files have been changed.

Adding Files to a Repository

When you have made changes to a file, you can add it to the repository using the command git add. This will add the file to the staging area. The git add command takes in a file path as an argument, however it supports wildcards.

Here's how you can add all files to staging:

git add .

Here's how to add all files from a specific file extension:

git add *.txt

Here's how to add all files from a specific directory:

git add src/

Here's how to add a specific file:

git add src/file.txt

After you've added a file to staging, you will see that the output of git status has changed. Let's say you have a file called README.md in your repository.

On branch master

Initial commit

Changes to be committed:
(use "git rm --cached ..." to unstage)

    new file:   README.md

Committing Files to a Repository

When you have added files to the staging area, you can commit them to the repository using the command git commit. A commit is a snapshot of the files in the repository at a specific point in time. This will create a new commit that contains the files that you added. The commit message is optional, but it is a good idea to include a message that describes what you did. Here's an example:

git commit -m "Add new README.md"

This will create a commit that contains all your staged files, in this case, README.md. The message of the commit will be Add new README.md. This will be committed to the HEAD of the master branch, the latest commit on the branch.

Adding a Remote Repository

Before we can push our commits to the remote repository, we need to add a remote repository. This allows us to connect to a remote server that will accept our commits. To do this, we can use the command git remote add. This command will add a remote repository for us to interact with.

To add a remote repository, we need to specify the name of the remote repository and the URL of the remote repository. Here's an example:

git remote add origin

Here's an example using GitHub:

git remote add origin https://github.com/AlanMorel/sabe.git

The default remote repository name is origin. However, you can change the name of the remote repository by specifying the name as the second argument. The last argument is the URL of the remote repository.

Pushing to a Repository

When you have committed files to the repository, you can push them to the remote repository. This will push the commit to the remote repository. To push to the remote repository, you can use the command git push. Before pushing, your changes are just on your local copy of HEAD. When you have all the commits ready, you can push them to the remote repository so others can see them.

Here's how to push your changes to a remote repository:

git push origin master

You can replace master with the name of the branch you want to push to. You can also replace origin with the name of the remote repository.

Pull from a Repository

When you push your changes to the remote repository, you can also pull those changes from the remote repository. This will pull the changes from the remote repository and merge them into your local repository. To pull from the remote repository, you can use the command git pull.

Here's how to pull from a remote repository:

git pull origin master

If you don't specify a branch name, Git will pull from the branch that you are currently on, like so:

git pull

Creating a Branch

An important concept to understand is branching. A branch is a specific version of a repository. This is useful for isolating changes that you want to make to a repository, like features or bug fixes. The default branch is called master or main. However, you can create as many branches as you want.

Here's how to create a new branch called updates:

git checkout -b updates

The -b flag tells Git to create a new branch. The -b flag is followed by the name of the branch.

Switching Branches

You can switch between branches using the command git checkout. This will switch to the branch you specify. Before you do so, you can use git branch to see all the branches that you have.

git branch
* master
updates

The asterisk indicates that you are currently on the branch master. To switch to the branch updates, you can use the command git checkout updates.

Deleting Branches

Branches in Git are not permanent. You can delete a branch by using the command git branch -d. This will delete the branch from your local repository.

git branch -d updates

You can also delete a branch from the remote repository. To do this, you need to specify the remote repository name as the first argument.

git push origin --delete updates

Merging

When you have multiple branches, you can merge them together. This is useful for combining multiple branches into a single branch or taking updates from another branch. To merge a branch in to your current one, you can use the command git merge.

git merge <branch>

Unfortunately, sometimes during a merge, Git will ask you to resolve conflicts. This is usually because you have made changes to the same file in both branches. Git will ask you to resolve the conflicts by choosing which changes to keep and which changes to discard. This process is called a merge conflict.

After you have resolved the conflicts, you can commit the changes to the repository.

Stash

Git has a feature called Stash. Stash is a way to save changes that you have made to a file and then restore them later. This is useful when you want to make changes to a file, but you don't want to commit those changes yet. Stashing creates a temporary space in your repository where you can store your changes.

To stash changes, you can use the command git stash. This will create a new stash in your repository.

git stash

You can add a name to your stash for easier identification. Here's how to add a name to your stash:

git stash save "cool work"

To see all your stashes, you can use the command git stash list.

git stash list

When you want to add your changes back, you can use the command git stash apply. This will apply the stash to your repository.

git stash apply stash@{stash_index}

To delete all your stashes, you can use the command git stash clear, or delete individual stashes using the command git stash drop and passing the stash index as the argument.

Git Log

Git provides a command called git log that will show you all the commits that have been made to the repository. This is useful for seeing what changes have been made to the repository and when they were made.

git log

This command supports several different flags however. If you want to see commits from a specific author, use the --author flag.

git log --author "Alan Morel"

If you want to see the Git log in a compressed format, you can use the --pretty flag.

git log --pretty=oneline

If you want to see the Git log in a graph format, you can use the --graph flag.

git log --graph

If you want to see the Git log that shows the name of the files that changed, use the --name-status flag.

git log --name-status

This command alone is very powerful so definitely check it out yourself.

Restoring files

If you've made changes that you want to discard, you can use the command git restore. This will restore files you have deleted or modified. Simply use the command git restore and the name of the file you want to restore.

git restore <file>

You can restore all files by using the command git restore .. This will restore all files in the current directory.

git restore .

Conclusion

Knowing how to use Git properly can make you a better and more versatile developer. Git is powerful and complex so hopefully this tutorial has helped you get comfortable with it. If you wish to learn more, check out the resources linked below.

Resources

Recommended Tutorial »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.