Git – Learn to push your code and some tips

Git : What is it ?

Git is a very popular version manager and is widely used today. Originally, Linus Torvalds, the creator of the kernel of the well-known Linux operating system. Git was developed in 2005.

This is called a commit to make a change or modification. Git’s success compared to other alternative tools can be explained by its performance, security and flexibility.

  • Performance : algorithms to commit changes, create branches, compare old versions, make merges have been optimized for performance.
  • Security : It ensures the authenticity of the history of your source code thanks to its SHA1 cryptography algorithm.
  • Flexibility : It allows non-linear development processes and is compatible with many systems and protocols.

To transport the data, you can use SSH (Secure Shell), the Git protocol that uses a background process (deamon). Its disadvantage is that it is not secure unlike SSH and finally HTTP/S.

SSH is preferred most of the time.

Install Git on Linux

There are several installation methods on Linux. I show the one that I think is the simplest.

Install it from your terminal in this way on a Debian-based system, such as Ubuntu

$ sudo apt update
$ sudo apt install git 

On a system such as Fedora

$ sudo yum install git

You can check the version by typing the following command :

$ git --version 

Create an account with the Github code host and add your user address and email address that you previously entered to the following commands

Git: Person wearing a Gthub T-shirt

Configure it with your username and email address

$ git config --global user.name "John Doe"
$ git config --global user.email "[email protected]"

To check your settings :

 $ git config --list

Hub – GitHub’s command line git extension without ever leaving the terminal

Install Hub easily with snap

 $ sudo snap install --classic hub

Before taking our first steps, you can start by following my step-by-step guides in the following order: Ruby on Rails : The 3, 2 and 1 ways to install the framework on Linux, and New Rails project – Starting a new Ruby or React project

First steps and use to push your code on Github

If you have followed the previous step-by-step guides, we will start by initializing the Git repositories from our existing project.

Go to your existing project, for our example our folder is called helloworld

$ cd helloworld 

Initiate the Git repository of your helloworld directory

$ git init 

This command creates a subdirectory named .git that will be used for version tracking

git : subdirectory

Place your files under version tracking

$ git add .

Validate the informations

$ git commit -m 'First commit' 

Create the repository on your chosen code host, I use Github in my example

$ hub create 

To check your remote connection to your newly created remote repository

$ git remote -v 

You must obtain the following result :

origin [email protected]:pccindow/helloworld.git (fetch)
origin [email protected]:pccindow/helloworld.git (push)

Push your code

$ git push origin master 

You can check your Github account to make sure you find the code you just pushed

From now on, each time you have made progress on your project, you will save your work, which will be both locally on your machine and also on your Github account to make sure you don’t lose anything !

To check the status of your files and know which branch you are on

$ git status 

Create a new branch and give it a name. In this example we call it “commit”

$ git checkout -b commit

Places files under version tracking

$ git add . 

Some information will be given to you such as the number of files modified, statistics on the lines added and deleted in this commit.

Validate the modifications of the branch on which you are, here “This stuff is done”.

$ git commit -m 'this stuff is done' 

Finally, you push your work on the Github repository

$ git push origin commit 

Here is a new command: the “pull-request” allows you to inform others of the changes you have made. Once a request is sent, project stakeholders can review the changes before validating them.

 $ hub pull-request commit

Some shortcuts for your Git commands

To avoid having to type the entire text of a command, it is possible to define an alias for each of the Git commands.

You will gain in productivity 😉

git: be more productive

Here are some examples :

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status 

From now on, you can chain the following commands and push your code :

$ git st
$ git add .
$ git co -b 'nom_de_la_branche'
$ git ci -m 'this stuff is done'
$ git push origin 'nom_de_la_branche'
$ hub pull-request 'this stuff is done' 

As a bonus, the tig tool to view the different commits

Tig is an improvement of the git log. This way, you can see the status of your branch and the different commits.

To install it, it’s very simple

 $ sudo apt install tig 

From your project, enter the Tig command

 $ tig 

Here’s an overview !

If you also have other tips or tools that make your tasks easier, come and share them in comments !

Sources

https://git-scm.com/docs

https://github.com/github/hub

https://github.com/jonas/tig

signature Pierre-Christophe

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.