How do I use Git in Ubuntu

Learn Git in 30 Minutes - Beginners Tutorial (2021)


The use of Git has exploded in the last few years. The popular version control system is used by large open source projects like Linux, developer teams, solo developers, and even students.

Beginners find it difficult at first, as Git brings many new and sometimes cryptic commands with it.

In this extensive git tutorial you will learn the version control system from scratch. A big advantage of Git is that you don't know everything to get started with. You can start immediately as soon as you know 2-3 commands. You can then gradually build up your knowledge.

You don't need to work through the entire guide in one day. By the way, if you want to get into practice right away, I recommend our Git quick start course. If you just want a Git cheat sheet with all of the Git commands you can download the LearnProgramming Git Cheatsheet for free in our members area.

Here we go!

What is git

Git is a suite of command line utilities that track and record changes in files (mostly source code, but you can "track" all kinds of files such as text files and even image files).

This functionality allows you to restore old versions of your project, compare them with one another, analyze them, merge changes and much more.

This process is called Version control designated. There are a number of version control systems that do this job. You may have heard of some of them by now - Perforce, Mercurial, CVS, SVN, and more.

Git is decentralized, that means it doesn't depend on a central server to keep old versions of your files. Instead, it works entirely locally by storing this data as a folder on your hard drive. We also call that Repository.

However, you can Copy of your repository save online. This makes it easier for teams to collaborate and work on the same code. Usually external Git services such as GitHub and BitBucket are used for this.

1. Install Git

In order to work with Git, you must first install it on your computer. Here are simple instructions for the most common operating systems:

  • Windows - we recommend Git for Windows as it offers both a GUI client and a BASH command line emulator.
  • Linux - Just open a new terminal and install Git using the package manager of your Linux distribution. For example, for Ubuntu the command is sudo apt-get install git
  • Mac OS X - The easiest way is to install homebrew and then just start brew install git from your terminal.

If you don't want to work with Git on the command line, I can recommend client programs like GitHub Desktop and SourceTree (from Atlassian). However, there are many other Git programs that can be used free of charge. Even if you decide to use a GUI application, you should still know the most important Git commands.

In this Git tutorial, we will mainly focus on the command line, as this is the easiest way to learn the Git commands (learning by doing).

2. Configure Git

Now that we've installed git on our computer, we still need to add a few configurations. There are a ton of options available to you (e.g. the appearance and functionality of the client), but we're only going to do the two most important configurations here. On the one hand we set our username and on the other hand our email.

To do this, open the program "Git Bash" and execute these commands:

git config --global "Daniel" git config --global [email protected]

Every action we do in Git will now have a stamp with the name and email address just given. This way, other users always know who did what and when. This brings order out of chaos, especially in large projects that many developers are working on.

3. Create a new repository - git init

As we mentioned earlier, git stores its files and history directly as a folder in your project. To set up a new repository we need to open a terminal, navigate to our project directory and run git init. This will enable Git for that particular folder and a hidden one .git directory which will store the history and configuration of the repository.

Create a folder on your desktop called "learn git", open a new git terminal and type:

cd git-learn git init

The command line should produce something like the following:

Or as text:

Initialized empty Git repository in C: /Users/schwe/git-lernen/.git/

This means that our repository was created successfully. Of course it's still empty at the moment. So create a simple text file called lerneprogrammieren.txt and save it in the folder "learn git".

It should look something like this:

Create a text file with the name "lerneprogrammieren.txt" in the "git-Learn" folder

4. Check the status of the repository - git status

Git status is another important command that you need to know. It returns information about the current status of the repository.

  • Is everything up to date?
  • What's new?
  • What has changed?
  • etc.

Execute the status command as follows:

git status

Running git status in our newly created repository it should return something like:

Or as text output:

On branch master No commits yet Untracked files: (use "git add ..." to include in what will be committed) lerneprogrammieren.txt nothing added to commit but untracked files present (use "git add" to track)

The message returned says that lerneprogrammieren.txtis not tracked (more precisely, not tracked). This means that the file is new and Git doesn't yet know whether to track changes to the file or just ignore them.

To confirm the new file, we need to "stage" it.

5. Staging - git add

Git knows the concept of a "Staging AreaYou can think of this as a blank canvas containing the changes you would like to commit. At the beginning your staging area (or staging area) is empty, but you can use the command git add Add files (or even individual lines and parts of files) and finally with git commit commit everything (take a snapshot).

In our case we only have one file. Let's add these:

git add lerneprogrammieren.txt

If you want to add everything in the directory, use the command like this:

git add -a

When you have executed the above command, check the status again with git status. You should then see the following:

git status


Or as text:

