Let's Talk Git And GitHub

Photo by Joan Gamell on Unsplash

Let's Talk Git And GitHub

Git

Git is a version control that tracks changes in computer files. It is mostly used in collaboration among programmers during a software development process. Git comes with a command-line tool for it's users. It was created by Linus Torvalds in 2005 for managing the development of the Linux kernel

Git Concepts

  • Repositories (also known as Repos) - This is a directory or folder that Git tracks changes.

  • Commits - A commit is a snapshot of the repository at a specific point in time. Usually indicated by descriptive messages.

  • Branches - Branches in Git allow separate development. You can make changes to your branch and only merge it to the master once done with making changes

  • Merging - Merging is the process of adding the changes from one branch to the master branch.

  • Pull Requests - A pull request is a mechanism for starting a discussion on a set of changes. It's mostly used in collaboration scenarios, where one contributor asks the repository owner to review and merge their changes into the main branch.

Git Workflow

Local Workflow

In a local workflow, you work directly on your local machine, creating branches, making changes, and committing them. This is a simple and effective way to use Git for personal projects or small-scale collaboration.

Centralized Workflow

The centralized workflow involves a central repository. Developers clone the repository, make changes locally, and push them back to the central repository. It's a common workflow for small teams.

Forking Workflow

The forking workflow is often used in open-source projects. Developers fork the main repository, create a clone on their GitHub account, make changes, and then create a pull request to propose changes to the original repository.

Feature Branch Workflow

In the feature branch workflow, each new feature or task is developed in a dedicated branch. This keeps the main branch clean and allows for focused development. Once the feature is ready, it's merged back into the main branch.

Gitflow Workflow

The Gitflow workflow is a branching model that defines a strict branching structure. It's designed to handle larger projects, enabling parallel development and smooth release management.

NOTE : In essence, "main" and "master" serve the same purpose—they represent the default branch and are typically used as the main development branch where the most up-to-date code resides. The shift from "master" to "main" is part of a broader movement to promote inclusive and diverse language within the tech industry

Resolving a merge conflict

Resolving merge conflicts is an essential skill when working with Git, especially in collaborative projects where multiple contributors are making changes to the same files. Merge conflicts occur when Git is unable to automatically merge changes from different branches.

Let's walk through a practical example of resolving a merge conflict. Imagine we have a repository with a file example.txt, and two contributors made changes to this file on different branches. We'll simulate a merge conflict scenario:

  1. Initialize the Repository: First, let's initialize a Git repository and create an initial commit with a file.
$ mkdir git_merge_conflict_example
$ cd git_merge_conflict_example
$ git init
$ echo "Initial content" > example.txt
$ git add example.txt
$ git commit -m "Initial commit"
  1. Create Branches and Make Changes: Next, let's create two branches, make conflicting changes on each branch, and then attempt to merge them.
$ git branch branch1
$ git checkout branch1
$ echo "Change from branch1" >> example.txt
$ git add example.txt
$ git commit -m "Change from branch1"

$ git checkout main
$ git branch branch2
$ git checkout branch2
$ echo "Change from branch2" >> example.txt
$ git add example.txt
$ git commit -m "Change from branch2"
  1. Attempt to Merge: Now, let's try merging the changes from both branches.
$ git checkout main
$ git merge branch1

At this point, Git will detect a conflict because both branches modified the same line in example.txt.

  1. Resolve the Conflict: Open example.txt in your text editor. You'll see something like this:
<<<<<<< HEAD
Initial content
Change from branch1
=======
Initial content
Change from branch2
>>>>>>> branch2

Choose which changes to keep or combine them. For this example, let's keep both changes:

Initial content
Change from branch1
Change from branch2
  1. Mark as Resolved and Complete the Merge: Save the file, then add and commit the changes to complete the merge.
$ git add example.txt
$ git merge --continue

If using an older Git version:

$ git add example.txt
$ git commit -m "Merged branch1 into main"

Now we've successfully resolved a merge conflict! This process involves manually merging changes and ensuring that the resulting code integrates both contributors' modifications effectively.

GitHub

Github is a platform and cloud-based service that hosts the majority of code used in software development mostly using Git.

Here are some handy commands when using Git:


$ git clone url_of_your_remote_repository # Clone a repository from a remote repository

$ git add file1 file2    # will add those two files to the index if they were modified

$ git commit -m "Meaningful commit message"   # will commit those two files (locally)

$ git add .   # will add all of the modified files to the index at once  

$ git commit -m "Other meaningful commit message"   # will commit all of those files together

$ git push origin master   # send all commit to the remote server

Now, when working on a branch:

$ git branch my_feature   # Creating the branch  

$ git checkout my_feature   # Changing the codebase so that we're on that branch now  

$ git checkout -b my_feature   # This does the two previous operations in one ;)  
$ git add file1 file2 

$ git commit -m "Meaningful commit message"   # We didn't just commit this on the master branch like last time, but on the my_feature one  
$ git add .  

$ git commit -m "Other meaningful commit message"  

$ git push origin my_feature   # Notice: we're not pushing master anymore, you just create a new remote branch

Next time you want to work on that branch, you should probably do this first:

$ git checkout my_feature   # Just making sure you're currently on the right branch! 

$ git pull origin my_feature   # Pulling what your coworkers have done so far.

And when you’re done and want to merge it to master/main:

$ git checkout master  
$ git merge my_feature

If you have a non-Git directory in your computer, and you want to turn it into a Git repository, it’s this easy:

$ git init   # You're done!  
$ git remote add origin url_of_your_git_server   # So that you can push your code somewhere.

To find out which files have been modified in a Git repository:

$ git status

Git provides many more abilities, such as rewriting pieces of the history of the project if you feel the commits were not meaningful enough, displaying the history in visually meaningful ways,

For instance, you should run this right now, and see how a complex history can be viewed really nicely:

$ git clone [https://github.com/example/repo.git](https://github.com/example/repo.git)   # You will need a GitHub account for this to work  

$ cd example   # changing your directory into the one you just downloaded  

$ git log --graph --pretty=tformat:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%an %cr)%Creset' --abbrev-commit --date=relative

Additional command:

$ git diff # Shows the changes between the working directory and the staging area.

Difference between Git and GitHub

While Git is a tool that's used to manage multiple versions of source code edits that are then transferred to files in a Git repository. GitHub serves as a location for uploading copies of a Git repository.

Conclusion

Mastering Git and GitHub empowers developers to work together seamlessly, manage project versions effectively, and resolve conflicts efficiently. Continuous practice and exposure to real-world scenarios will enhance your proficiency in using Git and GitHub for successful software development.