Git and GitHub – how to start.

By | 24th March 2019
Git and GitHub

Installation on Linux

First of all, we have to download and install Git on our machine.

Click for Git Download <- recommend for Windows users.

Open terminal and type:

$ sudo apt-get install git

And that’s it. We have already installed Git, let’s check which version we have. Enter:

$ git --version

Initialise Git repository

Just in case, I would highly recommend doing this entire small tutorial, before you start work with your own project. When encounter some difficulties it’s much easier to come out of the problem working on a ‘demo project’ instead of making a stressful situation i.e lost a few files with source code.

Prepare a few files and folders which can make a simulation of our app.

Files can contain whatever you want. I recommend to fill them in something like this: The first line of the test app.
etc…

Go to folder and use below command for initialise Git repository environment.

$ git init

Configure environment (user / email)

$ git config --global user.name 'Peter' # press Enter
$ git config --global user.email 'name@domain.com' # press Enter

Create, add and delete files

$ touch settings.py  # create new file
$ git add settings.py  # add file to repository
$ git add *.py  # add all .py files
$ git add .  # add all files
$ git rm --cached settings.py  # remove file

Ignored files

For some reasons we will not add all files to our repo, the best option is to create a file which contains a list of our folders and files. Let’s do below example.

$ touch .gitignore  # create a file 
$ kate .gitignore  # open a file in very simple Linux editor

Add a list of files and/or folders line by line to be ignored when commit.

i.e:

/Dir2
views.py
$ git add .  # add '.gitignore file' to repo
$ git status  # check status of your repo

Git Commit

For first time round type below command in the console.

$ git commit  # it will open a file where you have to un-comment 'Initial commit' and save. 

And now let’s change something in our file. For example add another one line in your index.html file. After changes we can go directly to below command:

$ git commit -m 'add changes to index file'
$ git status

Git Branches

As you noticed we work on the ‘master’ branch. I advise creating a new branch when building new features. In a few steps, let’s create a new branch named ‘develop’ and switch to the branch.

$ git branch develop  # creating new branch
$ git checkout develop  # switch
$ git status  # and check are we on right branch

And now let’s make a small exercise which shows us a difference between ‘master’ and branch. Create a new file, also edit file index.html belongs to ‘master’ (add a new line of text in it).

$ touch test.py  # create a new file
$ kate index.html  # edit a file belongs to master and save
$ git add .  # add all files to repository
$ git commit -m 'Create test file'
$ git checkout master  # switch back to master

As you probably noticed there is no test.py file and no changes in index.html file. It means that we worked in a different branch and our main app stays untouched.

Git Merge

In generally speaking if we work at some features for our app the good practice is using branches. When a job is done we can merge our features to the main project.

$ git merge develop

After this command an editor will pop-up with a statement to write a message about the reason for merge. In short: ‘Added test file’.

Remote Repository

First of all, we have to create an account on GitHub if we don’t have one.

https://github.com

Create a new repository. See attached screen below.

https://github.com/new

The last thing to do is connect our project with the remote repository is copy and paste a marked line of command to console.


$ git remote add origin https://github.com/pbwis/training.git
$ git remote

After that our project is connected with GitHub.

Push Repository

And now we should push our repository. During this process, we do have to enter login and password to our GitHub account. And after that our files and folders (except Dir2) are on the GitHub.

$ git push -u origin master

Pull and Clone

Imagine that you work in software developers team and someone made an update, it’s very simple to download the latest version of the code. Just use a ‘pull’ command.

$ git pull  # as result you should see Already up-to-date

If you don’t have a repository at all – clone it. Go to GitHub and under green button copy the ‘http link’.

$ git clone https://github.com/pbwis/....git

And that’s it. Of course, this is a minimum amount of information but I think enough to run and develop our new project. Most of the developer’s apps (PyCharm) has built-in support for GitHub. But as I mentioned every software engineer should know basic GIT commands.