DEV Community

Cover image for Git: A beginner's guide
Abdur-Rahman
Abdur-Rahman

Posted on

Git: A beginner's guide

Welcome to this week's article peeps 👋, where I would be talking about git, why it's important to you as a developer/engineer and how to get started with it.

🔰 Introduction:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. In short, git is a version control system. A Version Control System (VCS) is a tool that helps software developers keep track of how their software development projects change over time 🔃.

Each snapshot or state of the files and folders in a codebase at a given time can be called a "version". Version control systems were created to allow developers a convenient way to create, manage, and share those versions. It allows them to have control over managing the versions of their code as it evolves over time. It also enable collaboration within a team of software developers, without losing or overwriting anyone's work (it prevents intra-company fights and brawls 🔥) . After a developer makes a set of code changes to one or more files, they tell the version control system to save a representation of those changes.

In this way, a history of code changes - or versions - is built up as the code evolves. This history is preserved so that all developers, even if they are working in totally different locations, have access to a consistent and up-to-date history of the project. A version control system can also be referred to as a source control system, source code management, version control software, version control tools, or other combinations of these terms.

Version control discussion sourced from IC.

🐱 Github:

First thing to note: Github is not Git! Github is a platform (a service) used by developers (who use git) to upload and download resources and source codes. Git is used to connect and interact with Github to download and upload resources. There are other platform aside from Github used for versioning, some of which are GitLab, GitBucket, etc. With over 73 million user base, Github is a very popular and a favorite of many. If you don't have an account yet, head over there and set one up now!

Github Profile Page

A typical Github user's profile page.

📩 Installation:

Head over to Git.com to download git installer, it can also be installed via the command line but let's do it the easy way 😉.
Git Download Page

After downloading and running the installer, the next thing to do is test that the git installation was successful. Navigate into any folder using the cd command. Run the command git init. If you get the message "Initialized empty gi...", voila! you've had a successful installation!

git init

🔧 Connecting Git to Github:

Let's connect our git to github, first, let's configure our name (Github username) and email. Run these two commands:

git config --global user.name "Your name here"
git config --global user.email "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

After that, run the command:

ssh-keygen -t rsa -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

This creates two files, ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. On windows, head into your C:/username folder and look for a folder called .ssh (You might need to enable "Show hidden files" to see it). On Linux, it would be in your "Home" folder (*also a hidden folder). Navigate into that folder and open the one called id_rsa.pub.
ssh file

You would see a long string of gibberish in there ending with your email, that's good. Head over to Github and go to your settings
Settings
Head to the "SSH and GPG" keys and click "New SSH Key" in the top right corner. Give your ssh key a name and paste that gibberish in the id_rsa.pub file. Click "Create key" and head back to your terminal. Run the command:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

If you get a message like: "Hi username, ...". Then, mission successful!

🔖 Git Keywords:

Let's discuss some of the terminologies you would find in the git ecosystem (including GitHub), after all, you need to be able to speak like a version control master 😄. I would be using remix-pwa as an example here.

  • Repository: A Git repository (also called "repo") tracks and saves the history of all changes made to the files in a Git project. This information is used by online platforms like Github to store, track and display resources (mostly source codes) in a graphical format.

Repository

A repo example on Github. This can be used to view changes and if needed, reverse those changes. Plus, Github gives a lot more features.

  • Branch: A branch is a version of the repository that diverges from the main working project. Imagine a project, that has multiple versions that don't interfere with each other and these versions can change independent of the others. A git repo can have several branches.

Branch

A branch system in a repo, each of these branches contain different versions of code of a particular project. One benefit here is that I can compare across branches and edit a project's source code without "destroying" the project itself. Think of a project as a tree, and different versions as different branches.

  • Fork: Fork or forking is a process of duplicating someone else's project (or code) in order to make edits or changes. One of the ethics of being a developer or engineer is to never edit someone else's source code directly, if you wish to make a change, you duplicate that person's code as your own (you fork that person's project)

Fork

If I wanted to make a change to this person's work, I would fork it then edit, and not edit it directly.

  • Pull Request: Pull requests (also known as PR) is the process of trying to merge to seperate code together. Imagine this scenario, we realized a mistake in someone's code (could be a team member or just an Open Source work) and we want to fix that bug 🐛. The steps we would take would be:
  • Fork the project
  • Make the changes and fix what we intended
  • Open a PR (request the owner of the original project to merge the two codes together)

Pull Request

