Skip to main content

Command Palette

Search for a command to run...

Git And Github For Beginners: learn the basics of git and github.

Published
โ€ข10 min read
Git And Github For Beginners: learn the basics of git and github.
V

I'm developer and technical writer passionate about creating exceptional software solutions and crafting engaging technical content. With a strong background in software development and a flair for effective communication, I bring a unique blend of technical expertise and writing skills to my work.

When collaborating on projects, it's essential to track changes and updates. Git and GitHub are tools that facilitate version control and collaboration, enabling team members to stay informed and contribute effectively to shared projects. This article explores how Git and GitHub help manage files and streamline group project workflows. This is the first part of a series on git and github.

What is git?

Git is a distributed version control system that allows users to keep track of file changes. Each change is stored as a snapshot and is referred to as a version, which can be recovered at any time.

What is version control?

Version control is a system used to keep track of changes made to files and documents over time.

There are two main types of version control systems;

  • Centralized version control: This type of version control uses a central server that houses the project's main repository; each team member submits their modifications to the main repository.

  • Distributed version control :this type still uses a centralized server that holds the main repository, but each user has their own working copy of the project, and team members can contribute and stay up to speed on teamwork via push and pull requests.

distributed version control has gained more importance and popularity in recent times because it has a greater advantage than centralized version control.

Why use version control systems?

  • It tracks changes made to our files or projects.

  • Every change is referred to as a version and can be restored at any time.

  • It serves as a backup and can be used to retrieve files in the event of an unexpected loss (distributed version control).

Getting started with git

Installing Git

Click here to install git.

Confirm the installation

Open your terminal/git bash and run the command

git --version

If you installed git correctly, you should see the Git version in your terminal. You are now prepared to use git.

Git commands

Git offers a wide range of commands to manage version control and collaborate on software development projects. Here are some commonly used Git commands:

  • git config: used to set up Git configuration options, such as username and email.

  • git init: creates a new Git repository in the current directory.

  • git add: adds changes to the staging area.

  • git status: displays the status of files in the working directory.

  • git commit: records changes in the repository with a commit message.

  • git push: uploads local branch commits to a remote repository.

  • git branch: lists existing branches and shows the current branch.

  • git checkout: switches to a different branch.

  • git merge: merge changes from one branch into another.

  • git pull: downloads and incorporates remote changes into the current branch.

  • git clone <repository link>: downloads a remote repository and creates a local copy.

There are a lot of git commands but those listed above are the main focus of this article. You can use git help -a to see the list of all git commands.

Git config

Configuring git is vital because it keeps your information and uses it as an identifier on every commit and modification made by you or any other user.

It is recommended that you use the same login and email address on github for easier identification.

Add your username

git config --global user.name "Your name here"

Add your email

git config --global user.email "your email here"

Your git has been configured successfully.

Git init

Initialize a repository: A repository is similar to a house in that it stores all of your project's data, codes, and documents, as well as a history of all modifications made and who made them. Initializing Git implies generating a new, empty repository for your project.

Follow the steps below to initialize a git Repository

  1. If you do not have an existing project folder, create one.

  2. In your git bash terminal, navigate to your project folder.

  3. Use the git init command to initialize an empty repository on the folder

The master indicates that you are on the repository's default branch, where you can now add and commit your files and changes.

Git add .

The command git add. is used to add all files and changes (including additions and deletions) to the staging area in preparation for a commit.

Add the files to the new repository (you can do this by opening the git initialized folder in vscode and adding files like index.html to it then run git add . in the terminal to also add the files to the staging area).

If you don't want to add all the files, you can specify which file to add with git add <filename>

git add README.md

Git commit

The commit command updates the repository with all staged files. This command adds your files to the repository; to describe your commit, include a commit statement in quotes.

git commit -m "Added some files"

All the files in the git-demo folder have been successfully added to the git repository.

Hosting Your Repository on GitHub

What is GitHub?

GitHub is a git remote hosting platform. It is a web-based tool that allows developers and users to work together on projects, share code or files, and so on.

Getting started with gitHub

  1. Go to the official github website and sign up with your username, email, and password (ideally the same ones you used for Git configuration).

  2. To create a new repository, click the + button in the upper left corner of your screen.

  3. Enter your repository name and optionally a description, then click the Create Repository button.

  4. Connect your local repository to your remote repository using HTTPS or SSH. (Using the HTTP approach.) Copy the HTTPS link and then navigate to your terminal; this transfers data from your local repository (git) to the remote repository (github).

In the terminal, run git remote add origin <HTTPS url>

git remote add origin https://github.com/VilmaScript/git-demo.git

