Tags Archives: git

Using the gh Command with GitHub

The gh command enables you to work more speedily with GitHub.

 

 

To create a new GitHub repository:

 

gh repo create

 

gh repo create [<name>] [flags]

 

 

 

To create a repository interactively, use gh repo create with no arguments.

 

 

To create a remote repository non-interactively, supply the repository name and one of –public, –private, or –internal. Pass –clone to clone the new repository locally.

 

To create a remote repository from an existing local repository, specify the source directory with –source. By default, the remote repository name will be the name of the source directory. Pass –push to push any local commits to the new repository.

 

Examples for using gh

 

# create a repository interactively
gh repo create

 

# create a new remote repository and clone it locally
gh repo create my-project –public –clone

 

# create a remote repository from the current directory
gh repo create my-project –private –source=. –remote=upstream

 

Options
–add-readme
Add a README file to the new repository
-c, –clone
Clone the new repository to the current directory
-d, –description <string>
Description of the repository
–disable-issues
Disable issues in the new repository
–disable-wiki
Disable wiki in the new repository
-g, –gitignore <string>
Specify a gitignore template for the repository
-h, –homepage <URL>
Repository home page URL
–include-all-branches
Include all branches from template repository
–internal
Make the new repository internal
-l, –license <string>
Specify an Open Source License for the repository
–private
Make the new repository private
–public
Make the new repository public
–push
Push local commits to the new repository
-r, –remote <string>
Specify remote name for the new repository
-s, –source <string>
Specify path to local repository to use as source
-t, –team <name>
The name of the organization team to be granted access
-p, –template <repository>
Make the new repository based on a template repository

 

 

gh
Work seamlessly with GitHub from the command line.

 

USAGE
gh <command> <subcommand> [flags]

 

CORE COMMANDS
browse: Open the repository in the browser
codespace: Connect to and manage your codespaces
gist: Manage gists
issue: Manage issues
pr: Manage pull requests
release: Manage GitHub releases
repo: Create, clone, fork, and view repositories

 

ACTIONS COMMANDS
actions: Learn about working with GitHub Actions
run: View details about workflow runs
workflow: View details about GitHub Actions workflows

 

ADDITIONAL COMMANDS
alias: Create command shortcuts
api: Make an authenticated GitHub API request
auth: Login, logout, and refresh your authentication
completion: Generate shell completion scripts
config: Manage configuration for gh
extension: Manage gh extensions
gpg-key: Manage GPG keys
help: Help about any command
secret: Manage GitHub secrets
ssh-key: Manage SSH keys

 

FLAGS
–help Show help for command
–version Show gh version

 

EXAMPLES
$ gh issue create
$ gh repo clone cli/cli
$ gh pr checkout 321

 

ENVIRONMENT VARIABLES
See ‘gh help environment’ for the list of supported environment variables.

 

LEARN MORE
Use ‘gh <command> <subcommand> –help’ for more information about a command.
Read the manual at https://cli.github.com/manual

 

Continue Reading

Using GitHub – Git Commands

A git repository or repo is an online store for your code. After you push your files from your local repo to your remote git repo it means there is a copy of them held offsite at a remote location.

 

So if you change your local files, the remote versions don’t change until you ‘commit’ your local changes and then ‘push’ them to the remote git repo.

 

The examples below use the GitHub service, but there are many others such as GitLab and bitbucket. Alternatively you may use your own git repository on a server maintained on your own premises.

 

The remote git repository can also be cloned by other team members so they can also contribute to the code maintained in the repo.

 

Using GitHub

 

You need to make sure the remote url for the remote Git repo exists in your GitHub account, and to do this you need to first create a new repository, for example called AnsibleLab.

 

The GitHub account url will be something like, for this example: https://github.com/KevWellsLinux/

 

KevWellsLinux being the account name.

 

And the repository, in this example called AnsibleLab, will have a unique url, for example

https://github.com/KevWellsLinux/AnsibleLab.git

 

Note that a repository can have multiple “remotes” but usually you will only use one.

 

