Skip to main content

Workflow using git

Install git on your machine:

See Git installation guide.

Create and setup your gitlab account:

To create a gitlab acount see Gitlab Sign Up.

In the settings tab add your SSH key to your gitlab account. If you don't have a ssh key see Learn how to create a SSH key.

Follow the workflow

Clone the remote repository into a new local directory.

	[user@fedora ~]$ mkdir my_repo 
	[user@fedora ~]$ cd my_repo
    [user@fedora my_repo]$ git clone git@git01.ncg.ingrid.pt:lip-computing/project_name.git

Create a new branch to work on a new feature. The branch is named new_feature in the example bellow.

	[user@fedora my_repo]$ cd project_name
    [user@fedora project_name]$ git branch new_feature
	[user@fedora project_name]$ git checkout new_feature 
	Switched to branch 'new_feature'  

Push your changes to the remote repository. In the following example, new_feature.py is the file that contains the code for the new feature.

    [user@fedora project_name]$ git add new_feature.py
    [user@fedora project_name]$ git status
    On branch new_feature
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        new file:   new_feature.py

At this point your changes were added to the staging area. First commit your changes and push them to the remote repository so that other team members can review them.

    [user@fedora project_name]$ git commit -m "<your_commit_message>"
    [new_feature f8ebb26] <your_commit_message>
 	Author: User <user@lip.pt>
 	1 file changed, 1 insertion(+), 1 deletion(-)  
    [user@fedora project_name]$ git push origin new_feature
    Enumerating objects: 7, done.
    Counting objects: 100% (7/7), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (6/6), 625 bytes | 625.00 KiB/s, done.
    Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
    remote: 
    remote: To create a merge request for new_feature, visit:
    remote:   https://git01.ncg.ingrid.pt:user/lip-computing/project_name/-/merge_requests/new?merge_request%5Bsource_branch%5D=new_feature
    remote: 
    To git01.ncg.ingrid.pt:user/lip-computing/project_name.git
     * [new branch]      new_feature -> new_feature
    

Your branch is now available in the remote repository. From the dashboard you can create a merge request and assign a team member to review your code.

Once your code has been reviewed, all the changes to your code have been performed and the final version has been approved, your branch can be merged to the master branch.

You can now update your local repository to the latest state of the remote repository and work on another feature repeating the same steps.

    [user@fedora project_name]$ git fetch
    remote: Enumerating objects: 1, done.
    remote: Counting objects: 100% (1/1), done.
    remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (1/1), 250 bytes | 250.00 KiB/s, done.
    From git01.ncg.ingrid.pt:user/lip-computing/project_name
       113c798..e490c8f  master     -> origin/master
    [user@fedora project_name]$ git merge -X theirs origin/master 
    Updating f8ebb26..e490c8f
    Fast-forward
    
Manage conflicts

A conflict arises if the state of the remote repository changed while you were working on your local repository. This means you don't have the latest state of the remote repository on your local machine.

[user@fedora test]$ git push origin new_feature
To git01.ncg.ingrid.pt:user/lip-computing/project_name.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://git@git01.ncg.ingrid.pt:lip-computing/project_name.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.