Git & GitHub Operations

Hilal Gevrek
8 min readApr 28, 2023

--

In this article, I will cover the details of GitHub usage. You can check my other article, if you want to learn more things about the logic of Git Version Control System and GitHub.

You can use Git Bash for the Git commands. Git Bash is an application for Microsoft Windows, allowing developers to use Git in a command-line interface.

Let’s do a few exercises to better understanding of the Git logic.

  • How to get a repository from GitHub account?

If you already have a repository in your GitHub account, or you want to get someone else’s repository, you can transfer the repo to the local with ‘git clone <repo URL>’ command.

  • How to push a local repository to GitHub account?

In this practice, we will create a local repository, then we will push our local repository to our GitHub account as a remote repository.

First, we will create a folder in local. You can write the name you want as a folder name between the angle brackets. I’ve created a folder named practice.

mkdir <folder name>

Then, get into the folder we’ve just created

cd <folder name>

Next, create the necessary files for you. As an example you can create 3 files with ‘touch index.html file.txt style.css’ command.

touch <file names that you want to create>

You can check with ‘ls -al’ command if your files created.

After, we will start the version control system. But you should be careful in which folder you run the command that starts the version control, because of it will keep track of all files under the folder where you run this command. This means that now the version control system is dominant in the folder where this command is run, and all your movements are recorded.

git init

You can see your all files with ‘ls -al’ command -including hidden files- in your folder. If there is a ‘.git’ file in your folder which is a hidden file, it means version control system is started.

Until now, you worked in your working directory in your local. We want to push all the changes to our remote repository. As you can see below we should add our files to the staging area which is the middle ground between what you have done to your files. As the name implies, the staging area gives you space to prepare (staging) the changes that will be reflected on the next commit.

Main states of Git

‘git status’ command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

When you look at it with the ‘git status’ command, you will see that your filenames are red as you can see following. We’ll check after moving all files to back to the staging area.

The git add command adds a change in the working directory to the staging area.

git add .

The dot at the end of ‘git add’ means all files. If you don’t want to stage your all files, you can write only the name of the file that you want to stage.

If you check your status with git status command, you will see the file names are green. That means your files are in staging area.

If you want to unstage your files from the staging area to the working directory, you can use ‘git reset’ or ‘git rm --cached <your file name>’ commands

Now, we will commit our files to our local repository with the following command. When you make a commit, you must include a commit message that briefly describes the changes. You can write your commit message between the quotation marks.

git commit -m "<commit message>"

As another option for your commit message you can use vi editor. ‘-m’ sets the Git commit’s message. You can open insert mode with ‘i’, and write your commit message then, write ‘:wq’ which means save and quit.

Please look at vi cheat sheet to better understanding of vi editor usage.

If you check your status with git status command, you will see ‘working tree clean’ message. This means your files are ready to be push to the remote repository.

Create a repository in your GitHub account as following, and get the URL of the repository. This will be your remote repository. According to your choice, it can be a private or public repository.

Use the following command to add your remote repository.

git remote add origin <GitHub repo's URL>

In the following command we used -f ,which means force, because we are trying to push an another repository in the local to the remote repository. We used master because it is default branch name in git. If you are working in a different branch you can write your own branch name.

git push -u -f origin master

Finally, refresh your GitHub page, and you’ll see the changes.

  • How to push a folder on desktop to GitHub account?

First, you should create an empty repository in your GitHub account. After cloning the repo, you can add any folder you want to the cloned repository. After, you should push the changes to the repository.

  • How to update local repository with changes from the GitHub repository?

Generally there are a few branches who working on the same project. Hence, you should update the project to the latest version before you work on it. You can clone the project again from the repository, but it’s a waste of time for the large scale projects. You can get all changes with ‘git pull origin’ command.

  • How to revert to previous version of the project?

You can use ‘git log’ command to see commits which is a utility tool to review and read a history of everything that happens to a repository.

The term ‘HEAD’ refers to the current commit you are viewing.

Git assigns each commit a unique ID, called a SHA or hash, that identifies: the specific changes, when the changes were made, who created the changes. Select the commit that you want to revert, and get the SHA code, you can get only last 5 character, then run the following command.

git checkout <last 5 character of your SHA code>

If you want to revert to the initial version of your project, you can use ‘git checkout master’ command.

‘git commit --amend’ command lets you modify your last commit. You can change your log message and the files that appear in the commit. The old commit is replaced with a new commit which means that when you amend your old commit it will no longer be visible in the project history.

  • How to work with different branches?

Git branches facilitates the feature branch workflow popular with many Git users. Feature branches provide an isolated environment for every change to your codebase.

You can create multiple branches with the following command.

git branch <branch name>

You can change the branch by specify the name of the branch you want to switch to in the following command.

git branch checkout <branch name>

You can see your all branches with “git branch” command.

  • What is merge conflict?

During the software development process, several developers commonly change artifacts in parallel, and with a merge process these parallel changes can combined. Merge conflicts occur when competing changes are made to the same line of a file, or when one person edits a file and another person deletes the same file. For this reason, some changes that cannot be automatically combined because of the merge conflicts occur, and git needs to help to decide which changes to incorporate in the final merge. At this stage, the main branch who is responsible for the merge must reconcile decisions and resolve conflicts.

  • What is pull request?

In a general scenario many developers work on a repository, and not all of them have push access to master. This means they cannot push the code directly to master. Hence, developers push the code to a different branch and then raise a pull request to merge the changes to master.

Pull requests let you tell others about changes you’ve pushed to a branch in a repository on GitHub. Once a pull request is opened, you can discuss and review the potential changes with collaborators and add follow-up commits before your changes are merged into the base branch.

First, you can create a new branch to your repo with ‘git branch <branch name>’ command. Then, you can change your branch with ‘git checkout <branch name>’ command. After you push your changes, you will see a pull request on your GitHub account as following.

Finally, you can click ‘compare & pull request’ button, and merge the changes. If there is a conflict because of the different branches you can also see and fix it as a main branch.

  • How to see code history?

You can see your all commands with ‘history’ command. If you want, you can create a text file which is contain your all codes with ‘history > history.txt’ command.

The command line keeps a history of the most recent commands you executed. By pressing the arrow keys, you can step through the last commands you called (starts with the most recently used).

I hope I helped. See you in my next post 🙂

--

--