Updating GitHub repository using Git Commit

MALAY JOSHI
9 min readMay 5, 2021

Git is primarily a version control system and a staple in any software development project. It usually serves 2 main purposes: code backup and code versioning.

The common trouble is that Git can be tricky to use. There are times where versions and branches are out of sync and you spend serious time just trying to push your code to GitHub using it with all efforts going in vain. Even worse, not knowing how exactly certain commands work could easily lead to accidentally deleting or overwriting bits of code.

That’s why I’ve prepared this tutorial, to teach you how to properly use Git so as that you can get started with updating your GitHub repository using it.

Do read about https://git-lfs.github.com/ also.

Install and setup

Download Git Bash from the following link: https://git-scm.com/downloads

Once you install Git Bash in your system, launch it and type the following code:

git config --global user.email “13.malayjoshi@gmail.com”git config --global user.name Malay

Note: Please enter your email id and username in place of entries filled by me

First situation: When you want to update GitHub repository owned by you and located at your own GitHub account

Note: Execute all the following steps in Git Bash

A) First choose a location in your local system (let us say “/c/Users/barcode/Desktop”) where you would like to clone/copy your GitHub repository.

Then using Git Bash, change your current working directory to this desired/selected location by using “cd” command.

cd /c/Users/barcode/Desktop

B) Next, clone your GitHub repository to the selected location (let us say I wish to update this “https://github.com/malayjoshi13/Describe” repository of mine) by using “git clone” command.

git clone https://github.com/malayjoshi13/Describe.git

Note: You have to use this step (B) just for the first time to clone your desired repository (present in your GitHub) to your local machine. After that follow all steps except this step until you are updating/committing in that same cloned repository.

C) Now move inside “Describe” folder of repository cloned in your local system by using “cd” command.

cd /c/Users/barcode/Desktop/Describe

D) Before moving ahead let us understand the role of “git remote”. This command will help you to view all urls (and also help to add a new url to some new shortname, will see in later section) of cloned GitHub repositories referenced to some word, like:

git remote -v

From above it is clear that url of our GitHub repository (which we have freshly cloned in our system) is referenced as shortname “origin”. Then from now what all actions you have to perform (like fetching, deleting, committing, staging, etc) with this url, will be done by using this shortname “origin” and not the whole url.

E) Now to ensure that your local copy/cloned GitHub repository (owned by you and located in your own GitHub repository) is up to date, you have to use the “git fetch” command alongwith shortname “origin”.

This command will fetch all new changes/commits (if any) made to the remote repository whose url is stored in shortname “origin”.

git fetch origin 

F) Before pushing a new commit to the cloned repository, you have to delete any changes made by mistake or intentionally after the latest commit pushed to the remote GitHub repository by using “git reset” command.

This is done to make changes/commit to the cloned repository right from the latest state in which the remote repository exists currently.

git reset --hard origin/main

G) Next to create and switch to a new branch use the git checkout command.

In first-time execution it will create a new branch called “new-update” and then from second execution onward this command will be used to switch from the “main” branch to the “new-update” branch.

A separate branch is created so as to not directly make changes/committing to the main branch of remote GitHub repository without finally assuring changes to be perfect.

git checkout -b new-update
Keep Going

H) Now you can do whatever changes you want to make in the cloned repository located at your local system. These changes includes like updating the file’s content, renaming file(s), deleting/adding new file(s), etc.

Once we are done with all changes in the cloned repository, the next step is to execute the “git add” command which adds new or changed files to the Git staging area by taking snapshot of the changes done in the cloned version (an intermediate stage between the cloned repository and the remote GitHub repository).

 git add *

I) Next step is to execute the “git commit” command, which will commit/save history of snapshot(s) of changes made during step (H) in the cloned repository.

In addition to saving history, this command also enable us to write a message about what the commit/update is all about.

git commit -m 'new update'

J) Now using the git push command, you will be able to upload content of the local cloned repository (on which you are working currently) back to its original remote GitHub repository.

You have to use the shortname “origin” where we referenced the url in step (D) to specify the path of original remote GitHub repository were you want to make the final changes.

git push origin new-update

K) Now open the remote repository (where you pushed the changes) in the browser and then click on “Compare and Pull request” button located in the notification pop up at the top of page.

