A world of Git and GitHub

A world of Git and GitHub

Git

Git is an open source distributed version control system.

A version control system, or VCS, tracks the history of changes as people and teams collaborate on projects together. As developers make changes to the project, any earlier version of the project can be recovered at any time.

VCS helps you maintain the state of your file and easily switch between them(just like backup with steroids ) It helps to find out:

  • Which changes were made?
  • Who made the changes?
  • When were the changes made?

In a distributed version control system, every developer has a full copy of the project and project history.

INFO:Centralized VCS keep the history of changes on a central server from which everyone requests the latest version of the work and pushes the latest changes to. On the other hand, on a distributed VCS, everyone has a local copy of the entire work’s history.

Install

Git for All Platforms

http://git-scm.com

Git Repository

A repository, or Git project, encompasses the entire collection of files and folders associated with a project, along with each file's revision history. A git repository contains, among other things, the following:

1) A set of commit objects (files history as snapshots in time) .

Commit Objects contains three things:

  • A set of files, reflecting the state of a project at a given point in time.
  • References to parent commit objects.
  • A 40-character string that uniquely identifies the commit object.

2) A set of references to commit objects, called heads.

head is a reference to a commit object. Each head has a name. By default there is a head in every
repository called "master". A repository can contain any number of heads. At any given time, one head
is selected as the “current head.” This head is aliased to HEAD, always in capitals.

GitHub

GitHub is a Git repository hosting service. It takes that change history made by you so far in your code and hosts it online so that you can access it from any computer. You do this via pushing changes from your local machine (i.e.: the computer you’re currently using) up to Github, and then, from the new/different computer pulling those changes down.

GitHub and the command line

Configure user information for all local repositories

git config --global user.name "[name]"

Sets the name you want attached to your commit transactions

git config --global user.email "[email address]"

Sets the email you want attached to your commit transactions

Create repositories

When starting out with a new repository, you only need to do it once; either locally, then push to GitHub, or by cloning an existing repository.

git init

Turn an existing directory into a git repository

git clone "[url]"

Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits

Some basic commands

git add

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. It is basically stage and take a snapshot of the changes to include them in the project's history.

git status

It shows the status of changes as untracked, modified, or staged.

NOTE:It is generally a good practice to check the state of changes before committing.

git commit -m "<descriptive-message>"

This command saves the snapshot to the project history and completes the change-tracking process. In short, a commit functions like taking a photo. Anything that's been staged with git add will become a part of the snapshot with git commit.

Working with remote repository

git push <remote> <origin>

Uploads all local branch commits to GitHub. The git push command is used to upload local repository content to a remote repository.

git fetch <remote>

Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository.Use git fetch to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

NOTE:git fetch <remote> <branch> fetches the specified branch from that remote.

 git merge

Combines remote tracking branch into current local branch

git pull <remote>

Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy. This is the same as git fetch <remote> followed by git merge origin/<current-branch>.

NOTE: git pull is a combination of git fetchand git merge

Working with branches

Branches are an important part of working with Git. Any commits you make will be made on the branch you're currently “checked out” to.

Check your current working branch using the command git branch

git branch [branch-name]

Creates a new branch

git checkout [branch-name]

Switches to the specified branch and updates the working directory

git merge [branch]

Combines the specified branch’s history into the current branch. This is usually done in pull requests,but is an important Git operation

git branch -d [branch-name]

Deletes the specified branch

Undo changes in the staging area

git log

Lists version history for the current branch

Screenshot from 2022-10-12 01-34-56.png Each commit has a unique SHA-1 identifying hash

git reset [commit]

Undoes all commits after [commit], preserving changes locally

Git & GitHub workflow

git init
git branch feature
git add <some-files-added>
git commit -m "added the files"
git checkout master
git merge feature

Screenshot from 2022-10-12 01-57-57.png

Did you find this article valuable?

Support Cheithanya Pr by becoming a sponsor. Any amount is appreciated!