Domain 1: Introduction to Git
In this module, you'll be introduced to version control and Git. Git can seem confusing at first, but learning it step by step reveals why it's becoming the most popular version control system—not just for developers, but for teams collaborating on various projects.
What is version control?
A version control system, or VCS is a program that keeps tracks the changes made to a file. Version control can easily retrieve earlier version of the file. Also, it allows multiple individual work in a same project at the same time without hampering each others process. These two features are so far version control main goal.
With VSC, you can -
See the all changes of your project, when a change was made, who made the change
Retrieve the past file of the whole project or individual files.
See why a change was made along with the reasoning behind it
Create branches for experiments, i.e. solving bugs, trying new systems, trying new features etc. without affecting/changing the main project. This allows experiment all the possible modification that could develop the project.
Another name for version control system is Software configuration management (SCM) system
Distributed version control
Git is distributed. It means the complete file’s history is stored in the server also available to the client. You can make changes on your project’s file without internet, check them locally, then you can sync with the stored file. Even if the server’s gets down, you still have a copy of your project. So, you really don’t have to have a server either. Changes can be shared via e-mails and so so. But, this is not practicable.
Git terminology
To understand Git, you need to know some terms. They might be hard to remember at first, but with practice, you'll get used to them!
Working Tree: It is a set of nested files and directories where your project is stored while you work.
Repository (repo): It is a directory that contains all the history and metadata of your project. It is located on top of the working tree. But, it’s not a part of working tree, it is used for sharing or backup. Repo directory ends with .git - for example myproject.git
Hash: Hash is a bunch on numbers that contains the details of a file. Git uses 160 bits long hashes. To look for changes made to a file, git generates a hashing of the current and previous file and compares them.
(If you don’t know about hashing, don’t worry i got you! Go through Hashing and you are good to go!)
Object: A git repo contains 4 types of object:
Blob: contains an ordinary file
Tree: represents directory; it contains names, hashes and permissions
Commit: represents specific version of the tree
Tag: it is a name attached to a commit
Commit: Commit means making a commit object, in verbs. Simply a terminology
Branch: A branch is a named series of linked commits. The head of the list is called
HEAD
and the body which is created by default while initializing a repo is calledmain
, often named asmaster
in Git. Branches are super helpful because it allows to individual and group works in branches and later combine them together.Remote: A remote is a named reference to another git. When creating a repo, Git creates it’s own remote named
origin
, this is the default remote used for push and pull methods.Commands, subcommands, and options: Git operations are performed through commands such as
git push
andgit pull
. Heregit
is the main command andpush
/pull
is the subcommand. Subcommand specifies the work you want your git to perform.
The Git command line
There are several types of GUIs available for Git. Many programming editor also has interface to Git but they all work different and has their own limitations. None of them implements all of Git’s functionality.
In this blog, everything is based on Git command line—Git commands executed in Microsoft Azure Cloud Shell, more to be exact. Git’s command line interface works same in every operator system. People who see Git only through GUI, sometimes faces error which they can’t resolve, then they have to resort to the command line to get set again.
Git and GitHub
Git is a distributed version control system (DVCS) that lets multiple developers work on the same project. It allows you to work with one or more local branches and push them to a remote repository.
GitHub is a cloud platform that uses Git as its core technology. GitHub makes collaboration easier, offers a website for work, and provides more command-line tools. GitHub serves as the remote repository.
Key features provided by GitHub include:
Issues
Discussions
Pull requests
Notifications
Labels
Actions
Forks
Projects
To learn more about GitHub, see the Introduction to GitHub Microsoft Learn module or the Getting started with GitHub help documentation.
Exercise - Try out Git
You will need a Sandbox for creating a git. Make one from Microsoft and use them for Microsoft Learning only. Any other usages might end up in loss of access. Before you create your first repo, check if Git is installed. Git comes with preinstalled with Azure Cloud Shell, so use that for now.
Configure Git
In cloud shell, check if git is installed. type
git --version
:git --version
You should see output like:
git version 2.39.4
To configure Git, you must define some global variables: user.name and
user.email
. Both are required for you to make commits.Set your name in Cloud Shell with the following command. Replace
<USER_NAME>
with the user name you want to use.git config --global user.name "<USER_NAME>"
Now, use this command to create a
user.email
configuration variable, replacing<USER_EMAIL>
with your e-mail address:git config --global user.email "<USER_EMAIL>"
Run the following command to check that your changes worked:
git --list
Confirm that the output includes two lines that are similar to the following example. Your name and e-mail address will be different from what's shown in the example.
user.name=User Name user.email=user-name@contoso.com
Set up your Git repository
Git tracks changes to files in a folder. We'll create a folder as our working tree (project directory) and initialize a Git repository to start tracking changes.
Create a folder named Cats. This will be your project directory, where all related files are stored.
mkdir Cats
Change to the project directory by using the
cd
command:cd Cats
Now, initialize your new repository and set the name of the default branch to
main
:If you're running Git version 2.28.0 or later, use the following command:
git init --initial-branch=main
Or use the following command:
git init -b main
For earlier versions of Git, use these commands:
git init git checkout -b main
After you run the initialize command, you should see output that's similar to this example:
Initialized empty Git repository in /home/<user>/Cats/.git/ Switched to a new branch 'main'
Now, use a
git status
command to show the status of the working tree:git status
Git responds with this output, which indicates that
main
is the current branch. (It's also the only branch.) So far, so good.On branch main No commits yet nothing to commit (create/copy files and use "git add" to track)
Use an
ls
command to show the contents of the working tree:ls -a
Confirm that the directory contains a subdirectory named .git. (The
-a
option withls
shows hidden files.) This folder is the Git repository, storing metadata and history for the working tree.You usually don't interact with the .git directory directly. Git updates it as changes occur in your files. It's crucial for Git's operation.
Get help from Git
Git, like most command-line tools, has a built-in help function that you can use to look up commands and keywords.
Type the following command to get help with what you can do with Git:
git --help
The command displays the following output:
usage: git [--version] [--help] [-C <path>] [-c name=value] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>] These are common Git commands used in various situations: start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink reset Reset current HEAD to the specified state rm Remove files from the working tree and from the index examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status grow, mark and tweak your common history branch List, create, or delete branches checkout Switch branches or restore working tree files commit Record changes to the repository diff Show changes between commits, commit and working tree, etc merge Join two or more development histories together rebase Forward-port local commits to the updated upstream head tag Create, list, delete or verify a tag object signed with GPG collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
Explore the different Git options, each with its own help page for more details. Some commands may already be familiar if you've used a VCS before.
In the next lesson, you learn more about the commands you just tried and the basics of Git.
Basic Git commands
git status It is the most commonly used Git command. It is used to check if git repo has been initialized properly or not.
git add
git add is the command you use to tell Git to start keeping track of changes in certain file.
git commit
After you've issued some changes for commit, you can save your work to a snapshot by invoking the git commit
command.
git log
the git log
command allows you to see information of previous files. Each commit has a text attached to it (a commit message). This commit helps to keep track of what has been done on the file and what changes h as been saved and made.
git help
Use this command to easily get information about all the command you’ve learned so far. Each command comes with it’s own help pages too. just type git <command> --help
. This one gives information about how to use Git.
End.
Subscribe to my newsletter
Read articles from Fatima Jannet directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by