L) Next follow the green buttons one by one and keep on agreeing to merge the pull request, and after successfully merging the commit, click on “delete the branch” button.

M) With this, you have successfully updated GitHub repository owned and present in your GitHub account by using Git commit.

Second situation: When you want to update GitHub repository owned by some other person and present in your GitHub account

Note: Execute all the following steps in Git Bash

A) Go to the GitHub account of the user from where you want to fork the repository.

Then on the top right corner, click on “the Fork” button to fork that original repository from the GitHub account of that user to your own GitHub account.

B) Choose a location in your local system where you would like to clone that forked GitHub repository (like I selected the “/c/Users/barcode/Desktop” location).

Now change your current working directory to your desired location by using “cd” command in Git Bash.

cd /c/Users/barcode/Desktop

C) Next, clone the repository (which you forked from some other person) located in your GitHub account to the selected location in your local system. (let us say I want to update this “https://github.com/malayjoshi13/demo” GitHub repository that I forked from “https://github.com/malay1931025/demo”) by using “git clone” command.

git clone https://github.com/malayjoshi13/demo.git

Note: You have to use this step (C) just for the first time to clone your desired repository (present in your GitHub) to your local machine. After that follow all steps except this step until you are updating/committing in that same cloned repository (forked from another person’s GitHub account).

D) Now move inside “demo” folder of the repository cloned in your local system by using “cd” command.

cd /c/Users/barcode/Desktop/demo

E) Now we use the “git remote add” command to add the location of the original remote GitHub repository (located in the GitHub account of some other user) where we want to finally commit changes by referencing the url of that repository to the shortname “upstream”.

Before
git remote add upstream https://github.com/malay1931025/demo
After

After executing this code, “git remote” will have two paths added:

i) One is the path of the repository (let us say repository “describe”) owned by some other user and forked to your GitHub account → referenced by the shortname “origin”

ii) Another path is of that same repository (let us say repository “describe”) owned and present in GitHub of some other user → referenced by the shortname “upstream”

Note: shortname “upstream” has url of remote repository where final changes will be merged.

F) Now to ensure that your local copy/cloned GitHub repository (forked from some other person’s GitHub account) is up to date, you have to use the “git fetch” command alongwith shortname “upstream”. This command will fetch all new changes/commits (if any) made to the remote repository whose url is stored in shortname “upstream”.

Note: Unlike step (E) of first situation here we are not using shortname “origin” because this time the final changes are aimed to be happen in url https://github.com/malay1931025/demoof repository owned and present in GitHub account of some other person referenced by shortname “upstream”.

On other hand, shortname “origin” for this situation has url https://github.com/malayjoshi13/demo.gitof repository in GitHub of other person and forked to your GitHub account.

git fetch upstream

G) Before pushing a new commit to the cloned repository, you have to delete any changes made by mistake or intentionally after the latest commit pushed to the remote GitHub repository) by using “git reset” command.

This is done to start making changes/commit to the cloned repository right from the latest state in which the remote repository exists currently.

git reset --hard upstream/main

H) Now you have to create a new branch and switch from “main” branch to that newly formed branch named by using “git checkout” command.

git checkout -b new-update

I) Then do the required changes in cloned repository and once done, stage these changes by using “git add” command.

git add *

J) Next using “git commit” command commit files in the staging area. You can also write a message about what the commit/update is all about.

git commit -m 'new update'

K) Now using the git push command, you will be able to upload content of the local cloned repository (on which you are working currently) back to its original remote GitHub repository.

You have to use the shortname “origin” where we referenced the url https://github.com/malayjoshi13/demo.gitin step (D) to specify the path of repository (present in your GitHub account) forked from GitHub account of some other user.

git push origin new-update

Note: However, unlike the first situation discussed above, here “git push origin new-update” do a little magic.

Here the changes go from local cloned repository to repository located in your GitHub account (which is a forked version of the original repository of some other user) as directed by the “git push origin new-update” command.

Then from your GitHub account, the changes can be merged to the GitHub repository of another user where we want to commit changes finally (will see this below).

L) After pushing changes to forked repository present in your GitHub account, go to your GitHub account, open a pull request, and wait for your changes to be merged into another user’s repository.

With this you have learned basics of using Git commands to update GitHub repository using Git Bash CLI.

Please feel free to post any questions in the comments below, and I will try to answer them to the best of my knowledge.

--

--