Basic Git
Basic Git
By :
Name : Divya Thakur
UID : 28300
Basic Git
• Introduction to Git
• Git lifecycle
• Common Git command
• Git Workflow
• Working with Remote Repository
• Version controlling using Git
GitHub
• GitHub is a cloud-based platform where you can store, share, and work together
with others to write code.
• Storing your code in a "repository" on GitHub allows you to:
• Git is the most popular distributed version control system that intelligently tracks changes in files.
• Git is commonly used for both open source and commercial software development, with
significant benefits for individuals, teams and businesses.
• Git is particularly useful when you and a group of people are all making changes to the same files
at the same time.
• Git lets developers see the entire timeline of their changes, decisions, and progression of any
project in one place. From the moment they access the history of a project, the developer has all
the context they need to understand it and start contributing.\
• Developers work in every time zone. With a DVCS like Git, collaboration can happen any time
while maintaining source code integrity. Using branches, developers can safely propose changes
to production code.
• Businesses using Git can break down communication barriers between teams and keep them
focused on doing their best work. Plus, Git makes it possible to align experts across a business to
collaborate on major projects.
• Typically, to do Collaborative working in a Git-based workflow, you
would:
• Create a branch off from the main copy of files that you (and your collaborators) are working on.
• Make edits to the files independently and safely on your own personal branch.
• Let Git intelligently merge your specific changes back into the main copy of files, so that your
changes don't impact other people's updates.
• Let Git keep track of your and other people's changes, so you all stay working on the most up-to-
date version of the project.
How do Git and GitHub work together?
• When you upload files to GitHub, you'll store them in a "Git repository." This means that when you make
changes (or "commits") to your files in GitHub, Git will automatically start to track and manage your changes.
• There are plenty of Git-related actions that you can complete on GitHub directly in your browser, such as
creating a Git repository, creating branches, and uploading and editing files.
• However, most people work on their files locally (on their own computer), then continually sync these local
changes—and all the related Git data—with the central "remote" repository on GitHub. There are plenty of
tools that you can use to do this, such as GitHub Desktop.
• Once you start to collaborate with others and all need to work on the same repository at the same time,
you’ll continually:
• Pull all the latest changes made by your collaborators from the remote repository on GitHub.
• Push back your own changes to the same remote repository on GitHub.
• Git figures out how to intelligently merge this flow of changes, and GitHub helps you manage the flow
through features such as "pull requests."
Signing up for a new personal account
• Navigate to https://github.com/.
• Click Sign up.
• Follow the prompts to create your personal account.
• During sign up, you'll be asked to verify your email address. Without a
verified email address, you won't be able to complete some basic
GitHub tasks, such as creating a repository.
• Now that you've created your personal account, we'll start to explore
the basics of GitHub
Exercise : GitHub Essentials
• Lets perform basic GitHub essentials like repositories, branches,
commits, and pull requests.
• Step 1: Create and use a repository
• Step 2: Create and manage a branch
• Step 3: Make changes to a file and push them to GitHub as commits (commit
changes)
• Step 4: Open a pull request
• Step 5: Merge your pull request
About repositories
• A repository is the most basic element of GitHub.
• It's a place where you can store your code, your files, and each file's revision history.
• Repositories can have multiple collaborators and can be either public or private.
• Repository terminology:
• Branch : A parallel version of your code that is contained within the repository, but does not
affect the primary or main branch.
• Clone : To download a full copy of a repository's data from GitHub.com, including all versions of
every file and folder.
• Fork: A new repository that shares code and visibility settings with the original "upstream"
repository.
• Merge: To take the changes from one branch and apply them to another.
• Pull request: A request to merge changes from one branch into another.
• Remote:A repository stored on GitHub, not on your computer.
• Upstream: The branch on an original repository that has been forked or cloned. The corresponding
branch on the cloned or forked branch is called the "downstream."
Step 1: Create and use a repository
• You can think of a repository as a folder that contains related items, such as files, images, videos,
or even other folders.
• A repository usually groups together items that belong to the same "project" or thing you're
working on.
• Often, repositories include a README file, a file with information about your project.
• README files are written in Markdown, which is an easy-to-read, easy-to-write language for
formatting plain text.
• GitHub lets you add a README file at the same time you create your new repository.
• GitHub also offers other common options such as a license file, but you do not have to select any
of them now.
Steps to create a repository
• In the upper-right corner of any page, select , then click New
repository.
• In the "Repository name" box, type “any name “.
• In the "Description" box, type a short description. For example, type
"This repository is for practicing the GitHub Flow."
• Select whether your repository will be Public or Private.
• Select Add a README file.
• Click Create repository.
Step 2: Create a branch
• Branching lets you have different versions of a repository at one time.
• By default, your repository has one branch named main that is considered to be
the definitive branch.
• You can create additional branches off of main in your repository.
• Branching is helpful when you want to add new features to a project without
changing the main source of code.
• The work done on different branches will not show up on the main branch until
you merge it, which we will cover later in this guide.
• You can use branches to experiment and make edits before committing them to
main.
• When you create a branch off the main branch, you're making a copy, or snapshot,
of main as it was at that point in time. If someone else made changes to the main
branch while you were working on your branch, you could pull in those updates.
• This diagram shows:
1. The main branch
2. A new branch called feature
3. The journey that feature takes before it's merged
into main
Creating a branch
• When you created a new branch in the previous step, GitHub brought you to the
code page for your new branch, which is a copy of main.
• You can make and save changes to the files in your repository.
• On GitHub, saved changes are called commits.
• Each commit has an associated commit message, which is a description
explaining why a particular change was made.
• Commit messages capture the history of your changes so that other contributors
can understand what you’ve done and why.
Under the new branch you created, click the README.md file.
• To edit the file, click .
• In the editor, write a bit about yourself.
• Click Commit changes.
• In the "Commit changes" box, write a commit message that describes
your changes.
• Click Commit changes.
• These changes will be made only to the README file on your new
branch, so now this branch contains content that's different from
main.
Step 4: Open a pull request
• Now that you have changes in a branch off of main, you can open a pull
request.
• Pull requests are the heart of collaboration on GitHub.
• When you open a pull request, you're proposing your changes and requesting
that someone review and pull in your contribution and merge them into their
branch.
• Pull requests show diffs, or differences, of the content from both branches. The
changes, additions, and subtractions are shown in different colors.
• As soon as you make a commit, you can open a pull request and start a
discussion, even before the code is finished.
• In this step, you'll open a pull request in your own repository and then merge it
yourself. It's a great way to practise the GitHub flow before working on larger
projects.
• Click the Pull requests tab of your repository.
• Click New pull request.
• In the Example Comparisons box, select the branch you made, new, to
compare with main (the original).
• Look over your changes in the diffs on the Compare page, make sure
they're what you want to submit.
• Click Create pull request.
• Give your pull request a title and write a brief description of your
changes. You can include emojis and drag and drop images and gifs.
• Click Create pull request.
Step 5: Merge your pull request
• In this final step, you will merge your new branch into the main
branch.
• After you merge your pull request, the changes on your new branch
will be incorporated into main.
• Sometimes, a pull request may introduce changes to code that
conflict with the existing code on main.
• If there are any conflicts, GitHub will alert you about the conflicting
code and prevent merging until the conflicts are resolved.
• You can make a commit that resolves the conflicts or use comments
in the pull request to discuss the conflicts with your team members.
• Merge your branch into the main branch.
• At the bottom of the pull request, click Merge pull request to merge
the changes into main.
• Click Confirm merge. You will receive a message that the request was
successfully merged and the request was closed.
• Click Delete branch. Now that your pull request is merged and your
changes are on main, you can safely delete the new branch.
• If you want to make more changes to your project, you can always
create a new branch and repeat this process.
• Click back to the Code tab of your repository to see your published
changes on main.
Other Platforms
• GitHub is an open source Git hosting platform that is available as a
cloud service and as a self-hosted installation.
• Similarly , we have other popular platforms as Bitbucket and gitlab ,
which are also open source Git hosting platform.
• But the most popular platform is Github with many good features
than others .
Difference between GIT and GitHub
• Git: It is a Distributed Version Control System for tracking versions of files.
• Github: It is web portal and cloud hosting service for your Git repositories.
• Git uses commands to track versions of your files.
• Github is just a remote platform where these files are hosted.
• Git allows you to track versions of your code in your local machine.
• Github :if you want a remote backup of your code or want to publish your code to
a community then you've to push it to Github.
• It's not mandatory to use Github.
• If you're the sole person working on the project and don't need to publish your
code to the world then you can simply avoid using Github.
• Just track the versions of your code in local repository of your machine using Git.
• By this point, it'd be clear that committing the code using Git in your local
machine doesn't automatically upload it to Github too.
• There are two distinct terms for these activities - Committing and Pushing.
• Committing is capturing the changes from your working copy to your local
repository. On the other hand, Pushing is uploading the captured changes from
your local repository to Github.
The Git Lifecycle
• The lifecycle of Git consists of four main stages:
• Create
• Modify
• Stage
• Commit
Create
Git remove
• git rm:
• The git rm command removes tracked changes from the Staging Area.
• It tells Git that the updates which were pushed to staging earlier with git add command,
are not ready to commit
• git rm --cached <file> : command used to remove file .
• To remove multiple file : git rm --cached <file> <file> .
O/P parameters of git status
• branch master: This says that the working tree is a master branch. no
• commits yet: This gives information on the commits, as of now there are no
commits yet.
• Untracked files: This says that Git sees the files but has not started tracking
changes yet and marked these as red.
• Status messages: This gives relevant instructions for staging/unstaging files.
Git Commit
• Committing the changes in Git:
git commit
• Committing the changes in Git without commit message:
git commit -m “this is my first commit”
Git does not recommend to commit without any message.
Git commit messages are necessary to look back and see the changes made during a particular commit.
If everyone will just commit without any message, no one would ever know what changes a developer has
done.
• Commit Hash: The first part of the commit log is "commit hash" which is the hash value by which Git saves or
refers everything internally.
• Commit Author: This part tells you about who committed the changes in the repository i.e. the author name.
• Commit Date: This part shows you the date and time of the commit with the day and time zone. It is
important to have a day and time zone because many people work on a single project from different
geographical locations.
• Commit Message: This part shows the commit message that was written while committing the changes.
• git log --oneline:
• As you can see some of the details that were previously visible are now hidden
and the view is of only one-liner.
• Commit Id: Every commit has a commit id which is actually a compressed version
of commit hash. Since a commit hash is not a sequential number, compressing it
still shows unique commits.
• Commit Message: commit message to understand what the commit was about.
• git log <commit hash>
• see the log details using the commit hash that we have got from git
log --oneline command .
• Output:
The first line shows the file names that have been considered as the input in git diff. You can see that
they have been marked by a and b along with the two different file state that has been taken as input.
This shows the metadata related to the command and the execution of it on the files.
This line defines the symbol, called a legend, to tell you what is used to describe the first file and what is
used to describe the second file. As you can see, - is used in front of the first file, and + is used in front of
the second file. So whenever diff shows you the changes related to the first file, they will be marked by -
and the changes in the second file will be marked by the symbol +.
The fourth line shows you symbol @@ and symbols ahead of it. They are called chunks. Chunks in git diff
define the change' summary. In our image the following chunk can be seen @@ -1,2 +1 @@
This means that lines one and two were changed in the first file and line one was changed in the second
file. Remember the - and + symbol used in the third point as a symbol to the first and second file
respectively.
Git Show command
• Git show is similar to git log ,the only difference is that with logs it
shows :
• Difference between the versions of the file to which HEAD is pointing (diff)
• Output:
The above command follows the generic syntax of git remote add <name> <repository url>
Add: To add a new URL to the repository.
Name: To give a name that you will use instead of the URL of the repository.
URL: The URL of the repository.
• To check whether we linked our repository or not, execute the git remote command.It will show
you that the origin repository (alias for the GitHub myFirstRepo repository) is available.
• git remote -v command : show you the url of origin repo .
• git remote rm <name> : to remove a connection , in the above scenario name is origin.
• git remote rename <old-name> <new-name> : change name of old repo .
• git fetch : The git fetch command downloads commits, files, and refs
from a remote repository into your local repo.
• Scenario :
1. git remote add new https://github.com/Divyathakur273/devops.git
2. git remote -v
3. git branch -r :to see branches
4. git fetch new : fetch the refs from new .
git fetch new <branch name>
git fetch --all
5. git branch -r
6. git log new/main: check the logs of all branches.
git fetch has similar behavior to git pull, however, git fetch can be considered a safer,
nondestructive version.
Git fork
• A fork is a copy of a repository. Forking a repository allows to freely experiment with changes
without affecting the original project.
• When a user forks a repository, all the files in the repository are automatically copied to the user's
account on GitHub and it feels like the user's own repository.
• This process is similar to copying a folder from one drive to another drive on a computer.
• Through git forking, the users can develop their own modifications to the code that belongs to
someone else.
• To be noted, that this process does not have any effect on the original repository (also called an
upstream repository) code.
• Forking a repository on GitHub is done for two main purposes:
• Improving someone's code/software
• Reusing the code in a project
• Forking is allowed for public repositories without permission. But if the repository is private, one
can only be able to fork if he or she has permission from the owner of the repository.
Cloning a Remote Repository from GitHub
• Cloning is a process of creating an identical copy of a Git Remote
Repository to the local machine.
• A repository that is uploaded online for collaboration is called an
Upstream Repository or a Central Repository.
• A central repository indicates that all the changes from all the
contributors pushed into this repository only. So, this is the most
updated repository instance of itself. Sometimes this is often called
the original repository.
• git clone <repoURL>
• It is a standard GIT command to clone an existing remote
repository.
Difference btw git clone and git fork
git push
• A git push command, when executed, pushes the changes that the user has made
on the local machine to the remote repository.
• To be able to push to your remote repository, you must ascertain that all your
changes to the local repository are committed.
• Consider Git push as a part of the synchronization process in Git.
• The synchronization happens between the local and remote repository where the
source and the receiver may vary.
• It uploads the changes done on the local repository to keep the remote
repository up to date.
• command : git push
• command :
• git push <remote_repo> <branch_name>
Blob
• Git represents your file’s contents in blobs, which are also leaf nodes
in something close to a directory, called a tree.
• A Git blob (binary large object) is the object type used to store the
contents of each file in a repository.
• The file's SHA-1 hash is computed and stored in the blob object.
• These endpoints allow you to read and write blob objects to your Git
database on GitHub.
• Blobs leverage these custom media types.
• git cat-file -p 21a40280c2ce1d4cdd0e6590f154479da9837495
• Here the command is git cat-file -p hashvalue
• 21 is the folder inside which the hash is there
• and a40280c2ce1d4cdd0e6590f154479da9837495 is the actual hash
• to check the data of the file through hash
Step 1:
Step 2: Adding file to the staging area
Step 2:
Step 3 : from staging to commit
• Now , in step 2 , before commit , we have got one file in objects i.e, 58
, which contains hash value of the data inside the file f1.txt , which we
created .
• We have only added the file to the staging area and not commited yet
.
• In step 3 , we will add the file to the commit and then in the object
folder , 2 new files will appear in which one file will contain hash value
of commit and other will contain tree.
Step 3 : commit of file f1.txt
After committing , new files created are 25,d8
Mktree and read tree
• Mktree : used to create a tree
git cat-file | git mktree
• Read tree: To read the contents of the tree.
git read-tree <hashvalue>