Book Image

GitLab Cookbook

By : Jeroen van Baarsen
Book Image

GitLab Cookbook

By: Jeroen van Baarsen

Overview of this book

Table of Contents (16 chapters)
GitLab Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Cloning your repository and pushing code to it


In this recipe, we will take a look at cloning your repository from GitLab to your local machine. When you use Git Bash on Windows, the commands will be the same.

Getting ready

You need to create a new repository in your GitLab instance. In this example, we will use the repository named super-git.

How to do it…

In the following steps, we will set up our repository and push code to it:

  1. Go to the newly created repository.

  2. Select the URL in the top-right section.

  3. Go to the folder where you want to check out the project in the terminal. No need to create a new folder for the project.

  4. Enter the Git clone command, and change the URL to the URL you just copied:

    $ git clone URL
    

    In my case, the URL is as follows:

    $ git clone [email protected]:jeroen/super-git.git
    

    The following screenshot shows you the output of this command:

  5. You can now go to the folder and check whether it is a Git folder by running the following command:

    $ git status
    

    The following screenshot shows you the output of this command:

  6. Now, we create a change that we can commit:

    $ echo "Hello GitLab" >> README.md
    
  7. Next, we add this file to the stage. We can do this by running the following command:

    $ git add README.md
    
  8. To commit the change, we run the following command:

    $ git commit -m "Added readme file"
    
  9. Now, we have the commit in our local Git repository, but to send it to the GitLab server, we have to enter another command:

    $ git push -u origin master
    

    The following screenshot shows you the output of this command:

  10. When we take a look at our GitLab instance in the following screenshot, we see that the code has been sent here:

How it works…

Git works as a decentralized repository system, which means that there is no central server on which you can store your code. Instead, every user has their own local copy of the entire Git project. If you want to move your code to another server, let's say GitLab, you have to tell GitLab where that server is.

If you have created a new project, you can do this easily by cloning the repository like we just did. However, in case you already have the repository on your local system, you can tell GitLab where the server is by pointing it to the existing GitLab repository.

You can add a new remote by executing the git remote add NAME URL command, for example, git remote add origin [email protected]:example/example.git.

The remote address is the URL that Git is using to push the code to. It's a convention that uses the origin as the main repository's URL.

When you clone a repository, the name of the remote will be origin. So, when we try to push our local commits to the GitLab server, we have to tell it where we want the commits to go. We do this by running git push –u origin master. origin is the remote server, and master is the branch we want to push a commit to.