Git one01  | A beginner's guide

Git one01 | A beginner's guide

If you are a developer and you want to get started with Git and Github, then this article is made for you.

Today we'll be learning about using Git & Github to help us in developing our project. After a short introduction on what is Git and how to use it, you will be able to create and work on a Github project.

git1600.png

What is Git

Git is a free and open-source software created in 2005 by Linus Torvalds, The Famous creator of the Linux Operating System Kernel. This tool is a version control system that was initially developed to work with several developers on the Linux kernel.

Many control systems exist, like CVS, SVN, Mercurial, and others, but today Git is the standard software for version control.

Git's Version Control

Version control is a management system that takes into consideration modifications you’ve made on a file or a set of files, for example, a code project. With this system, developers can collaborate and work together on the same project.

  • Allowing us to keep track of the changes we make to our code by saving snapshots of our code at a given point in time.

fileshots.png

  • Allowing us to easily synchronize code between different people working on the project by allowing multiple people to pull information from and push information to a repository stored on the web.

fileshareshot.png

  • Allowing us to make changes to and test out code on a different Branch without altering our main codebase, and then merging the two.

  • Allowing us to revert to earlier versions of our code if we realize we've made a mistake.

In the explanation above we mention the word repository, that's just a fancy way of saying folder.

A Git repository is where we'll be storing all of the files related to a given project. These can either be remote (online) or locally (on your machine).

Github

om2x6xyakcnwboojgqu5.png

Github is a for-profit company that offers a cloud-based Git repository hosting service. Essentially, it makes it a lot easier for individuals and teams to use Git for version control and collaboration.

Github is so user-friendly, though, that some people even use GitHub to manage other types of projects, like writing books. see GitBook

Without Github, using Git generally requires a bit more technical savvy and use of the command line.

Let's get started by creating a new repository online.

  1. Be sure to have a Github account set up if you don't, you can create one here.

  2. Click + on the top-right corner and select "New Repository".

  3. Create a name for your repository.

  4. Describe your repository (optional).

  5. Choose whether you want it public (seen by everyone on the web) or private (seen by the people you give access to).

  6. Add a README file if you'd prefer.

reposhot.png

Once we have a repository set up, Let's go ahead and add some files to it. To do that, we'll take our newly created remote repository and create a copy or a clone of it as a local repository on your machine.

  • Make sure you have git installed on your machine by going onto your terminal/cmd and typing: git

if it isn't installed, you can download it here

  • Click the "Clone or Download" button on your repository's page and copy the URL that pops down. if you didn't create a README file, this link should appear near the top of the page in the "Quick Setup" section.

cloneshot.png

  • In your terminal, run: git clone <repository URL>

This will download or clone the repository to your machine. if you didn't create a README file, you'll get a warning: Screenshot from 2021-01-13 14-40-41.png

This is completely normal, you don't need to worry about it!

  • Let's run: ls, which will list all files and folders in your current directory & you should see the name of the repository you just cloned.

Screenshot from 2021-01-13 14-54-54.png

  • Now change the directory into that folder by running: cd repository name/

You should be in the repository by now.

So you need to create a new file in that repository by running: touch <new file name>

You can now make edits to that file. Alternatively, you can open that repository in your text editor and manually add new files.

  • Now, to let git know that it should be keeping track of the new file you've made, run : git add . To track all files within that repository or to track that specific file run : git add < new filename >

Commits

Now, we'll start to get into what Git can really be useful for, after making some changes to a file lets run:
git commit -m "some message" The command will commit those changes taking a snapshot of the current state of our code.

Note. The "some message" text is where you most likely to write changes you've made.

  • After this change, to see how our code compares to the code on the remote repository. We can run: git status
  • When we're ready to publish our local commits to Github we can run:
    git push

pushshot.png

Now let's go to Github on our web browser see if our changes will be reflected.

gitreview.png Yea!! like a charm.

  • If you've only changed an existing file and not created a new one, instead of running: git add . and then: git commit ... We can condense this into one command: git commit -am "some message"

This command will commit all the changes that you've made.

Sometimes, the remote repository on Github will be more up-to-date than the local version. In this case, you want to first commit any changes, and then run: git pull to pull any remote changes to your repository.

Merge Conflicts

One problem that you will most likely face when working with Git, especially when you’re collaborating with other people, merge conflict.

A merge conflict occurs when two people attempt to change a file in ways that conflict with each other.

  • This will typically occur when you either run: git push or git pull.

conflict.png

  • When this happens, Git will automatically change the file into a format that clearly outlines what the conflict is. Here’s an example where the same line was added in two different ways

highlights.png

In the above example, you added styles on the body tag in line 9, and another person working on the remote file decided to keep the style external and removed the style from the body tag on line 11, and now we must choose one of those to keep.

The long number is a hash that represents the commit that is conflicting with your edits. I use vscode but many text editors will also provide highlighting and simple options such as “accept current” or “accept incoming” that save you the time of deleting the added lines above.

  • git log Is another potentially useful git command, which gives you a history of all of your commits on that repository.

log.png

  • Potentially even more helpful, if you realize that you’ve made a mistake, you can revert to a previous commit using the command git reset in one of two ways: git reset --hard < commit >

This reverts your code to exactly how it was after the specified commit.

  • To specify a commit, use the commit hash associated with a commit which can be found running: git log as shown above.
  • To revert your code to the version currently stored online on Github run: git reset --hard origin/master

Branching

After you’ve been working on a project for some time, you may decide that you want to add a feature. At the moment, we may just commit changes to this new feature as shown in the graphic below.

branches1.png

  • The branch you are currently looking at is determined by the HEAD, which points to one of the two branches. By default, the HEAD is pointed at the master branch, but we can check out other branches as well
  • Now, let’s get into how we actually implement branching in our git repositories: git branch to see which branch you’re currently working on, which will have an asterisk to the left of its name.

termbranch.png

  • To make a new branch, we’ll run: git checkout -b <new branch name>

feature.png

  • Switch between branches and commit any changes to each branch run: git checkout <branch name>
  • When we’re ready to merge our two branches, we’ll check out the branch we wish to keep (the master branch recommended) and then run: git merge <another branch name>

This will be treated similarly to a push or pull, and merge conflicts may appear.

More Github features

There are more useful features specific to Github that can help when you’re working on a project:

Forking: As a Github user, you have the ability to fork any repository that you have access to, which creates a copy of the repository that you are the owner of. We do this by clicking the “Fork” button in the top-right.

Pull Requests: Once you’ve forked a repository and made some changes to your version, you may want to request that those changes be added to the main version of the repository. For example, if you wanted to add a new feature to React, you could fork the repository, make some changes, and then submit a pull request. This pull request could then be evaluated and possibly accepted by the people who run the React repository. This process of people making a few edits and then requesting that they be merged into the main repository is vital for what is known as open-source software, or software created by contributions from many developers.

Github Pages: Github Pages is a simple way to publish a static site to the web. To do this:

  • Create a new GitHub repository.

  • Clone the repository and make changes locally, making sure to include an index.html file which will be the landing page for your website

  • Push those changes to Github.

  • Navigate to the Settings page of your repository, scroll down to Github Pages, and choose the master branch in the dropdown menu.

  • Scroll back down to the Github Pages a part of the settings page, and after a few minutes, you should see a notification that “Your site is published at: …” including a URL where you can find your site!

That is it for now till next time!

Thank you for reading to this point, really I appreciate it, seeming this is my first article. So if you found this article useful, don't learn alone. Share it with your friends or other developers who might find it useful.

Please leave a comment, I'd love to hear your thoughts & suggestions on what to write about next.

Let's connect on Twitter for a chat!!