Git: Repo Set Up, Get And Update

Step-By-Step Set-up, Fetch And Update Of Repository

Mi'kail Eli'yah
5 min readJun 7, 2019

1. Repo Set-Up
2. Repo Get
3. Repo Update

Repo Set-Up

Set-up a remote repo as following example:

Caveat: Choose Use SSH to get the URI link for git clone.

Set-up a local repo. This will be the local copy where you will get and update the repository from. Example:
From ~/utility_git/git_home/

% mkdir tutorial_git; cd tutorial_git
# git initialize the folder
% git init

Repo Get

Repo Get is termed as `git clone`.
Example:# git clone <git_repo_name> <local_dir_clone_to>
% git clone git@github.com:Mikail-Eliyah/tutorial_git.git
With a new folder downloaded / clone, view the contents:
% cd tutorial_git/

To get specific file or just override the file existing from the one in the repo:

$ git checkout <git_name>/<path_to_file>/<file_name>.<file_type>e.g. git checkout tutorial_git/chapter_01_access_setup/main.sh""" example:
git init
git pull https://github.com/Ursa/2019-docker.git
"""

Repo Update

Make some changes README.md as a test. When you try updating, you may get an error.

git add .
git status
git commit -a -m "Update notes in README.md"

The reason could be that you may have multiple accounts or that your SSH keys are not correctly referred to. Recall that we set the SSH key with specific name to ensure we get this right and that the keys do not get corrupted by multiple set-ups.

Ensure that the ssh-agent is connected : % eval "$(ssh-agent -s)"

Let’s apply some correction to the connection:
% ssh-add $key_store$key_id # add private key reference for ssh-agent

* ensure that the path to key is defined, e.g.

key_store='../key_store/.ssh/' # ensure key_store folder exist by: 
$ cd $key_store
or at the folder node:
$ find -name $key_id
key_name=$user_id'_rsa'
key_id=$key_name

or it could be called in a script: $ cat connect.sh

user_id=’Mikail.Eliyah’                # e.g. name of key file
key_file=$user_id’_rsa’
key_store_path=’../key_store/.ssh/’ # folder of key file
eval “$(ssh-agent -s)”
ssh-add $key_store_path$key_file

We should see:

"Identity added: ../../../../key_store/.ssh/Mikail.Eliyah_rsa (<email_address>)"

Check as well that the repo domain is added by git remote -v:

Try pushing the code to the main branch again:
git push -u origin master
* `master` changed to main

$ git remote add origin <git_source_remote> # e.g. $git_source_remote:= https://github.com/<id_user>/<id_repo>.git$ git push origin main

Caveat: If you are not starting from a git clone, do
1. git remote add origin <git_repo_name> # e.g. <git_repo_name> := git@github.com:Mikail-Eliyah/operational_ready_in_c.git
2. git remote -v (set new remote in this case) to ensure that the location is accessed and added
3. then do git pull, then git push -u -f origin master.
* `master` changed to main

Refreshing the github url, we can see the updates (in this case, we just append a timestamp to the file) that was made:

Use to git log to view the history of updates:

If someone updates the same repo, to synch with repo:

git checkout master # caveat: `master` may have be renamed as main
git pull

Automate Launch 1st Push

% initialize_1st_git_repo “https://github.com/ursa-mikail/reference_codes.git"
# sample usage: initialize_1st_git_repo "https://github.com/ursa-mikail/shell_script_utility.git"
function initialize_1st_git_repo() {
local remote_url="$1"

if [ -z "$remote_url" ]; then
echo "Usage: initialize_git_repo <remote_repository_url>"
return 1
fi

# Create the folder if it doesn't exist
# create_folder_if_not_exist "$repo_path" "repository directory"

# $repo_path: e.g. ~/ursa/git/shell_script_utility
# Navigate to the project directory
#cd "$repo_path" || { echo "Failed to navigate to '$repo_path'"; return 1; }

echo "Initializing Git repository in '$PWD'..."
git init

# git remote add origin https://github.com/ursa-mikail/shell_script_utility.git
echo "Adding remote origin '$remote_url'..."
git remote add origin "$remote_url"

echo "Creating and switching to the main branch..."
git branch -M main

echo "Adding all files to the staging area..."
git add .

echo "Committing the files with message 'Initial commit'..."
git commit -m "Initial commit"

echo "Pushing to the remote repository..."
git push -u origin main

echo "Git repository initialized and pushed successfully."
}

Let’s set a user menu to assist for semi-automation:

Chapter_02_Repo_Setup,_Get_and_Update User Menu

Alternatively, you can configure a git_main:

git_main

Further Notes

Given there are files which are to be omitted from the push, state them in a file in the folder name .gitignore.

echo '*.tmp' > .gitignore # here we assume all *.tmp files are to be omitted
git add .gitignore
git commit -m "adding .gitignore file"
git push -u origin master

Github scripts for the tutorial: Chapter_02_Repo_Setup,_Get_and_Update

It wasn’t raining when Noah built the ark. — Unknown

--

--