Note also that Git is case-sensitive so beware of accidentally creating multiple undesired remotes!

 

For example, if you were to run the command

 

git remote add origin

 

and then run the command

 

git remote add ORIGIN

 

the result will be two new remotes, one called origin and the other called ORIGIN.

 

ORIGIN is just a simple name for the url of a remote Git repository.

 

When you clone a repository using git clone, it automatically creates a remote connection called ORIGIN linking back to the cloned repository and called ORIGIN by default.

 

For many Git commands many details are presumed. Thus in most instances you don’t need to actually specify ORIGIN since it’s presumed that it’s the name of your remote repo.

 

This means that all of the following commands are actually identical in effect:

 

git fetch ORIGIN
git fetch https://remote.repo.url
git fetch

 

Thus when you enter:

 

git fetch

 

– the ORIGIN part for git fetch is presumed.

 

If you had renamed your remote repo to for example MYREMOTEREPO then you would need to enter:

 

git fetch MYREMOTEREPO

 

 

How to set the remote Git repository

 

You use the git remote add command when a remote repo with the name specified does not yet exist.

 

However, if there is already a remote with the name specified, this will generate an error.

 

In this case you need instead to run git set-url.

 

For this you specify:

 

A remote name, eg: ORIGIN

A remote URL, eg: https://github.com/repouser/repo.git

 

Thus to set the ORIGIN in git, you would enter:

 

git remote add ORIGIN https://github.com/user/repo.git

 

This then links your local repo to the remote github repo at the address you just specified. You can then push your changes from the local repo to the remote git repo.

 

To display the name of your remote repo or the git origin, use:

 

git remote

 

To display the full url of ORIGIN, use:

 

git remote -v

 

for example:

 

kevin@asus:/media/kevin/STORAGEVOLUMELUKS/DATAVOLUME/ANSIBLECODE$ git remote -v
origin https://github.com/KevWellsLinux/ANSIBLECODE.git (fetch)
origin https://github.com/KevWellsLinux/ANSIBLECODE.git (push)
kevin@asus:/media/kevin/STORAGEVOLUMELUKS/DATAVOLUME/ANSIBLECODE$

 

 

To change the remote url use git remote set-url

 

The git remote set-url command changes the url of the remote git repo – ie ‘git change remote origin’. As with the git remote add command, git remote set-url takes 2 commands:

 

– an existing remote name, eg: origin or myremote

 

– a new URL for the remote. For example https://github.com/USERNAME/REPOSITORY.git

 

 

To check if your current folder is already a git repo, use:

 

git status

 

 

To create a repo with your current folder in which you are located, use:

 

git init

 

 

GitHub push commands

 

Here are the Git commands for pushing an existing project to GitHub.

 

These commands perform a push to a GitHub repo named KevWellsLinux/AnsibleLab.git, owned by GitHub user named KevWellsLinux (kevin on local host):

 

git init
git add .
git commit -m “Add existing project files to Git”
git remote add origin https://github.com/KevWellsLinux/AnsibleLab.git
git push -u -f origin master

 

 

 

How to create a new GitHub repository on the command line

 

 Basically what you do to push a new project to an existing GitHub repository is:

 

 

Create a GitHub repository for an existing project.
Copy the GitHub URL for the new git repository to the clipboard.
Perform a git init command in the root directory of the existing project.
Add all of the existing project files to the Git index and then run a commit.
Add the GitHub repository as a remote reference for the existing project.
Perform a git push using the -u and -f switches.
Verify the existing project files have now been pushed to GitHub.

 

 

 

echo “# AnsibleLab” >> README.md
git init
git add README.md
git commit -m “first commit”
git branch -M main
git remote add origin https://github.com/KevWellsLinux/AnsibleLab.git
git push -u origin main

 

 

How to initialize Git in an existing project

 

If an existing project does not already use Git, you issue a git init command in the root folder.

 

After the repository is initialized, you then add all of the project files to the Git index and then perform a commit followed by a push:

 

git add .
git commit -m “Add existing project files prior to the push to GitHub.”

 