On branch master No commits yet Changes to be committed: (use "git rm --cached ..." to unstage) new file: lerneprogrammieren.txt

Our file is now in the staging area. The status message also tells us what has changed in the files in the staging area - in this case, one is new file added. Further status messages can also relate to the modification or deletion of files.

6. Create a commit - git commit

A commit represents the state of our repository at a specific point in time. It's like a snapshot that we can go back to. With this we can see what the repository looked like "back then" when we took the snapshot.

In order to create a new commit we need to have added at least one change in the staging area (we just did that with git add made). Now create your first commit as follows:

git commit -m "My first commit"


Or as text:

[master (root-commit) c78793a] My first commit 1 file changed, 0 insertions (+), 0 deletions (-) create mode 100644 lerneprogrammieren.txt

This will create a new commit with all changes from the staging area (adding the lerneprogrammieren.txt). The option -m with the text "My first commit" is a custom description that summarizes the changes to the commit.

It is considered good practice to commit fairly often and to write commit messages that are as specific as possible.

7. Remote repository

At the moment our commit is local - it only exists in the .git Folder. Although a local repository is useful in itself, in most cases we will want to share our work with other developers and host it on a server or on repository hosting (such as Github or Bitbucket).

7.1 Establishing a connection to a remote repository - git remote add

To upload something to a remote repo, we need to connect to it first.

For the purposes of this tutorial, the address of our repository will be used used. We recommend creating your own empty repository on GitHub, BitBucket, or another service. Registering and setting up the repo can take a while, but all services have good step-by-step instructions to help you create one.

To link our local repository to the one on GitHub, we run the following line in the terminal:

(Note: Please replace the URL with your own remote repository URL)

git remote add origin

A project can have multiple remote repositories at the same time. In order to be able to distinguish them, we give them different names. In practice, the main remote repository is usually with the word "origin" (Origin).

7.2 Transferring changes to the remote server - git push

Now is the time to push our local commits to the server. This process will Push and is done every time we want to update the remote repository.

The git command for this is git push. He needs two parameters - 1) the name of the remote repo (we have our repo "origin" called) and 2) the Branchto be pushed (master is the standard branch for every repo).

git push origin master


Or as text:

Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 238 bytes | 238.00 KiB / s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To * [new branch] master -> master

Depending on the remote service you are using, you may have to log in beforehand so that the push can be carried out. If everything was done right, the lerneprogrammieren.txt be available there when you call up the previously created remote repository in your web browser. In our case we can check the "push" at this URL:

Our first push to the GitHub remote repository

7.3 Retrieve changes from the remote server - git pull

When you make updates in your repository, other developers can download your changes with a single command - git pull.

(In the following example I have created the "test.txt" file. We will now "pull" it from the remote repository)

git pull origin master


Or as text:

remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), 675 bytes | 39.00 KiB / s, done. From * branch master -> FETCH_HEAD c78793a..4f9a390 master -> origin / master Updating c78793a..4f9a390 Fast-forward test.txt | 1 + 1 file changed, 1 insertion (+) create mode 100644 test.txt

8. Clone a repository - git clone

At this point, team members can see and search your remote repository on Github. You can download it locally and use the command git clone make a fully functional copy of your project:

git clone

A new local repository will be created automatically with the Github version configured as remote.

9. Branches

When developing a new feature, it is considered good practice on one Copy of the original project to work that as Branch referred to as. Branches have their own history and isolate their changes from one another until a decision is made to merge them again. This happens for several reasons:

  • An already working, stable version of the code will not be affected by undesired bugs
  • Many functions can safely be developed by several developers at the same time
  • Developers can work on their own branch without the risk that their code base will change due to the work of another developer.
  • If you're not sure what is best, multiple versions of the same feature can be developed on different branches and then compared.

9.1 Create branches - git branch

The default branch of any repository is called master. To create additional branches, use the command git branch :

git branch feature1

With the above command you create the branch "feature1", which (still) resembles the "master" branch.

(Note: You will not see any further messages after you have successfully created the branch)

9.2 Switching branches - git checkout

We are currently still in the "master" branch. But now we want to switch to our newly created branch "feature1". This is done with the command git checkout, where one parameter is expected, namely the branch name we want to switch to.

git checkout feature1

9.3 Merge branches - git merge

Our new feature (feature1) will just be another text file called button.txt be. We'll create, add, and commit them:

git add button.txt git commit -m "New feature ready"

The new feature is complete, we can switch back to the master branch:

git checkout master

Oh no! Where did our "button.txt" suddenly go? It has disappeared from the file system without a trace.

No panic.

This is because we are back in the master branch, and here it was the feature.txt never created. To add them in the master branch, we need to merge both branches and the changes made in "feature1" apply to the major version of the project. Sounds complicated, but it's not. Again, you only need one line to perform the merge process:

