Mentor Léo: how to share your code using Git & Github 🚀

This series is about teaching Léo to become a professional developer from scratch. Léo can be any of you, desiring to learn and grow 🚀

Learn by doing challenging projects, be mentored to achieve your goals and join a dedicated community ⚡

Apply here, it will always be free ♥ https://mentorleo.co

Problem

When you are working as a team (or just showing your creations at mentorleo.co), you need to save your code somewhere and handle the versions of your code.

Git

Git is a powerful versioning system that is now used by almost every developer: https://git-scm.com/

It works in a distributed way. Every project have an history of commits (code changes annotated with a custom message, auto-generated timestamps and author account). For each project, there is a remote repository (it could be on Github, Gitlab, Bitbucket, etc), and local repositories on each developers’ machines. Here is the workflow of a developer:

  • pull the changes from others = synchronize the local repository from the remote repository (git pull)
  • make code changes
  • check which files have been modified (git status)
  • add the modified files to the next commit (git add <my-file>)
  • save the changes by making a commit (git commit -m “my message”)
  • push the local commit to the remote repository (git push)

You’ll see more advanced commands later in the article.

Github

It’s an extremely popular platform for hosting your code. It’s totally free if you open source your code (aka public). Big projects like Servo are built there.

You are more than encouraged to contribute to open source projects. You will learn tons of stuff, meet awesome developers, and build a strong profile that will be examined by technical recruiters.

That’s why you are required to open source the code you produce within Mentor Léo on this repository: https://github.com/Orbmancer/mentorleo

SSH key

To identify your machine and link it to your Github account, you will need to generate a SSH Key. This documentation will help you: https://help.github.com/articles/connecting-to-github-with-ssh/

Git branches (advanced)

Git is extremely powerful with its branches system that have their own history. The default branch is master.

If you create a branch, you can work on your own separate history and make your modifications. You can show these modifications to your coworkers (while other branches are progressing as well). If your changes are validated, you can merge your branch onto master, adding your modifications to the main history.

Create a branch (-b option)

git checkout -b my-branch

It will copy the history of master at the moment you did that command, and place you on this new branch. Your next commits will be saved in your branch history.

Push your branch the first time (-u option will create it on the remote repository, you will just have to do only “git push” the next times):

git push -u my-branch

Switch branches:

git checkout my-branch
git checkout master

Merge your branch onto another branch (here: master):

git checkout master
git merge my-branch

Git Protips

Add all files from the folder (careful with this, you may not want to add everything)

git add .

Add all modifications from tracked files (-A) and accept/deny each chunk of modification (-p):

git add -Ap

Use a “.gitignore” file to tell Git not to include certain files/folders. For example in a project that uses NPM, you must exclude the node_modules folder. Otherwise you will commit a huge useless folder (the other guys from your team will do a “npm install” to get these dependencies). Example:

We’ll see later more advanced stuff like rebasing which is a complex concept to understand :)

Call to action

Join us ! We are creating a community of motivated Léos helping each other. Learn by doing challenging projects and be mentored to achieve your goals.

Apply here, it will always be free ♥ https://mentorleo.co