git remote add origin https://github.com/KevWellsLinux/AnsibleLab.git
git branch -M main
git push -u origin main

 

 

How to add files to git

 

cd root@asus:/home/kevin/LOCAL/WORK/AnsibleLab

 

git add inventory.yaml
git commit -m “added inventory.yml”
git branch -M main

 

you only need this when creating a new git:

 

git remote add origin https://github.com/KevWellsLinux/AnsibleLab.git

 

then finally do:

git push -u origin main

 

Example:

kevin@asus:~/LOCAL/WORK/AnsibleLab$
kevin@asus:~/LOCAL/WORK/AnsibleLab$ git add AnsibleLabNOTES
kevin@asus:~/LOCAL/WORK/AnsibleLab$ git commit -m “added AnsibleLabNOTES”
[main 61729d0] added AnsibleLabNOTES
1 file changed, 488 insertions(+)
create mode 100644 AnsibleLabNOTES
kevin@asus:~/LOCAL/WORK/AnsibleLab$ git branch -M main
kevin@asus:~/LOCAL/WORK/AnsibleLab$ git remote add origin https://github.com/KevWellsLinux/AnsibleLab.git
error: remote origin already exists.
kevin@asus:~/LOCAL/WORK/AnsibleLab$ git push -u origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 5.30 KiB | 5.30 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/KevWellsLinux/AnsibleLab.git
b1e3ea2..61729d0 main -> main
Branch ‘main’ set up to track remote branch ‘main’ from ‘origin’.
kevin@asus:~/LOCAL/WORK/AnsibleLab$

Continue Reading

Installing Git & Gitea

This article explains how to configure Git and the web-based interface to Git called Gitea.

 

 

Install Git with the following command:

 

 

$ sudo apt install git -y

 

 

Check your Git version:

 

kevin@gemini:~$ git –version
git version 2.25.1

 

Set your user account:

 

git config –global user.name “Your Name”

git config –global user.name “kevin”

git config –global user.email “***@gmail.com”

 

root@gemini:/home/kevin# git config –global user.name “kevin”
root@gemini:/home/kevin# git config –global user.email “***@gmail.com”

This information is then stored in your .gitconfig file in your home directory.

 

Verify with:

 

root@gemini:/home/kevin# git config –list
user.name=kevin
user.email=***@gmail.com
root@gemini:/home/kevin#

 

 

Gitea is the web-based Git interface similar to GitLab. Instructions below for installing on Ubuntu systems.

 

You first need Git installed and configured on your machine before installing Gitea (see above).

 

 

Installing and Configuring MySQL for Gitea

 

Gitea works with MySQL, PostgreSQL, SQLite3 and MSSQL databases. For this example, Gitea is configured to use MySQL database.

 

Install MySQL on Ubuntu 20.04 LTS machine using:

 

$ sudo apt install mysql-server mysql-client -y

 

Then login to the MySQL console as root:

 

$ sudo mysql -u root -p

 

Type in your MySQL root password and press <Enter>.

 

By default no password is set for root user (you don’t actually need a password but it is still advisable to set one).

 

root@gemini:/home/kevin# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10287
Server version: 10.3.29-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]>

 

 

Next create a new MySQL user “gitea” with password “secret”:

 

mysql> CREATE USER ‘gitea’ IDENTIFIED BY ‘secret’;

 

Then create a gitea database for Gitea:

 

mysql> CREATE DATABASE gitea CHARACTER SET ‘utf8mb4’ COLLATE ‘utf8mb4_unicode_ci’;

 

Allow the gitea user full access to the gitea database:

 

mysql> GRANT ALL PRIVILEGES ON gitea.* TO ‘gitea’;

 

Finally, so that the changes will take effect, do:

 

mysql> FLUSH PRIVILEGES;

 

and exit the MySQL shell:

 

mysql> exit

 

root@gemini:/home/kevin# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10287
Server version: 10.3.29-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> CREATE USER ‘gitea’ IDENTIFIED BY ‘secret’;
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> CREATE DATABASE gitea CHARACTER SET ‘utf8mb4’ COLLATE ‘utf8mb4_unicode_ci’;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON gitea.* TO ‘gitea’;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]>

