Git(ing) It Together!

Written by hadizat | Published 2024/01/19
Tech Story Tags: version-control | version-control-system | git-workflow | github | git-tutorial | git-repository | host-your-project-on-github | programming-tutorial

TLDRVersion control, also known as source control, is the practice of tracking and managing changes made to codes and files, and a version control system is software designed to automate this process. With version control systems, developers can manage a whole project and work on the project simultaneously. A good version control system allows effective collaboration and enables faster development, maintaining a complete history of activities and changes within your codes and files and allowing you to return to a previous version if needed.via the TL;DR App

Basic Workflows of Version Control with Git

Version control, also known as source control, is the practice of tracking and managing changes made to codes and files, and a version control system is software designed to automate this process. With version control systems, developers can manage a whole project and work on the project simultaneously.

A good version control system allows effective collaboration and enables faster development, maintaining a complete history of activities and changes within your codes and files and allowing you to return to a previous version if needed.

Git is an open-source version control system.

Let’s Get Started on Using Git and GitHub!

GitHub is a popular hosting site for git; this is where all your repositories (project files) will be hosted.

Create an account on GitHub.

Install Git:

Ensure that is installed on your terminal or command prompt. Use the following command to confirm if you have git installed:

git -—version

If you do not have git installed, you can install it via the link below:

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Using Git and Hosting a repository on GitHub

  1. Clone an existing repository, and work on it on your local machine (PC).

    1. Create a new repository (on GitHub.com)

    2. Fill out the details, and ensure to check the initialize box (this adds a README file to the repository). This way, you will be able to clone the repo.

    3. Copy the repository’s remote URL (HTTPS) by selecting the code tab

    4. Clone the repository by using the following command in the command prompt or terminal:

      git clone <remote-url>
      

      Ensure you are in the directory where you want the project to be cloned.

    5. Now, you can modify, rename, delete, or add new files. After the modification, you have to stage and commit your files.

      1. You have to check the files you modified using the following command;

        git-status 
        
        #“git-status” informs you of the latest modifications since the last commit.
        
      2. Now, you can add files to the “Staging Area.”

        Just because a file was modified doesn’t mean it will be part of the next commit; you have to explicitly decide which changes you want to commit. You can add the files with the following command:

        git add <filename>
        
        git add <file1> <file2> <file3>
        

        Or you can add all files with the following command:

        git add .
        
      3. Commit staged changes

        To commit, you need to enter a relevant message to indicate what modifications were done in that particular commit. This message can be useful later to track the change history. Using the following command, you can commit your changes, where “Initial commit” is the message in this case:

        git commit -m "Initial commit"
        
      4. Now push!

        To put your work on GitHub, you have to “push” your files from your local machine (PC) to the remote repository. To do this, you have to know the name of the remote repo; the default name for the remote repo is (mostly) origin. You can use the command below to confirm the name and to push your files:

        git remote
        
        git push origin master
        
  2. Initialize the project directory (local) as a Git repository

    1. Create a new repository on GitHub.

    2. Navigate to the project directory in the terminal or command prompt.

    3. Initialize the local directory as a Git repository using “git init”:

      git init
      

    4. Add files to “staging”; this stages them for commit:

      git add .
      #This adds all files
      
      git add <file1> <file2> <file3>
      #This adds specific files
      

    5. Commit staged files in your local repository.

      git commit -m "Initial commit [or any message of your choice]"
      
      #Commits the changes and prepares them to be pushed to a remote repository.
      

    6. Copy the remote repository’s URL from the Quick Setup page:

    7. Now, in the command prompt or terminal, set the remote repository where your local repository will be pushed:

      git remote add origin <remote-url>
      

      Use the following command to confirm the new remote URL:

      git remote -v
      

Now push!

Use the following command to push the changes in your local repository to the remote repository you have set:

git push -u origin main
Inspecting Commit History
git log

This command [“git log”] lists all the commits that were saved in sequential order. This allows you to see which changes were made in detail, the author of each commit, the date of the commit, and the commit message.

Branching and Merging

By default, all commits go into the “master” branch which is created by default by the “git init” command. A branch is a movable pointer to the latest commit in the repository.

Branches are needed especially when working on multiple parallel developments. Branches provide a context that keeps your work and changes separate from other contexts.

Creating a New Branch

Using the command below, you can create a new branch anytime you want to start a new feature, or fix a bug (don’t worry because when you mess things up, you mess up only in this context [i.e., the branch you created and are working in]).

git branch <bug-fix>

#in this case, bug-fix is the name of the new branch. You can name it anything you like.

This creates a new branch called “bug-fix” in the repository.

Switch Branches

We are still in the context of the master branch to switch to the new branch; use the following command:

git checkout <bug-fix>

Every commit made will be recorded in this branch and kept separate from other contexts until you switch branches. You can list out all branches using the following command:

git branch

Merging

Once you are done working on your new branch, you might want to integrate it into another branch. To merge into another branch, first, you have to switch to the branch you want to merge into using:

git checkout master

#in this case I want to merge the “bug-fix” branch into the “master branch” so, I have to switch to the "master" branch.

Then, use the “git merge” command to merge your changes.

git merge <bug-fix>

#use git merge with the name of the branch you want to merge
#bug-fix is the name of the branch that I want to integrate in this case

Collaborating With Others via Remote Repositories

Publishing a Local Branch

To share your local branches (the branches created in your local repository), you need to publish them to the remote repository. To do that, you can use the command below:

git push -u <remote> <local-branch>

git push -u origin bug-fix

#in this case, "origin" is the remote and "bug-fix" is the local branch name

To confirm that the branch has been pushed, head to Github and select the branches drop-down; you should see the new branch there.

Staying Up-To-Date with Remote Changes

When collaborating with others, it is necessary to stay up to date with their changes. Using the command below informs you of changes made to the remote repository but doesn’t integrate them into your local.

git fetch <remote>
Integrating Remote Changes

Using the “git pull” command, you can integrate the changes from the remote repository. This will update your current HEAD branch (the branch you are currently working on) with the changes from the remote branch. These changes will be merged into your local.

git pull
Upload Local Changes to the Remote Repo

To upload the changes in your current HEAD branch, use the following command to push the changes:

git push

Viola!

I know, this was a long one, but now you know the basic workflow of Git and how to host your project (files) on GitHub.


Written by hadizat |
Published by HackerNoon on 2024/01/19