This is an example of a merged PR. We have two different versions (two different branches in this case) and we want to combine their content. We open a PR, and then if the change we want to merge is valid (has no bugs or issues), we merge.

  • Staging: Staging is a used to add files of a working tree (directory) to a snapshot. This is used to prepare the files for a commit.

  • Commit: A commit is used to record the changes in the repository. The staging and committing are co-related to each other. Staging allows us to continue in making changes to the repository, and when we want to share these changes to the version control system, committing allows us to record these changes.
    Commits are the snapshots of the project. Every commit is recorded in the master branch of the repository. We can recall the commits or revert it to the older version.

  • Clone: Cloning is used to make a copy of the target repository locally or "clone it". If for example, we want a local copy of a repository from GitHub, this tool allows creating a local copy of that repository on your local computer.

  • Merge: Merging is a process to put a forked history back together. It is simply just the merging of two seperate code history into one branch.

  • Issue: An issue is a Github thing (I don't know any other providers that offer it too 😅) that is used to keep track of bugs, issues, requests and features in a repository.

Issues

An issue example, in this case, there is an issue with using this project that the developer hasn't fixed yet. A user can easily come here and contact the developers directly who can offer solutions, fixes or alternatives.

  • Push: Pushing is the process of moving (more like copying) a local repo to a remote repo. A remote repo means a repo stored on the cloud, in a database somewhere. It is used for example, to move code from your computer to github.

That's ten popular keywords used in git and github for you guys 😄, there are a lot more you can read up on by yourself. If you know of anymore and want to share, let's hear it in the comments!

📝 Example Time:

That's enough talk! Let's do a miniature project and implement git in it. Open up your terminal and navigate to a directory (as always, I would be using "Documents"). Run the command:

mkdir git-example
Enter fullscreen mode Exit fullscreen mode

The command mkdir means "make directory" and is used to create a directory in the folder we're in (in this case, git-example). Afterwards, use the cd command to navigate to the newly created folder. Let's now create the files we would upload to github, as a person with Javascript running in their veins, we would create a very simple js example (don't worry if you don't know js). Run the command:

touch example.js
Enter fullscreen mode Exit fullscreen mode

The command touch creates that particular file in our current directory, in this case, example.js. Open up an editor and let's write a very simple conditional statement in JavaScript.

const a = 12
let b = 13

b = 12

if (a == b) {
  console.log('a and b are equal')
} else {
  console.log('a and b are not equal')
}
Enter fullscreen mode Exit fullscreen mode

this code initializes two variables, a and b with two different numbers (12 and 13). We later assigned 12 to the variable b. We then had an if statement, if a is equal to b, then log a message in the console, if not, log a different message. Okay! Let's push to Github!

Open up your github's repo page and click "New Repository". I would be calling mine "DEV-example", make sure the visibility is set to public (other users would be able to view your source code). Add a description for your repo and skip the remaining questions and boxes below, click "Create repository".

Head back to the terminal of your project and run the commands:

git init
git remote add origin <URL-of-the-repo>.git
git add .
git commit -m "Initial commit"
git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Let's run through each command, git init initializes an empty, local repo in your project (a .git folder).

git remote add origin <URL-of-the-repo>.git is used to tell your local repo the location of the remote repo, where on the cloud are we connecting to, to store our repo.

git add . is used for staging, normally, we use git add <name-of-file> but as a shortcut, you can use . to stage every file in the directory (except the .git local folder).

git commit -m "Initial commit" is used to commit our project. The -m part is called a "flag". Flags are used to pass arguments to a command, in this case, the flag -m stands for "message" (Every commit must have a message, usually, this should contain a brief description of what your changes consists of or what they do). The text between the double quotes is the message argument we want to pass to the commit command.

git branch -M main is used to change our branch (also known as rebasing). The -M flag here means "move" and what it does is "move our current folder content to the branch called 'main'".

git push -u origin main is used to push our local changes to our remote repo. The -u flag is used to track that repo changes (useful when pushing to a remote repo for the first time). It means "set up an upstream tracker to track changes". origin main is telling git where to push to, which branch should it push to, in this case, our branch "main".
git

I forgot to authenticate my github and forgot to rebase my repo to main 😅😅

Head back to your github repo and refresh the page, voila!
done


That's it for this week's peeps, I would still try and write as often as I can but it would be an irregular writing schedule 😅. Keep the comments flowing with questions or observations, we would love to hear them. Remember to keep reading, learning, and coding. And don't forget to play now and then! Love you guys, cya in the next article 👋.

Top comments (3)

Collapse
 
sonicx180 profile image
sonicx180

This is not working for me.

Collapse
 
shafspecs profile image
Abdur-Rahman

What exactly isn't working?

Collapse
 
sonicx180 profile image
sonicx180

like the command to sign in to git. ssh git@github.com