The git remote add origin <url> command establishes a link between a local repository and a remote repository. It associates the provided URL with the remote repository name "origin," allowing you to interact with the remote repository using that name in subsequent Git commands.

Git Push

git push uploads local repository commits to a remote repository, allowing you to share your changes and updates with others working on the same project.

Run git push -u origin master to push the local commits from your local repository to the remote repository, making master the default branch for the commits made.

git push -u origin master

when you execute git push -u origin master, you are pushing the local commits in your current branch to the master branch of the origin remote repository. The -u option sets the upstream branch, so in the future, you can simply use git push without specifying the branch and remote name, as it remembers the default.

Return to your github account and refresh to check that the files in your local repository have been successfully added to your github repository. When you make modifications to these files, such as adding new lines of code, you can use git push to push them to github. You just made your first github push, congratulations.

Git branching

While making modifications to your live projects and adding new features, the user experience, functionality, or even the website's availability may suffer.

Git branching creates branches for developers to work on in the meantime, after which the modifications to be updated can be merged into the master branch.

Any new branch generated receives a copy of all the files and code in the Master branch, but changes in the new branch do not reflect on the Master unless Merged (more on git merge later).

Steps for git branching.

  1. In the terminal,while still in your project folder, use the git branch command to confirm the branch you're on.

     git branch
    
  2. Create a new branch with the command below

     git branch <branchname>
    
  3. Migrate to the branch adding your changes using the command below

     git checkout <branchname>
    
  4. or you can use the command below to create a new branch and migrate to it immediately (steps 2 and 3 are not needed).

     git checkout -b <branchname>
    

Now that you've generated your first branch, you can see that the branch has changed from the default master branch to the newB1 branch, as shown below. You are free to use whatever branch name you want.

Git merge

git merge is a command used to combine changes from branches other than master into the master branch. Let's edit the newB1 branch and add some HTML tags to the index.html.

then run git add . followed by git commit in the terminal, save changes to the git repository on the newB1 branch.

Run git checkout master to switch to the master branch and then return to vscode, you will notice that the changes made on the newB1 branch are not visible on the master branch.

Now merge the changes into the master branch.Check that you're on the master branch; if not, use git checkout master to switch or use git branch to confirm the current branch.

git merge <branchname>

this should merge the changes to the master branch as seen below

While still on the master branch, open Vscode to examine if the changes on the branch are visible in the master branch.

Note: The current branch is shown at the bottom left of your vscode.

Git pull

Using the git pull command, you can fetch and integrate changes from the github remote repository into your local repository.

Create a readMe file in the project's github repository. Add some text, then click commit.

go to git bash and run the git pull command

Open vscode code and see that the readMe file has been added.Hurray!!

Git clone

This command creates a local copy of a remote repository.You might wish to access another user's repository on github in order to access the files in the repository.This may be required when following Tutorials.

Using the Javascript30 challenge repo by wesbos as example;

Click the green code button, copy the "HTTPS url", and then run the git clone <https url> command.

The repository has been cloned successfully to the local workstation. You can now work with the files.

Forking a github repository

A fork is a process of generating a personal clone of a repository under your own GitHub account in Git. When you fork a repository, GitHub duplicates it, including all files, commit history, and branches, and creates an independent copy under your account.

The forked repository is not added to your local workstation automatically. The forked repository is established under your account on the GitHub server.

Click Create New Fork, and then click Create Repository. Check your github repository list; the cloned repo will be there, ready for you to use.

To use the forked repository on your local workstation, clone it exactly like you would any other repository(following the same procedure for Git Clone)Cloning copies the forked repository to your workstation, allowing you to make changes, create new branches, and work on the project locally.

Alternatively, you can clone a repository directly to your local machine and then push it to your GitHub repository without forking.

Deploy project using github pages

Deploying a project implies making it accessible and available to internet users,you can achieve that using github pages through the following steps

  1. Click on settings and then select pages from the menu on the left.

  2. Choose the branch to deploy from and the root directory as the source,(select a domain (optional)) then click Save.

  3. Your project is now live.

Conclusion

Git and GitHub are effective collaboration and project management platforms. Understanding version control, branching, and the features of GitHub provides you with critical skills for efficient development. While further practice is required, this tutorial gives a solid foundation for becoming skilled in using git and github. You can now use them for your personal and group projects.

Please feel free to like, comment, and share. I welcome constructive criticism, so please don't hesitate to point out any errors. If you have any questions, please leave a comment. Meanwhile, watch out for the second part of this git and github series, Thank you.

I

Good keep going and running don't stop. It's personal

A

Wow..This is really helpful ๐Ÿ™Œ๐Ÿ™Œ๐Ÿ™Œ๐Ÿ‘