Let’s get into GIT! (forking, pushing, pulling, github + vim)
In this article, I want to show you how I spent some time working with git repositories betweeen github.com and my own cloud server. (It had a lot of fun with this!)
Prerequisites:
- Access to a linux server (physical or virtual) with git enabled
- A github.com account and access to a repository
- Git is installed and initialized on your linux server (link below for reference)
- Configure your linux server for using git (link below for reference)
https://www.tecmint.com/install-git-on-centos-8/
https://support.atlassian.com/bitbucket-cloud/docs/configure-your-dvcs-username-for-commits/
STEP 1: Fork a repository from Github
To get started you will want to go to github.com and access an available git repository.
From there you will click the ‘fork’ button in the top-right area of the screen. This will create a copy of the original repository that you can make changes to as you like without affecting the original copy. Access your profile page to verify the fork was created successfully
STEP 2: Clone the forked repository to my cloud server
Copy the URL from your new repository on your Github profile page by clicking the ‘code’ button.
From here you will jump over to your linux server. For this example, I used a CentOS 8 server.
Create a new directory to host our repository on the linux server. This can be done using the ‘mkdir’ command and choosing a name for the new directory. Then enter the new direcotry with the ‘cd’ command followed by the name of your new directory. (The directory name is case-sensitive so make sure it matches exactly.)
Now we will copy the repository from Github to our linux server environment. This is done with the command ‘git remote add origin’ command followed by the URL to our forked repository. Then we will clone a copy of it as well for our linux server.
This tells our server to grab a copy of our Github repository and host it locally.
STEP 3: Using vim, create a change in linux.sh file that is in the repository directory
From here we will want to make a change to our repository to make it unique. For this example I was given a file inside the repository to use for this. If you don’t have one given you can create a simple text file by tyipng the command ‘touch <your_file_name>’ [enter].
Before we enter our file we must make sure to add all the files to our local repository with the command ‘git add .’. This tells git on our linux server to acknowledge all files in the current repository (or head repository) and display then on our edited copy.
Now we will access our file and make changes using the ‘vim’ command
Type ‘vim <your_file_name> (‘linux.sh’ in my case)’ [enter] to access the file via vim
Type ‘i’ to edit the text in the vim file and make whatever changes you want to the text.
Next, hit the [esc] key and type ‘:wq’ [enter] to save and exit the file.
STEP 4: Save the file and add to my local repository
Nex we will want to verify the changes made to our file. Do this by typing ‘git status’ [enter] to view changes made in the repsitory.
From here, type ‘git add <your_file_name>’ [ener] to save the changes to your local repository. (or just ‘git add . [enter]’ to save everything)
Next we will type ‘git commit -m <detail the changes made to your files>’ to commit the changes to the repository and make them permanent. the ‘-m’ tells linux to accept the following text as notes about the changes made to the file(s). Without it, you will be prompted to a new screen and forced to make notes about your changes to proceed.
STEP 5: Push the file back to Github repository
From here we have completed all changes and will need to push the repository back to your Github account.
Type ‘git push -u <your_orignal_git_repository_URL>’ [enter] to send your edited repository back to github
You may encounter an error in this stage. If so, you will need to create an access token on github. You can find info about that here: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
STEP 6: Send a pull request to merge with the original repository
From here we will attempt to pull our changes back to the original repository that we forked from in the beginning. To do so you will open the ‘Pull Request’ menu on Github from our account page.
Verify the base repository and your head repository are correct and click ‘Create Pull Request’. Then enter heading and comments, if necessary, for the changes you made to the repository. Once this is done, you will click the ‘Create pull request’ button to complete the pull request process.
After this you are done! It will be up to the owner of the original repository to accept your pull request to add the changes made to their master copy.