Mastering Git and GitHub: A Comprehensive Guide to Version Control and Collaboration

Abdul TalhaAbdul Talha
4 min read

Introduction

Git

  1. Git is a version control system that allows users to collaborate. The version control system tracks all changes in files.

  2. It is used to manage the history of the code.

  3. It is free, open-source software.

GitHub

  1. GitHub is a web-based hosting service for Git repositories. Other names for repositories are directory, folder, or repository.

  2. It allows for the storage and sharing of code with others.

Setup

Step 1: Install Git

Git can be installed using the link below:

https://git-scm.com/downloads

Step 2: Create an Account on GitHub

We can create an account with Google. If you have a student account, you'll get additional benefits in the form of a Student Developer Pack.

https://github.com/

Step 3: Configure Git with GitHub

git config --global user.email 'youremail@gmail.com'
git config --global user.name 'yourname'
git config --list
  1. The above commands are used to configure Git. Replace “youremail@gmail.com” with your email.

  2. In the username section “yourname,” enter your username.

  3. The last command will display the configuration details, including your email and other settings.

Git Terminologies

When we install Git, we get a terminal called Git Bash. Using Git Bash, we can execute commands in Git.

1. Creating a Folder

First, create a folder, then perform operations. A folder can also be created using the mkdir command in Git Bash:

mkdir folder_name

2. Initialization of a Repository

In this step, we initialize the repository using the command below. This step is essential, as skipping it will result in a fatal error:

git init

3. Git Flow

Git flow starts with the initialization of a repository. Then, files are added to the staging area, committed to the repository, and finally pushed to GitHub.

4. Status

Check the status of files to see which are added, which need to be committed, and which need to be pushed to GitHub:

git status

5. Adding Files

Files, whether text files or programming files, can be added using the command below:

git add filename.extension

To add all files in a folder:

git add .

6. Commit Files

After adding files, they move to the staging area. Then, files are committed to create a snapshot of the staged changes:

git commit -m "message"

Replace “message” with a description of the commit.

7. Log

View the history of changes in the repository:

git log

To display the log in a single-line format:

git log --oneline

8. Creating a New Branch

List all branches in the repository:

git branch

Create a new branch:

git branch branch_name

9. Switching Between Branches

Switch to a specific branch:

git switch branch_name

Create and switch to a new branch in a single command:

git switch -c branch_name

Alternatively, use the older checkout command:

git checkout branch_name

10. Merging Two Branches

To merge two branches, switch to the main branch and run the command below with the name of the branch you want to merge:

git merge branch_name

11. Renaming and Deleting Branches

Rename a branch:

git branch -m old_branch_name new_branch_name

Delete a branch:

git branch -d branch_name

12. Diff

Show differences between two commits or branches:

git diff branch_name_1 branch_name_2

Compare specific commits using their hash IDs (retrieved via git log):

git diff commit_hash_1 commit_hash_2

13. Stash

Save changes temporarily using:

git stash

Name the saved changes:

git stash save "Saved file"

View the list of stashed changes:

git stash list

14. Moving Code to GitHub

Before pushing code to GitHub, configure Git and GitHub. Create a repository in GitHub, copy its URL, and run the following commands:

git remote -v
git remote add origin 'https://github.com/username/repository_name.git'
git push origin main

Replace 'https://github.com/username/repository_name.git' with your repository's URL.

Challenges, Solutions, and Resources

Challenge 1: Initial Configuration Issues

  • Problem: Errors during git config due to incorrect email or username formatting.

  • Solution: Carefully follow the syntax and use git config --list it to verify configurations.

  • Resource Used: Git documentation (link).

Challenge 2: Repository Initialization Errors

  • Problem: Fatal errors during git init.

  • Solution: Ensure you're in the correct directory before running the command.

  • Resources Used: Tutorials on Git basics, such as Atlassian Git Tutorials.

Challenge 3: Managing Code History and Logs

  • Problem: Logs are too verbose and hard to read.

  • Solution: Use git log --oneline for a simplified view.

0
Subscribe to my newsletter

Read articles from Abdul Talha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abdul Talha
Abdul Talha