How to Push Source Code to GitLab

Cara Upload Source Code ke dalam GitLab


Add Repository GitLab

  • Login ke dalam GitLab > Create a project > Create blank project
  • Masukan project name > Create project > Project repository gitlab sudah berhasil dibuat

Allow Force Push Branch Main

  • Secara default branch main tidak bisa digunakan untuk proses push > Settings > Repository > Protected branches > ceklis Allow to force push pada branch main

Opsional (Push dengan Token)

  • Buat token baru > Account > Preferences > Access Tokens > Add new token > masukan nama token > Ceklis api > Create personal access token > Copy nama token dan kode token pada notepad (nantinya akan digunakan untuk login ke dalam gitlab untuk proses push)

Push Source Code

  • Masuk ke dalam folder dari source code aplikasi yang akan di upload ke gitlab
cd folder-project

ls -al
---<output>---
total 12
drwxr-xr-x 4 root root  81 Aug  6  2023 .
drwxr-xr-x 4 root root  50 Feb 10 08:52 ..
drwxr-xr-x 2 root root  24 Aug  6  2023 css
-rw-r--r-- 1 root root 293 Aug  6  2023 Dockerfile
drwxr-xr-x 2 root root  38 Aug  6  2023 img
-rw-r--r-- 1 root root 975 Aug  6  2023 index1.php
-rw-r--r-- 1 root root 664 Aug  6  2023 index.php
  • Setting global config, ganti user email dan username
git config --global user.email "[email protected]"
git config --global user.name "sys-ops-id"
  • Jalankan intitial branch
git init
---<output>---
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /home/admin/test/web-test/.git/
  • Track/record file git yang akan di push/update
git add .
  • Cek status file pada git
git status
---<output>---
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   Dockerfile
	new file:   css/styles.css
	new file:   img/cat1.png
	new file:   img/cat2.png
	new file:   index.php
	new file:   index1.php
  • Buat branch main (untuk push file ke dalam branch main)
git checkout -b main
---<output>---
Switched to a new branch 'main'
  • Catat perubahan file, berikan deskripsi yang sesuai untuk file yang akan di push dari lokal ke gitlab repository
git commit -m "Upload (Push) Source Code to GitLab"
---<output>---
[main (root-commit) 33dc3d2] Upload (Push) Source Code to GitLab
 6 files changed, 112 insertions(+)
 create mode 100644 Dockerfile
 create mode 100644 css/styles.css
 create mode 100644 img/cat1.png
 create mode 100644 img/cat2.png
 create mode 100644 index.php
 create mode 100644 index1.php
  • Tambahkan remote gitlab repository yang sebelumnya sudah dibuat
git remote add origin https://gitlab.com/sys-ops-id/web-test.git
  • Berikan akses ke git branch main
git branch -M main
git branch -a
---<output>---
* main
  • Push file dari lokal ke gitlab repository
  • Masukan username dan password gitlab
git push -uf origin main
---<output>---
Username for 'https://gitlab.com': <masukan username gitlab>
Password for 'https://[email protected]': <masukan password gitlab>
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (10/10), 516.72 KiB | 17.82 MiB/s, done.
Total 10 (delta 1), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/sys-ops-id/web-test.git
 + b29d1d0...33dc3d2 main -> main (forced update)
branch 'main' set up to track 'origin/main'.
  • Cek status push file pada repository gitlab (semua file source code berhasil di upload)

Add New File

  • Menambahkan file baru dan push ke gitlab
  • Buat file new-file-01.html dan new-file-02.html beserta folder test
cat <<EOF | tee new-file-01.html
file-01
EOF

mkdir test && cat <<EOF | tee test/new-file-02.html
file-02
EOF
  • Track/record file git
git add new-file-01.html test/
  • Git commit
git commit -m "add new file: new-file-01.html and dir test"
---<output>---
[main 9a5a938] add new file: new-file-01.html and dir test
 2 files changed, 2 insertions(+)
 create mode 100644 new-file-01.html
 create mode 100644 test/new-file-02.html
  • Git push
git push -uf origin main
---<output>---
Username for 'https://gitlab.com': <masukan username gitlab>
Password for 'https://[email protected]': <masukan password gitlab>
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 403 bytes | 403.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/sys-ops-id/web-test.git
   33dc3d2..9a5a938  main -> main
branch 'main' set up to track 'origin/main'.
  • Cek status push file pada repository gitlab, new-file-01.html dan new-file-02.html beserta folder test berhasil di upload

Remove File

  • Hapus file new-file-01.html dari lokal dan gitlab repository
  • Hapus file test/new-file-02.html hanya dari gitlab repository saja
git rm new-file-01.html 
---<output>---
rm 'new-file-01.html'