MariaDB [(none)]> exit
Bye
root@gemini:/home/kevin#

 

 

Installing and Configuring Gitea

 

Download the Gitea binary from the official Gitea website:

 

LATEST VERSION as at 29.5.2021 is gitea-1.14.2-linux-amd64

 

https://dl.gitea.io/gitea/1.14.2/gitea-1.14.2-linux-amd64

 

so we do:

 

wget -O /usr/local/bin/gitea https://dl.gitea.io/gitea/1.14.2/gitea-1.14.2-linux-amd64

 

 

Assign execute permission to the Gitea binary /usr/local/bin/gitea:

 

$ sudo chmod +x /usr/local/bin/gitea

 

 

Verify with:

 

$ gitea –version

 

root@gemini:/home/kevin# chmod +x /usr/local/bin/gitea
root@gemini:/home/kevin# gitea –version
Gitea version 1.14.2 built with GNU Make 4.1, go1.16.4 : bindata, sqlite, sqlite_unlock_notify
root@gemini:/home/kevin#

 

 

Next create a new user git for Gitea:
$ sudo adduser –system –shell /bin/bash –gecos ‘Git Version Control’ –group –disabled-password –home /home/git git

 

 

root@gemini:~# adduser –system –shell /bin/bash –gecos ‘Git Version Control’ –group –disabled-password –home /home/git git
Adding system user `git’ (UID 115) …
Adding new group `git’ (GID 123) …
Adding new user `git’ (UID 115) with group `git’ …
Creating home directory `/home/git’ …
root@gemini:~#

 

 

This means the git repositories will be stored in the HOME directory of the git user at /home/git.

 

Next create the required directories for Gitea:

 

mkdir -pv /var/lib/gitea/{custom,data,log}

 

 

root@gemini:~# mkdir -pv /var/lib/gitea/{custom,data,log}
mkdir: created directory ‘/var/lib/gitea’
mkdir: created directory ‘/var/lib/gitea/custom’
mkdir: created directory ‘/var/lib/gitea/data’
mkdir: created directory ‘/var/lib/gitea/log’
root@gemini:~#

 

and change the user and group of these directories to git:

chown -Rv git:git /var/lib/gitea

 

root@gemini:~# chown -Rv git:git /var/lib/gitea
changed ownership of ‘/var/lib/gitea/data’ from root:root to git:git
changed ownership of ‘/var/lib/gitea/custom’ from root:root to git:git
changed ownership of ‘/var/lib/gitea/log’ from root:root to git:git
changed ownership of ‘/var/lib/gitea’ from root:root to git:git
root@gemini:~#

 

Then set permissions:

 

chmod -Rv 750 /var/lib/gitea

 

 

root@gemini:~# chmod -Rv 750 /var/lib/gitea
mode of ‘/var/lib/gitea’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
mode of ‘/var/lib/gitea/data’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
mode of ‘/var/lib/gitea/custom’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
mode of ‘/var/lib/gitea/log’ changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x—)
root@gemini:~#

 

 

Then create a Gitea configuration directory /etc/gitea/

 

mkdir -v /etc/gitea

 

root@gemini:~# mkdir -v /etc/gitea
mkdir: created directory ‘/etc/gitea’
root@gemini:~#

 

 

and change user to root and group to git and set permissions for the Gitea configuration directory /etc/gitea/

 

root@gemini:~# chown -Rv root:git /etc/gitea
changed ownership of ‘/etc/gitea’ from root:root to root:git
root@gemini:~#

 

root@gemini:~# chmod -Rv 770 /etc/gitea
mode of ‘/etc/gitea’ changed from 0755 (rwxr-xr-x) to 0770 (rwxrwx—)
root@gemini:~#

 

 

Next create the systemd services file for Gitea:

 

create service file gitea.service for Gitea in /etc/systemd/system/ directory.

 

nano /etc/systemd/system/gitea.service

 

 

Copy paste the following into the gitea.service file:

 

[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
Requires=mysql.service

 

[Service]
LimitMEMLOCK=infinity
LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web –config /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE

 

[Install]
WantedBy=multi-user.target

 

 

Then start the gitea service:

 

root@gemini:~#
root@gemini:~# systemctl start gitea
root@gemini:~#

root@gemini:~# systemctl enable gitea
Created symlink /etc/systemd/system/multi-user.target.wants/gitea.service → /etc/systemd/system/gitea.service.
root@gemini:~# systemctl status gitea
● gitea.service – Gitea (Git with a cup of tea)
Loaded: loaded (/etc/systemd/system/gitea.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-05-31 12:04:34 BST; 11s ago
Main PID: 269121 (gitea)
Tasks: 9 (limit: 2280)
Memory: 131.1M
CGroup: /system.slice/gitea.service
└─269121 /usr/local/bin/gitea web –config /etc/gitea/app.ini

May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 …dules/setting/git.go:101:newGit() [I] Git Version: 2.25.1, Wire Protocol Version 2 Enabled
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:93:PreInstallInit() [T] AppPath: /usr/local/bin/gitea
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:94:PreInstallInit() [T] AppWorkPath: /var/lib/gitea
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:95:PreInstallInit() [T] Custom path: /var/lib/gitea/custom
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:96:PreInstallInit() [T] Log path: /var/lib/gitea/log
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:97:PreInstallInit() [T] Preparing to run install page
May 31 12:04:34 gemini gitea[269121]: 2021/05/31 12:04:34 routers/init.go:100:PreInstallInit() [I] SQLite3 Supported
May 31 12:04:35 gemini gitea[269121]: 2021/05/31 12:04:35 cmd/web.go:189:listen() [I] Listen: http://0.0.0.0:3000
May 31 12:04:35 gemini gitea[269121]: 2021/05/31 12:04:35 …s/graceful/server.go:55:NewServer() [I] Starting new Web server: tcp:0.0.0.0:3000 on PID: 269121
May 31 12:04:35 gemini gitea[269121]: 2021/05/31 12:04:35 …s/graceful/server.go:66:func1() [D] Starting server on tcp:0.0.0.0:3000 (PID: 269121)
root@gemini:~#

 

 

 

First Initial Configuration of Gitea

 

Next you have to configure Gitea from the web browser

 

Point your web browser at the IP of the Gitea server (Gitea uses port 3000 by default):

 

http://kevwells.com:3000/

 

BUT – we need to switch this to https! else security risk, so:

 

Switching Gitea from http to https

 

To use Gitea’s built-in HTTPS support, you must change your /etc/gitea/app.ini file:

 

[server]
PROTOCOL = https
ROOT_URL = https://git.example.com:3000/
HTTP_PORT = 3000
CERT_FILE = cert.pem
KEY_FILE = key.pem

 

Have to give the full path for the .pem files:

 

 

[server]
PROTOCOL = https
SSH_DOMAIN = localhost
DOMAIN = localhost
HTTP_PORT = 3000
ROOT_URL = https://localhost:3000/
DISABLE_SSH = false
SSH_PORT = 22
LFS_START_SERVER = true
LFS_CONTENT_PATH = /var/lib/gitea/data/lfs
LFS_JWT_SECRET = xxxxxx (blocked out here)
OFFLINE_MODE = false
CERT_FILE = /etc/letsencrypt/live/kevwells.com/cert.pem
KEY_FILE = /etc/letsencrypt/live/kevwells.com/key.pem

 

 

 

then restart gitea server.

 

 

IMPORTANT – SECURITY!

 

to prevent general public from registering user accounts and using your Git server, you need to set following in the [server] and [service] sections of the /etc/gitea/app.ini file: (ie TWO entries)

 

set them both to:

 

DISABLE_REGISTRATION = true

 

 

 

then restart gitea server. Make sure you restart, else the change does not take effect!

 

The register link on the top right hand start page of gitea should now be gone.

 

Also be sure to switch your gitea server to use https instead of http. See above for configuration procedure.

 

 

 

 

 

 

 

Continue Reading