Git Basics, SSH, Git Hooks, CI/CD - Part: 2

Shrey Banugaria

In our previous blog post, “Git — Little More than Basic Part 1,” we delved into the essentials of Git, covering installation, initial commands, essential Git commands, stashing, rebasing, and more. While these fundamentals are crucial for individual and small team projects, working with larger teams or contributing to open-source repositories requires a deeper understanding of Git functionalities. One of these crucial features is Git forking.

What is Git Forking?

Imagine you’ve stumbled upon an amazing open-source project in active development, and you’re eager to contribute. Instead of directly cloning the repository, you opt for Git forking. Git forking follows the Gitflow workflow, a branching model that facilitates collaboration. When you fork a repository, you essentially create a copy of it on the server, establishing your public repository. While you can pull changes from the main repository, you can’t directly push to it. After committing your changes to your private repository, you’re ready to submit a pull request to the main repository. This pull request serves as a valuable discussion on GitHub, where you seek solutions. The project maintainer or community reviews your request and, if approved, integrates your contribution.

Setting Up SSH for GitHub Authentication

During your software development journey, you’ll likely find yourself needing two GitHub accounts. Whether you aim to contribute to open-source projects or collaborate within a team, you’ll need a public Git SSH key to authenticate your identity. This process is straightforward and can be accomplished through the command-line interface (CLI). First, open your CLI and enter the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This command generates a new SSH key pair with RSA encryption and a length of 4096 bits, linked to your email address. You may be prompted to choose a file location and passphrase, but you can leave these fields blank to accept the default settings.

Once you’ve generated your SSH key, add the public key to your GitHub account. To do this, access your GitHub account settings, navigate to “SSH and GPG keys,” and select “New SSH key.” Assign a title to your key, describing the machine you’re using (e.g., “Macbook Pro”). Copy and paste the contents of the public key file (usually located at ~/.ssh/id_rsa.pub) into the “Key” field. Finally, click “Add SSH key” to save your changes. You should now be able to authenticate your identity when pushing or pulling changes to GitHub repositories.

Git Hooks

Git Hooks are a valuable feature of Git that allows you to trigger custom scripts or actions automatically when specific events occur within your Git workflow. In simpler terms, Git Hooks are scripts that Git runs before or after certain events, such as committing, pushing, or merging. These hooks can help automate repetitive tasks, maintain code quality, and enforce coding standards.

create a pre-commit file in the .git/hooks directory and ensure it is executable using the

chmod +x .git/hooks/pre-commit` command will make the executable

The example provided is a pre-commit hook, but similar hooks, such as post-commit, commit messages, and pre-release, can be employed as well. You can explore more details and examples in the Git documentation. Check this doc for more details and examples.

No Fast Forward Error

Have you ever encountered the error message “Fatal: Not possible to fast-forward, aborting”? Let’s uncover why this error occurs.

Imagine you have two branches, ‘target’ and ‘source,’ with ‘source’ checked out from ‘target.’ After making changes to the ‘target’ branch, you attempt to merge ‘source’ into it. If someone else is also using the ‘source’ branch, you should first pull changes from the ‘source’ branch before creating a pull request. This is where the “Fatal: Not possible to fast-forward, aborting” error can occur.

This error arises because the ‘target’ branch (the branch you are merging into) contains new commits that are absent in the ‘source’ branch (the branch you are merging or rebasing). In such cases, Git prevents a fast-forward merge because it would not accurately represent the history of the branches. Instead, Git requires you to create a merge commit to capture the fact that the branches have diverged and are being merged back together.

Here you can:

git checkout target-branch
git merge - no-ff source-branch

OR

git pull — no-ff source-branch

In both cases, you will need to resolve merge conflicts before proceeding.

OR you can rebase the target branch by:
git checkout source-branch

git rebase target-branch

Check the git workflow before you use the above commands in the production GIt WorkFlow

Managing External Dependencies with Ease: Git Submodules

Repos depend on external code. These integrations of external code are constituted in different ways. One can copy the code but you will have to do it every time the code is updated, You can download a package like NPM, the downside for this is you will have to update the version every time they stop the support for the version you are using. To solve this problem of having an external code where you can constantly track specific commits on the external repo you can use it as your git submodule.

Git Submodule only tracks specific commits. You can use the git submodule in your repo by this command:

git submodule add https://github.com/yourrepourl

Initialize it after adding

git submodule init
git submodule update

When you make changes to a submodule you commit the changes the same way you do for any repo. Navigating to its folder git add . and commit

Check the utility, requirements and drawbacks of submodules and then implement it. To check more about the Git Submodules refer to this doc: Git Submodules

Apart from these, there are several topics one should know to move towards advanced use of git. Some of the terms/commands we all should be familiar with

git bisect: Binary searching through commit history. Used specifically to check when the bug was introduced or who changed the name of a global variable

Git worktrees: Working with multiple branches of the same repository without cloning it multiple times

Reflog: `git reflog` to access history of all reference changes in the repository

Git LFS: Git Large File System is an extension developed by Open Source contributors which downloads only the required versions of the large files in the repository. When you clone a repo every version of a file is downloaded and this can be heavy for the larger files which are changed regularly.

Last but not the least

CI/CD: Continues Integration, Delivery and Deployment. After choosing a platform to host your project, choose a tool like Jenkins or Github actions. Set a configuration pipeline file which should be auto-triggered whenever there is a commit to a particular repo or repos.

There is a lot to learn in Git and it is one of the most essential skills required in the development industry. As one explores a journey in software development along with being familiar with usual git commands, one should know the common git errors and what causes them, what the command does under the hood and better alternatives. Happy Version Control everyone.

Let us get connected:

LinkedIn: Shrey Banugaria
Twitter: BanugariyaShrey

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shrey Banugaria
Shrey Banugaria

Written by Shrey Banugaria

Backend Developer, GraphQL Certified, Node.js, JavaScript (ES6 +), PostgreSQL, MongoDB, MySQL and more

No responses yet

Write a response