- What you need
- What is Git?
- Find more Git learning resources
This tutorial is going to teach you a little bit about how Git works. It walks you through the steps of creating your own project, editing a file, and committing changes to a Git repository from the command line.
When you’re done, you’ll have a project where you can practice using Git.
Before you begin:
- Install Git on your local machine.
- Ensure you can sign in to an instance of GitLab. If your organization doesn’t have GitLab, create an account on GitLab.com.
- Create SSH keys and add them to GitLab. SSH keys are how you securely communicate between your computer and GitLab.
Before we jump into steps, let’s go over some basic Git concepts.
Git is a version control system. It’s used to track changes to files.
You store files, like code or documents, in a Git repository. When you want to edit the files, you clone the repository to your computer, make the changes, and push your changes back to the repository. In GitLab, a Git repository is located in a project.
Each time you push a change, Git records it as a unique commit. These commits make up the history of when and how a file changed, and who changed it.
When you work in a Git repository, you work in branches. By default, the contents of a repository are in a default branch. To make changes, you:
- Create your own branch, which is a snapshot of the default branch at the time you create it.
- Make changes and push them to your branch. Each push creates a commit.
- When you’re ready, merge your branch into the default branch.
If this all feels a bit overwhelming, hang in there. You’re about to see these concepts in action.
Here’s an overview of what we’re going to do:
- Create a sample project.
- Clone the repository.
- Create a branch and make your changes.
- Commit and push your changes.
- Merge your changes.
- View your changes in GitLab.
To start, create a sample project in GitLab.
- In GitLab, on the top bar, select Menu > Projects > Create new project.
- Select Create blank project.
- For Project name, enter
My sample project. The project slug is generated for you. This slug is the URL you can use to access the project after it’s created.
- Ensure Initialize repository with a README is selected. How you complete the other fields is up to you.
- Select Create project.
Now you can clone the repository in your project. Cloning a repository means you’re creating a copy on your computer, or wherever you want to store and work with the files.
On your project page, select Clone. Copy the URL for Clone with SSH.
Open a terminal on your computer and go to the directory where you want to clone the files.
git cloneand paste the URL:
git clone email@example.com:gitlab-example/my-sample-project.git
Go to the directory:
By default, you’ve cloned the default branch for the repository. Usually this branch is
main. To be sure, get the name of the default branch:
The branch you’re on is marked with an asterisk. Press
Qon your keyboard to return to the main terminal window.
Now that you have a copy of the repository, create your own branch so you can work on your changes independently.
Create a new branch called
git checkout -b example-tutorial-branch
In a text editor like Visual Studio Code, Sublime,
vi, or any other editor, open the README.md file and add this text:
Hello world! I'm using Git!
Save the file.
Git keeps track of changed files. To confirm which files have changed, get the status.
You should get output similar to the following:
On branch example-tutorial-branch Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md no changes added to commit (use "git add" and/or "git commit -a")
You’ve made changes to a file in your repository. Now it’s time to record those changes by making your first commit.
README.mdfile to the staging area. The staging area is where you put files before you commit them.
git add README.md
Confirm the file is staged:
You should get output similar to the following, and the filename should be in green text.
On branch example-tutorial-branch Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: README.md
Now commit the staged file, and include a message that describes the change you made. Make sure you surround the message in double quotes (“).
git commit -m "I added text to the README file"
The change has been committed to your branch, but your branch and its commits are still only available on your computer. No one else has access to them yet. Push your branch to GitLab:
git push origin example-tutorial-branch
Your branch is now available on GitLab and visible to other users in your project.
Now you’re ready to merge the changes from your
to the default branch (
Check out the default branch for your repository.
git checkout main
Merge your branch into the default branch.
git merge example-tutorial-branch
Push the changes.
You did it! You updated the
README.md file in your branch, and you merged those changes
Let’s look in the UI and confirm your changes. Go to your project.
- Scroll down and view the contents of the
README.mdfile. Your changes should be visible.
README.mdfile, view the text in the Last commit column. Your commit message is displayed in this column:
Now you can return to the command line and change back to your personal branch
git checkout example-tutorial-branch). You can continue updating files or
creating new ones. Type
git status to view the status
of your changes and commit with abandon.
Don’t worry if you mess things up. Everything in Git can be reverted, and if you find you can’t recover, you can always create a new branch and start again.