How Can We Help?

Using GitHub – Git Commands

You are here:
< All Topics

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$

Tags:
Table of Contents