Tech Hub

@ Solution Architecture Works

GitHub Fundamentals – Part 1 of 2

Exercise – Try Git

Estimated reading: 9 minutes 9 views

Note

Before you can create your first repository, you must ensure that Git is installed and configured.

Configure Git


In Git Bash, to check that Git is properly installed, type the following command:

git --version

tip

You can copy commands to the clipboard.
To paste, right-click on a new line in the Git Bash terminal and select Paste, or use the keyboard shortcut Shift+Insert (⌘+V on macOS).

You should see a result similar to this example:

git version 2.7.4

Exemple:

2. To configure Git, you need to set some global variables: user.name and user.email.

These two variables are required in order to make commits.

3. Set your name in Git Bash using the following command.
Replace <USER_NAME> with the username you want to use:

git config --global user.name "<USER_NAME>"

5. Now use this command to create the configuration variable user.email, replacing <USER_EMAIL> with your email address:

git config --global user.email "<USER_EMAIL>"

6. Run the following command to check that your changes worked:

git config --list

7. Confirm that the output contains two lines similar to the following example.
Your name and email address will be different from those shown in the example.

Set up your Git repository


Git works by tracking changes made to files in a specific folder. We’re going to create a folder that will serve as the working tree (project directory) and let Git know about it so it can start tracking changes.
We tell Git to start tracking changes by initializing a Git repository in this folder.

Method 1

Start by creating an empty folder for your project, then initialize a Git repository inside it.

Create a folder named Cats. This folder will be the project directory, also called the working tree. The project directory is where all files related to your project are stored. In this exercise, it’s where your website and the files that make it up and define its content will be saved.

mkdir Cats

2. Navigate to the project directory using the cd command:

cd Cats

3. Now, initialize your new repository and set the default branch name to main:

If you are using Git version 2.28.0 or later, use the following command:

cd Cats

Or use the following command:

git init -b main

For earlier versions of Git, use the following commands:

git init
git checkout -b main

After running the initialization command, you should see an output similar to this example:

Initialized empty Git repository in /home/<user>/Cats/.git/

Switched to a new branch 'main'

Now, use the git status command to display the state of the working tree:

git status

Git responds with this output, indicating 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)

5. Use the ls command to display the contents of the working tree:

ls -a

Set up your Git repository


Git works by monitoring changes made to files in a specific folder. We’ll create a folder that will serve as the working tree (project directory) and let Git know about it so it can start tracking changes.
We tell Git to start tracking changes by initializing a Git repository in this folder.

Method 1

Start by creating an empty folder for your project, then initialize a Git repository inside it.

Create a folder named Cats. This folder will be the project directory, also called the working tree. The project directory is where all files related to your project are stored. In this exercise, it’s where your website and the files that make it up and define its content will be saved.

2. Navigate to the project directory using the cd command:

3. Now, initialize your new repository and set the default branch name to main:
If you are using Git version 2.28.0 or later, use the following command:

Or use the following command:

For earlier versions of Git, use the following commands:

After running the initialization command, you should see an output similar to this example:

Now, use the git status command to display the state of the working tree:

Git responds with this output, indicating that main is the current branch.
(It’s also the only branch.) So far, so good.

5. Use the ls command to display the contents of the working tree:

Confirm that the directory contains a subdirectory named .git.
(Using the -a option with the ls command is important because Linux normally hides files and folders whose names begin with a dot.)
This folder is the Git repository — the directory where Git stores metadata and the history of the working tree.

In general, you don’t interact directly with the .git folder. Git updates the metadata it contains as the state of the working tree changes, in order to track changes to your files.
This folder should not be modified manually, but it is extremely important to Git.

Method 2

After downloading Git:

Go to the website github.com, click on Sign up, enter your information, type the confirmation code received in your email, re-enter your name and password, create a repository, select Repository, add a name, add a description, click on Create repository, and copy the link in the top right corner (you’ll need it).

2. Open your project in your code editor, go to the terminal of your project, and use GitHub’s basic commands. In your terminal, do the following:

  • git init (Create a folder on your local machine)
  • git add . (Add all files)
  • git commit -m "first commit" (Create a save point with a message)
  • git remote add origin "<The link copied from your GitHub>" (Link your local project to GitHub)
  • git push -u origin main (Push your code to GitHub using the main branch)
  • git branch -m main (Rename the current branch to main)
  • git push -u origin main (Synchronize the renamed branch with GitHub)

Get help with Git


Git, like most command-line tools, has a built-in help feature you can use to look up commands and keywords.

Type the following command to get help on what you can do with Git:

git --help

2. 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.

Read through the different options available with Git and note that each command has its own help page, which you can consult as you deepen your knowledge.
Not all of these commands will be clear to you yet, but some may seem familiar if you’ve used a version control system (VCS) before.

In the next lesson, you’ll learn more about the commands you just tried, as well as the basics of Git.

Share this Doc

Exercise – Try Git

Or copy link

CONTENTS