git merge feature1


The master branch is now up to date. The "feature1" branch is no longer needed and can be removed.

git branch -d feature1


Git for advanced users

In the first part of the Git tutorial, we covered the basics. In this part, we're going to take a look at some of the more advanced features of Git.

10. Check the difference between commits

Each commit has a unique ID in the form of a series of numbers and symbols (SHA). To see a list of all commits and their IDs, we can git log to use:

git log


Or as text:

commit b32146bcab79afebc73fb9e9fd4f2368ce00b052 (HEAD -> master) Author: Daniel Schwede Date: Fri Aug 7 02:44:17 2020 +0200 New feature ready commit 4f9a390b382aa179767e13fde866d8dede6. de> Date: Fri Aug 7 02:32:53 2020 +0200 Create test.txt commit c78793a8d226e5a4a2bfe7ebbca15b4cedfd8317 Author: Daniel Schwede Date: Fri Aug 7 02:14:37 2020 +0200 My first commit

As you can see, they are ID's very long, but you don't need to type it in. You can highlight them with the cursor to copy them to the clipboard. Alternatively, the first few characters are sufficient for Git to assign the correct commit.

To see what was in a commit we can use the git show to use:

git show b32146bc


Or as text:

commit b32146bcab79afebc73fb9e9fd4f2368ce00b052 (HEAD -> master) Author: Daniel Schwede Date: Fri Aug 7 02:44:17 2020 +0200 New feature finished diff --git a / button.txt b / button.txt new file mode 100644 index 0000000..2e2ff08 --- / dev / null +++ b / button.txt @@ -0,0 +1 @@ + I'm a button \ No newline at end of file

In the output above, we get a lot of information about the commit. You get information about the author, a time stamp, the commit message and all kinds of file information, such as the added lines within the text file ("i am a button") is displayed.

11. Revert a file to a previous version

Git allows us to restore any selected file to the way it was in a particular commit. This is done via the well-known Git checkout command that we used earlier to switch branches, but which can also be used to switch between commits (it is quite common in Git that one command is used for several apparently non-contiguous Tasks is used).

In the following example we take the text file lerneprogrammieren.txt and undo everything we've done with it since the first commit. To do this, we need to provide the id of the commit we want to go back to and the full path to our file. You can get the respective ID via git log. Then execute the following command:

git checkout b32146bcsab79a lerneprogrammieren.txt

12. Undo a commit - git revert

If you notice that you made a typo in your commit message or you forgot to add a file, you can fix the error with git commit --amend remedy.

This will add everything from the last commit back to the staging area. An attempt is also made to make a new commit. This gives you the option to touch up your commit message or add more files to the staging area.

For more complex fixes that aren't in the last commit (or if you've already pushed your changes), you'll need to git revert to use. This will undo any changes that were committed and create a new commit.

The latest commit can be accessed via the alias HEAD can be accessed.

git revert HEAD

For other commits, it is best to use an ID. You can find the ID of the commit via the git log Command. Then use the following git revert Command:

git revert

When rolling back older commits, keep in mind that merger conflicts are very likely to arise. This happens when a file has been modified by another recent commit and now Git can't find the correct lines to roll back because they no longer exist.

13. Ignore files with .gitignore

In most projects there are files or entire folders that we never want to commit. We can ensure that they are not accidentally included in our git add -A by adding a .gitignore file create:

  1. Manually create a text file called .gitignore and save it in the directory of your project.
  2. Lists the names of the files / directories to be ignored, each on a new line.
  3. The .gitignore itself needs to be added, committed and pushed as it is just like any other file in the project.

Good examples of files to ignore are:

  • log files
  • task runner builds
  • the node_modules folder in node.js projects
  • folders used by IDEs such as Netbeans and IntelliJ
  • personal notes from the developers

A .gitignore that prohibits all of the above will look like this:

  • *.log
  • build /
  • node_module /
  • .idea /
  • myComments.txt

The slash at the end of some lines indicates that this is a folder and that we have everything in it ignore recursively. The asterisk serves its usual function as a placeholder (wildcard).

Conclusion on the "Learning Git" tutorial

If you've made it this far in the tutorial, then respect! If you have worked through all of the individual sections, you have learned Git. You can always use this tutorial as a reference if you can't think of a Git command or if you need a simple reference work.

I can also give you our free Git cheat sheet recommend. All Git commands are briefly listed there. You can find it in the LerneProgrammieren member area.

In this guide I have tried to convey the most important information about the version control system. Git can seem quite complex and has a lot more features and tricks to offer.

At this point you have already learned 90% of the most important functions and commands about Git.