Git Bash script Operations on a Cent OS

Joseph Peter
7 min readSep 6, 2022

--

Git Bash Operations on Cent-OS

Objective : Git fork, clone, branch, push and pull from GitHub repository, using ssh to connect with GitHub.

1.Fork the GitHub Company Repository.
2. Clone the forked repository to your ACG cloud server environment
3. Using Vim, create a change to the file that is in the repository directory
4. Save the file and add to your local repository, then commit the file
5. Push the file back to your own github repository
6. Send a pull request to merge with GitHub production repository.

Prerequisites:

i. Should have a personal GitHub account if you don’t have please refer — https://docs.github.com/en/get-started/quickstart

ii. sudo user account privileges required.

iii. Should have cloud server access using any acloudguru or kodekloud or any other cloud services.

iv. Should know how to use ssh to connect with GitHub , if you don’t refer — https://docs.github.com/en/authentication/connecting-to-github-with-ssh or refer to my article which shows how to connect with GitHub using SSH->https://medium.com/@josephvpeter7/how-to-setup-git-on-your-cloud-server-using-ssh-f25742c9b797

v. No need to use PAT (Personal Access Token) instead we use ssh to connect with GitHub.

How to know which OS and release version are installed on your cloud server?

steps to do : use cat /etc/os-release to get the OS version details. if you need to know about the kernel version along with OS details then use this cmd — hostnamectl.

cat /etc/os-release

for my cloud server it shows these

OS information details

Step:1 — Fork GitHub Repository

What is Git and GitHub ?

In my simple term , Git is a local repository manger and GitHub is a global repository manger.

In actual terms from wiki — Git is free and open source software for distributed version control: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows (thousands of parallel branches running on different systems).

And GitHub is an Internet hosting service for software development and version control using Git. It provides the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous integration, and wikis for every project.

reference : https://en.wikipedia.org/wiki/GitHub

What is Fork?

When I want to do changes to the project from my companies account,first thing I do is copy from my company repository. GitHub uses the term fork which is copy the project to my own account from my company account.

As per GitHub — A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

Steps to do : we go to GitHub’s LevelUpTeam repository and click on fork and then create a new fork

Go to fork and create a new fork

Lets check and see if we have our own copy of LevelUpTeam project in our repository.

After forking we can see it in under our repository

I can see the LevelupTeam project under my username joey1089. so its done.

Step:2 — Clone the project from GitHub to the cloud server.

Login with ssh into cloud server

Check if git is installed by using this command, by default all Cloud server has git installed.

Steps to do :-

git --version
Shows current git version

Steps to do :- Check if GitHub account is setup properly.

Set up your own git account in your local server — for more information refer -https://docs.github.com/en/get-started/getting-started-with-git/setting-your-username-in-git

git config --list

In the terminal , it will show the user name and the email id. Its shows for me so I’m good.

Steps to do :- Now I created git_project folder/directory and change to that folder and did a git init

git init
git init — initializes the Git repository

Steps to do : Now check the folder /directory with cmd ls -al

ls -al
shows .git file created after initialization

Steps to do : Clone LevelUpTeam Project into your cloud server using SSH , First we go to GitHub LevelUpTeam repository and click on code and then select ssh and then click on clip icon to copy.

It will say copied once clicked on it

now on your terminal type git clone and past from your repository.

git clone git@github.com:your_repository_name/LevelUpTeam.git
LevelupTeam project copied on our local server

Now the git clone has successfully copied all the levelupteam project into our server repository.

Sets to do : Before we make any changes , we do a branch out.

Create a Branch and switch to it — Just in a single command

This can also be done in two separate commands by using git branch branch_name and then use git checkout branch_name

git checkout -b Add_Changes_br

here I have created a branch Add_Changes_br and switched to it.

Step:3 -Make changes to the file using VIM editor

Step to do : Lets check if we have all the files from the levelup team repository by using ls -al cmd.

We do have all the files from LevelupTeam

step to do : use VIM editor to do the necessary changes to the linux.sh file.

vim linux.sh

this cmd opens the vim editor and shows the existing code has curl cmd and then last line will throw error and break.

looks like last lines need to inside echo

step to do : lets fix this issue to print till the last line.

correct the last line and saving it

step to do: Testing our script , lets run and see if it works.

bash ./linux.sh

Test passed we have no issue.

Step:4 — Save the file and commit

git status

This command will show what has been modified in the repository.

Shows that linux.sh has been modified

Step to do :lets do a commit and as well as add at the same time, instead of using add first and then commit with message.

git commit -a -m "print the curl command and last line"

It looks successful has we can see 1 file changed , 3 insertions and 2 deletions.

Step: 5- Push these change to GitHub

step to do : lets push the code to our GitHub repository. since we have a new branch we have to set upstream to new branch in this case its “Add_Changes_br”

git push — set-upstream origin Add_Changes_br
Set up remote branch to new branch

Step : 6 — Pull request in GitHub

Lets check our GitHub repository for any pull request conflicts. whenever we push new code there is always chances of conflicts happening. In this case let check if we have any conflicts.

Compare and pull
Able to merge

No conflicts is reported and is able to merge.

No conflicts with base branch
We can see our commit message
Shows our changes at the file level

Lets do a pull request in our local server’s terminal and see

we have branch so we need to rebase to merge

step to do : We had a branch , so we have to do a git config pull.rebase false # merge to avoid any conflict if anyone has updated the repository.

git config pull.rebase false

git pull command also say its already up to date. no conflicts so its successful.

step to do : check our linux.sh file , looks like file is not modified by pull request.

file is not changed we kept our changes.

We have walked through how to do git fork, clone, branch out and pull request with a git repository. we also used vim editor to change the bash script and seen our changes reflected in the GitHub repository.

--

--

Joseph Peter

AWS Community Builder and 2x AWS Certified, Terraform, Linux Certified and I want to implement cost effective solutions to the cloud.