What is Git ?

Git was created by Linus Torvalds in 2005 for development of the Linux Kernel with th other kernel developers contributing to its initial development. Since 2005, Junio Hamano has been the core maintainer. As with most other distributed version control systems, and unlike most client–server systems, every Git directory on every computer is a full-fledged repository with complete history and full version-tracking abilities, independent of network access or a central server. Git is free and open-source software distributed under GNU General Public License Version 2.

Why is it called Git ?

Torvalds sarcastically quipped about the name git (which means “unpleasant person” in British English slang): “I’m an egotistical bastard, and I name all my projects after myself.

Why Git ?

Version control is the only reasonable way to keep track of changes in code, manuscripts, presentations, and data analysis projects. I used to make numbered tar.gz files for a project. But exploring the differences is difficult, to say the least. And if you use git properly, you’ll have annotated each small change. Merging collaborators’ changes made easy. Have you ever had to deal with a collaborator sending you modifications distributed across many files, or had to deal with two people having made changes to the same file at the same time? Painful. git merge is the answer.

Why use Github?

Github is like facebook for programmers. Everyone’s on there. You can look at what they’re working on and easily peruse their code and make suggestions or changes. It’s really open source. “Open source” is not so open if you can’t easily study it. With github, all of the code is easily inspected, as is its entire history.

What are the benefits of using version control?

Having a GitHub repo makes it easy for you to keep track of collaborative and personal projects — all files necessary for certain analyses can be held together and people can add in their code, graphs, etc. as the projects develop. Each file on GitHub has a history, making it easy to explore the changes that occurred to it at different time points. You can review other people’s code, add comments to certain lines or the overall document, and suggest changes. For collaborative projects, GitHub allows you to assign tasks to different users, making it clear who is responsible for which part of the analysis. You can also ask certain users to review your code. For personal projects, version control allows you to keep track of your work and easily navigate among the many versions of the files you create, whilst also maintaining an online backup.

Basic Git commands

Let’s go through the example to understand the commands faster and easier.

Let’s create a folder called git_and_github where our pycharm works.

You can see my directory below.

After creating the file in there, let’s open it on the terminal.

After doing this, when we check the terminal, we see that we are in our file’s directory.

git init

Although we are in the directory of our file, we did not enter the command to associate it with git. After the git init command, this folder will realize that it is in a world where the rules of the git world apply.

We also see that the git file is created in the file we created.

To better understand other commands, let’s drag some files to the file we created.

git status

The git status command is used to display the state of the repository and staging area. It allows us to see the tracked, untracked files and changes. This command will not show any commit records or information.

* Mostly, it is used to display the state between Git Add and Git commit command.

As you can see, the file we added appears in red. This means the file has not been updated to git yet. And also it means that it is untracked file.

git add

To make the changes traceable, we can use ;

git add 26_UNSUPERVISED_ML.py

or

git add * ( this adds all files )

As you can see after adding the file color turned green.

So in git world the file has been updated and become traceable.

git commit -m

To make notes what changes were made in the file and, if necessary, to go to the point before the change checkpoints should be created.

With this note, we will be able to see what has been done with this change, if necessary.

git log

Let’s make some changes in our file respectively :

Let’s add a code to our .py file, add a note document and then an excel document to our folder.

After every change we done, git add and git commit -m commands were used to create checkpoints and make them traceable.

git log command is used to see what has been done.

  • To see it more regularly git log — pretty=oneline is used.

We can see what changes we have made with the log records and the notes we left for the changes.

To add a document to our file using the terminal for windows, we can use

echo $null >> new_text.txt . The extension can be changed for other types of documents such as .csv, .xlsx etc.

In this way many new file can be added through terminal.

git show

git show is used to see the document in terminal. After typing git show , some part of the numbers in the logs are pasted.

‘q ’ key is used to exit this screen.

What Is BRANCH Concept?

A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process. You can think of them as a way to request a brand new working directory, staging area, and project history. New commits are recorded in the history for the current branch, which results in a fork in the history of the project.

git branch

To create new branch git branch (name of branch) is used.

As you see below, we created new branch called new_branch_for_new_features.

git checkout

Now, we can work in this new branch with git checkout new_branch_for_new_features. With this command we added code line new_features = ‘new_features’ as a new feature to our py extension file on pycharm.

To save and track changes git add * and git commit -m were used.

Checking last status with git status ;

As seen above we are working in our new branch.

In this way, we keep the main project safe by protecting it from mistakes.

With this command, we can work on the newly created branch. Just creating the branch is not enough.

We must also give the command that we will work on it.

As the only command to create the branch and work on it we can use git checkout -b new_branch_for_new_features. So, straight away we can work in new branch.

git branch -d

It deletes branches.

Used as git branch -d branch_name

git merge

It is used to merge the branch we are working on into the main project. For example, if we are working in new_branch_for_new_features, we can combine this work with the main project with git merge master.

The important point here is that before merging, we must switch to the master branch and use the merge command at this stage.

We should always tend to go to the main project.

  • Master always represents main project.

How Does Git Reset Work?

  • git reset --soft, which will keep your files, and stage all changes back automatically.
  • git reset --mixed, which is the default, and keeps all files the same but unstages the changes. This is the most flexible option, but despite the name, it doesn’t modify files.

The difference between soft and mixed is whether or not the changes are staged. Staged is basically an in-between zone between the local directory and Git history. git add stages files, and git commit writes them to the history. In either case, the local directory is unaffected, it just changes the state of Git’s tracking of those changes.

Should You Ever Hard Reset?

You should really only be using soft or mixed resets, but, if you messed up your repository so bad that you need to completely reset it, you can use the following sequence of commands to fully set it back to normal.

How to use ?

As seen below, after getting log record number with git log command we can see many records in there.

It is used as git reset — soft 09ae47c98534c2e4 (blue marked) to delete the part selected in blue. After doing this, when we use the git log command again, we see that there is no more after this point.

Main issue is here that it resets after selected point.

GITHUB

Let’s go through an example to understand the relationship between git and github.

After coming to our github account, let’s create a new repository with the new command from the repository section.

After naming, click on the create button.

We called our new repsository as EXAMPL.

On the page that appears, there are a few methods to connect git and github in local.

To use the marked method, we copy the part marked in blue and paste it into the local pycharm terminal.

This means that local and gihub will connect each other.

git push origin master

After providing the connection, we send the file to github with the git push origin master command. Here, the master word depends on which branch we are working in. In this case we are in master that’s why master was used.

After that point, when we come back to our github account in repositories we can see that our file is here.

Pull Request

In their simplest form, pull requests are a mechanism for a developer to notify team members that they have completed a feature. Once their feature branch is ready, the developer files a pull request. This lets everybody involved know that they need to review the code and merge it into the main branch.

Let’s say we create a new branch in local and work there and add a line of code to our file.

After making these changes git add * and git commit -m commands are used for making our file traceable.

We use the git push origin new_branch command to send this new featured branch we created to github.

Sample codes of these operations are also seen below.

  • git checkout -b new_branch provides creating new branch and getting into new branch. Easy way.

After that when we refresh our github repository, we get a message that a branch has been submitted.

When we scroll down the page after clicking the message, we will see that the changes made are shown in green.

With the merge button, we merge our master file with the branch files we sent later.

To learn more about using Git and GitHub, check out the resources at the following links:

https://www.cloudsavvyit.com/12962/how-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store