Philipp Conen

Engineering meets hobby.

Working with Git & Github

Introduction

Within my website project, I started using Github.

Generally, Git not equals Github. Git provides a framework, to build code and work on it efficiently. The best reason to use Github is the aspect of worldwide collaboration.

With this post, I like to share my new knowledge using Git and Github in the aspects of

  1. Installation
  2. Start a new project
  3. Use an existing project
  4. Usage of branches
  5. Pull request

1. Installation

For sure you first need to install Git with something like

sudo apt-get install git 

or

sudo pacman -S git 

You can also check if Git is already installed and in which version

git --version 

2. Start new projects

While performing inside the project path which should be linked with Git, one can initialize a new repository using

git init

Have in mind that this repository exists only locally at this state of time. While generating a new repository, git will send out a warning, that the default branch name would be master. Don’t worry we will come back on this later.

One can check the status of the repository, e.g. for new or changed files using

git status

Running this command right now will only tell you

  • which branch is active,
  • that there are no commits, and
  • the repository is out of data.

So a good idea would be to add a new file, e.g.

touch testFile.txt

Now that there is an undetected file (check out git status again), one can simply add it to the repository using

git add testFile.txt

or add several new or changed files using

git add --all

In the next stage, one can declare the latest changes. This is the huge advantage of Git because one can trace back what was changed at which point in time. Just use short and precise words here, e.g.

git commit -m "Added an empty test file"

Up to this point, everything was handled locally and in use of Git only. With Github one can publish the code online. This requires an account and the creation of a repository for the working project.

After signing up to Github, one can easily create a repository by performing on Github. There should be a button called “new”.

image

Create a new repository on Github

Now the repository is created, it can be linked to the local Git repository.

git remote add origin https://github.com/yourAccountName/yourRepository.git

The necessary link (HTTPS or SSH) could be found in the new Github repository.

image

Get the repository address

Afterward, it could be useful to let Git know who is operating here. This could be globally done for all projects on the system using

git config --global user.email "my@mail.com"
git config --global user.name "Your name"

Finally, one can push (upload) the project to Github using

git push -u origin master

3. Use an existing project

If one wants to work on an existing repository or just use the code, it can be downloaded via

git clone https://github.com/username/repository.git

Therefore, the HTTPS or SSH address can be found on Github (see section 2).

Like mentioned in this post there is no need to initialize the project before or after the cloning process since it is a combination of

  • git init
  • git remote add
  • git fetch
  • git checkout

4. Usage of branches

If one wishes to extend the project or try out a few things it could be useful to work with branches.

Besides the pre-existing primary branch (master), it is possible to add new branches. While creating and working with a new branch

git checkout -b newBranch

one can push forward the work without changing the basic project.

After the changes are done and everything works fine, first change back to the primary branch (master)

git checkout master

, and simply merge the new branch (newBranch) with the primary branch (master).

git merge newBranch

5. Pull request

For me Git and Github are also interesting for working on the same project but from different devices. This works great by performing as followed.

Check out in primary branch (master) and download (pull) the newest version of the project

git pull origin master

Make sure you are on the correct path of the project.

To follow the building process of the project, it can be useful to view the timeline of the project using

git log

Git also provides the possibility to travel back in time, by using the reset function

git reset strangeCode

using a “strange Code” which identifies the different time steps in git log.

The user also can step back one specific stage

git reset current~1

or several stages - n times -, outgoing from the current project stage.

Conclusion

To help new users like me, I shared the commands and workflows which helped me to use Git and Github. We started with the basic usage and the general workflow of a new project. Also, the aspects of using existing projects (cloning), branches, as well as pull requests were discussed.

As learning is a process that never ends, I will update this post, since new content seems to be helpful in my eyes.


Sources

Links Latest Access
Git beginner tutorial 19.05.2021
Git clone explanation 23.05.21

Attachments

Here I’d like to share a collection of the above presented commands.
Click here to open.

Last updated on 4 May 2021
Published on 4 May 2021
 Edit on GitHub