git rm test/* --dry-run
---<output>---
rm 'test/new-file-02.html'

git rm -r test/ --cached
---<output>---
rm 'test/new-file-02.html'

ls -al
---<output>---
total 12
drwxr-xr-x 6 root root 105 Feb 10 09:13 .
drwxr-xr-x 4 root root  50 Feb 10 08:52 ..
drwxr-xr-x 2 root root  24 Aug  6  2023 css
-rw-r--r-- 1 root root 293 Aug  6  2023 Dockerfile
drwxr-xr-x 8 root root 166 Feb 10 09:15 .git
drwxr-xr-x 2 root root  38 Aug  6  2023 img
-rw-r--r-- 1 root root 975 Aug  6  2023 index1.php
-rw-r--r-- 1 root root 664 Aug  6  2023 index.php
drwxr-xr-x 2 root root  30 Feb 10 09:06 test
  • Git commit
git commit -m "remove new file"
---<output>---
[main e67c70b] remove new file
 2 files changed, 2 deletions(-)
 delete mode 100644 new-file-01.html
 delete mode 100644 test/new-file-02.html
  • Git push
git push -uf origin main
---<output>---
Username for 'https://gitlab.com': <masukan username gitlab>
Password for 'https://[email protected]': <masukan password gitlab>
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 220 bytes | 220.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/sys-ops-id/web-test.git
   9a5a938..e67c70b  main -> main
branch 'main' set up to track 'origin/main'.
  • Cek status push file pada repository gitlab, new-file-01.html dan new-file-02.html beserta folder test berhasil di dihapus, tetapi test/new-file-02.html masih ada di lokal

Update File

  • Edit file index.php perhatikan pada gambar, yang di edit pada karakter 2023 menjadi 2024
git add index.php 

git commit -m "update file: index.php"
---<output>---
[main b3844e1] update file: index.php
 1 file changed, 1 insertion(+), 1 deletion(-)

git push -uf origin main
---<output>---
Username for 'https://gitlab.com': <masukan username gitlab>
Password for 'https://[email protected]': <masukan password gitlab>
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 293 bytes | 293.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/sys-ops-id/web-test.git
   e67c70b..b3844e1  main -> main
branch 'main' set up to track 'origin/main'.
  • Cek status push file pada repository gitlab, file index.php berhasil di update

Push to Another Branch

  • Cek posisi status branch saat ini
git branch -a
---<output>---
* main
  remotes/origin/main
  • Buat branch baru dengan nama testing
git checkout -b testing
---<output>---
Switched to a new branch 'testing'

git checkout testing
---<output>---
Already on 'testing'
  • Cek git status pastikan sudah ada pada posisi branch testing
git add .
git status
---<output>---
On branch testing
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   test/new-file-02.html
  • Git commit
git commit -m "Push to Branch testing"
---<output>---
[testing ed2f372] Push to Branch testing
 1 file changed, 1 insertion(+)
 create mode 100644 test/new-file-02.html
  • Berikan akses ke git branch testing
git branch -M testing
git branch -a
---<output>---
  main
* testing
  • Git push ke branch testing
git push -uf origin testing
---<output>---
Username for 'https://gitlab.com': <masukan username gitlab>
Password for 'https://[email protected]': <masukan password gitlab>
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 331 bytes | 331.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
remote: 
remote: To create a merge request for testing, visit:
remote:   https://gitlab.com/sys-ops-id/web-test/-/merge_requests/new?merge_request%5Bsource_branch%5D=testing
remote: 
To https://gitlab.com/sys-ops-id/web-test.git
 * [new branch]      testing -> testing
branch 'testing' set up to track 'origin/testing'.
  • Cek status push file pada repository gitlab branch testing

Git Clone

  • Git clone repository dari branch main
  • git clone https://gitlab.com/acb/xyz.git
git clone https://gitlab.com/sys-ops-id/web-test.git
  • Git clone repository dari branch testing
  • git clone -b testing https://gitlab.com/acb/xyz.git
git clone -b testing https://gitlab.com/sys-ops-id/web-test.git

Re-Sync Remote Repository

  • Update (Sync) file dari GiLab ke lokal repository
git pull --rebase
---<output>---
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 460 bytes | 460.00 KiB/s, done.
From https://gitlab.com/abc/xyz
   b1a54ab..ee8ff7e  main       -> origin/main
Updating b1a54ab..ee8ff7e
Fast-forward
 README.md | 5 +++++
 1 file changed, 5 insertions(+)
 create mode 100644 README.md

Opsional: Error Git Lab

  • Beberapa error yang sering terjadi di gitlab
# Error 1
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.

Solusi: 
Gunakan remote login ke gitlab dengan akses token
git remote add origin https://nama_token:[email protected]/repo/project.git
contoh:
git remote add origin https://web:[email protected]/sys-ops-id/web-test.git

---------------------------------------------------------
# Error 2
error: remote origin already exists.

Solusi: 
cek remote origin, lalu hapus remote origin (jika ingin berpindah ke project yg lain)
git remote -v
git remote remove origin

---------------------------------------------------------
# Error 3
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://gitlab.com/abc/xyz.git'

Solusi: 
Allow Force Push Branch Main

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.