Published on

Learn Git from Beginner to Advanced 2024

Learn Git from Beginner to Advanced 2024

Learn Git from Beginner to Advanced 2024

Git Explained:

What is git?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. The objective of using git is to work collaboratively and efficiently on any project and track your work without any hassels. Verson control helps manage change code and it's essential when working in teams.

What is difference between Git and GitHub?

Many people assume git and github as same thing but these two are entirely different things. 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 or it stores Git repository.

Git Terminology

There are few terms, you should aware of to understand git

  1. Repositories
  2. Branches
  3. Commits
  4. Pull/Merge Request

What is Repository?

A place or container in which something is stored in large quantities called Repository. Basically a repository represents the entire project. All of the project code along with any changes are stored in a repository. You can clone a repository in your system using git and can make changes to it and push or pull the code.

What is branches in Git?

Git allows to create a copy of a code and stored it, this copy known as a branch with unique name. You are allowed to create multiple branches. Git only stores changes we make from the time we create a new branch. It remembers the base, (the point where the branch was forked) and saves the minimal amount of information required to represent that change. The ability to store only the differences from the previous version is central to Git.

What is Commits?

After creating a branch when you want to add something to code, this process known as commit.Each commit has an associated message. Selecting files to be added to a commit is called staging.

Pull Request vs. Merge Request: What's the Difference?

When you want to add your chenages from one branch to another branch, you have to create a some sort of process to see changes and evaluating them, this process known as pull request or merge request.

GitHub and Bitbucket choose the name “pull request” because the first manual action is to pull the feature branch. Tools such as GitLab and others choose the name “merge request” because the final action is to merge the feature branch.

Tools to Manage Git

There are lot of tools available to manage git better in your system, few mwntioned below-

  1. Sourcetree
  2. GitHub Desktop
  3. Fork
  4. Tower

How to Install Git?

There are different way you can follow to install git, you can find more guide here

How to Configure Git?

After installing git on a computer, you have to connect a GitHub repository from Git, you will need to authenticate with GitHub using either HTTPS or SSH.

1. Connecting over HTTPS

now you have to configure your Github account's username and email address.This information will be used to document who made changes to files in git. It is important to use the same email address and username that you setup on GitHub.com.

here is how you can do that - use the --global to make this work for all the repo in your computer.

$ git config --global user.name "username"
$ git config --global user.email "email@email.com"

Once configuration is done you can verify with below commands

git config user.name
git config user.email

2. Connecting over SSH

This is the way you can connect your github account without password using ssh public key. Look to see if you have files ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. If not, create such public/private keys: Open a terminal/shell and type:

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

Copy your public key (the contents of the newly-created id_rsa.pub file) into your clipboard. On a Mac, in the terminal/shell, type:

$ pbcopy < ~/.ssh/id_rsa.pub

Paste your ssh public key into your github account settings.

Go to your github Account Settings Click “SSH Keys” on the left. Click “Add SSH Key” on the right. Add a label (like “My local Key") and paste the public key into the big text box. In a terminal/shell, type the following to test it:

$ ssh -T git@github.com

If it says something like the following, it worked:

Hi username! You've successfully authenticated, but Github does
not provide shell access.

How to Clone a Repository?

To clone a remote repository in your computer go to the terminal and navigate to a directory, then type:

$ git clone https://github.com/username/reponame.username.git

This will create a new directory for the project and set-up the remote origin to track the changes. This means when you push git, it knows where to send things.

Initialise and Add a remote repository

If you have existing work and you want to put it in a repository, then you have to intialize a local repository, you can do this with below commands

$ git init

Now you have to create a new remote repository on Github and then you can add that as remote origin for your local branch

$ git remote add origin https://github.com/username/reponame.username.git

Important Git Commands

Till now we are able to understand basic terminology of git and able to create a local repository. Next we will going to learn how to create branch, commit, merge, pull, push etc.

  • git clone: Clones an existing Git repository from a remote source, such as a website or server, and creates a local copy on your computer. This is typically used to create a local copy of a repository that you want to work with or contribute to.
  • git add: Adds files to the staging area, which is a holding area where Git tracks changes to files. This command is used to tell Git which files you want to include in the next commit, which is a snapshot of the current state of the repository.
  • git commit: Commits changes to the repository. This creates a new commit with a message that describes the changes that were made, and updates the history of the repository with the new commit.
  • git push: Pushes local commits to a remote repository. This sends the committed changes to the remote repository, where they can be shared with others and merged into the main branch of the repository.
  • git pull: Pulls changes from a remote repository and merges them into the local repository. This is used to get the latest version of the code from the remote repository and incorporate any changes that have been made by others.
  • git branch: Creates or lists branches in a repository. Branches allow developers to work on different versions of the code simultaneously, without affecting the main branch of the repository. This command can be used to create a new branch, switch to an existing branch, or list all of the branches in the repository.
  • git merge: Merges changes from one branch into another branch. This is used to combine the changes from multiple branches into a single branch, typically the main branch of the repository.
  • git diff: Shows the differences between two versions of a file or two branches in a repository. This command can be used to see what changes have been made to a file or a branch, and to compare different versions of the code.
  • git log: Shows the commit history of a repository. This command can be used to view the details of previous commits, such as the commit message, author, and timestamp.
  • git status: Shows the current status of the repository, including which files have been modified, added, or deleted, and which branch you are currently working on.
  • git tag: Adds tags to specific commits in the repository. Tags are used to mark important points in the history of the code, such as release versions, and can be used to refer to specific versions of the code.
  • git stash: Temporarily saves changes that have been made to the working directory, but have not yet been staged or committed. This can be useful when you need to switch to a different branch or work on a different task, but don't want to commit your changes or lose them.
  • git reset: Resets the state of the repository to a previous commit. This command can be used to undo changes that have been made to the code, either by discarding uncommitted changes or by moving the branch pointer to a different commit.