The Complete Tutorial of Git&Github for Beginners

The Complete Tutorial of Git&Github for Beginners

Table of Contents

How do you track changes and preserve coordination among multiple programmers? Keeping every team member on the correct version of the code is crucial, and therefore this is the part where version control systems start to play a significant role in our life.

In this tutorial, I will walk you through Git&Github with the most-used commands and hopefully will clarify some points for new programmers. 

Most Popular Version Control Systems

  • GitHub
  • GitLab
  • Bitbucket
  • Beanstalk
  • PerForce
  • Apache Subversion
  • AWS CodeCommit
  • Microsoft Team Foundation Server
  • Mercurial

What is Github?

GitHub is mainly a code hosting platform for version control and efficient collaboration between programmers owned by Microsoft Corporation.

Git vs. GitHub? Are They the Same?

The short answer is no! Git and GitHub are not the same. While Git is a version control system tool that enables you to manage your source code primarily for individual use, Github is a cloud-based service that hosts the git repositories and enables team collaboration. Git is free and open-source. Github is not. 

What is the Difference Between GitLab and GitHub? Are They the Same?

Same answer here! GitLab and GitHub are not the same. While both GitLab and GitHub are web-based Git repositories, they have specific features that need to be considered when choosing one over another. You can find a mature comparison between GitLab and GitHub هنا

Create a New Repository on github.com

Login to your Github account and navigate to https://github.com/new to create a repository.

Initialize a Local Directory as a Git Repository.

You can initialize a local directory as a Git repository or create a project from scratch and then initiate Git. Navigate to the folder you want to make a git repository and run followed commands;

				
					$ git init

Initialized empty Git repository in <PATH_TO_LOCAL_REPO>/git-blog/.git/

#Add files to Git to be committed later than
$ git add .

# Commit files
$ git commit -m "first commit"

# Set up local git branch name
git branch -M main
				
			

Connect Local Repository with GitHub Remote Repository

At this very point;

  • You have a local git repository that keeps your committed changes
  • You have an empty remote Git repository on Github.com

All you need is to create a remote connection called “origin” to point your local repository to the remote repository.  Use the link of your remote repository to connect with local.

				
					$ git remote add origin https://github.com/<YOUR_GITHUB_USERNAME>/git-blog.git
				
			

How to Push a Local Branch to Remote

I have set up the origin for the time being, and therefore I’m ready to push to a remote branch named “main”.

				
					$ git push -u origin main

Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1.42 KiB | 1.42 MiB/s, done.
Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/<YOUR_GITHUB_USERNAME>/git-blog.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
				
			

How to Clone a Git repository

You can get a copy of an existing Git repository with “git clone” to your local. This will enable you to contribute to the project, fix conflicts, or add new features.

				
					git clone https://github.com/exceptionly/blog-spring-boot-docker.git
				
			

How to Create a new branch

The followed code allows you to create a new branch from the top of the main branch. 

				
					$ git checkout -b my-feature-branch main
				
			

Assuming you are in the main branch already or another branch you want to fork;

				
					$ git checkout -b my-feature-branch
				
			

You can achieve this by creating a branch and then checkout it also.

				
					$ git branch my-feature-branch
$ git checkout my-feature-branch
# Switched to branch 'my-feature-branch'

				
			

Git Merge

Git merge is a way to combine two branches that have been forked before. For example, assume that you have the main branch, and you created another branch to fix a bug or implement a new feature. Keep in mind that when you apply changes with git merge; git will always generate an additional commit for merge. The followed snippet is how you merge a feature branch into the main branch.

				
					# Create a new feature branch
$ git checkout -b my-feature-branch main
# Edit some files
$ git add <file>
$ git commit -m "Initial commit for my feature"
#Switch to main branch to get changes here
$ git checkout main
# Merge in the my-feature-branch branch
$ git merge my-feature-branch
				
			
https://www.atlassian.com/git/tutorials/using-branches/git-merge

Git Rebase

Git rebase also aims to take the commits from one branch into another as git merge.

Alternatively, rebase has history rewriting features. For a detailed explanation look at Merge vs. Rebase, visit Atlassian’s Merging vs. Rebasing guide.

				
					# Create a new feature branch
$ git checkout -b my-feature-branch main
# Edit some files
$ git add <file>
$ git commit -m "Initial commit for my feature"
#Switch to main branch to get changes here
$ git checkout main
# Rebase my-feature-branch branch to main
$ git rebase my-feature-branch
				
			

If you are lucky enough, you will not have conflicts during rebase as it takes ages to fix the conflicts if there are several commits. 

# Fixing conflicts during merge or rebase will not be handled in this tutorial

https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

How to Delete Local Branch in Git?

				
					$ git branch -d <branchName> 
$ git branch -D <branchName>
				
			

How to Delete Remote Branch in Git?

				
					git push origin --delete <branchName>
				
			

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

arArabic