Tags Archives: github

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


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 in the new repository
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 from template repository
Make the new repository internal
-l, –license <string>
Specify an Open Source License for the repository
Make the new repository private
Make the new repository public
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



Work seamlessly with GitHub from the command line.


gh <command> <subcommand> [flags]


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: Learn about working with GitHub Actions
run: View details about workflow runs
workflow: View details about GitHub Actions workflows


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


–help Show help for command
–version Show gh version


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


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


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



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:





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)



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



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’.

Continue Reading