Day 15: Introduction to Git and Github. Git commands in brief with simple example.
Naga Priyanka Kanna
Ex-TCSer | DevOps | Azure | GIT| Terraform | Docker | Kubernetes | Jenkins | Shell Script
Git and GitHub
Git and GitHub serve related but distinct purposes in the world of software development:
1. Git:
2. GitHub:
Example:
Let's create a simple scenario to illustrate the difference between Git and GitHub:
1. Using Git (Locally):
git init
git add .
git commit -m "Initial commit"
2. Using GitHub (Remotely):
git remote add origin <repository_URL>
git branch -M main
git push -u origin main
In this example, Git is used locally to track changes and manage version control, while GitHub is used remotely to host the Git repository online, enabling collaboration with others and providing additional features like issue tracking and pull requests.
Git Installation :
The installation process for Git is generally similar across different systems, including Windows, macOS, and various Linux distributions. Here's a general guide to installing Git:
Windows:
1. Download Git: Visit the Git website at [https://git-scm.com/](https://git-scm.com/) and download the latest version of Git for Windows.
2. Run the Installer: Once the download is complete, run the downloaded installer file (.exe).
3. Follow Installation Wizard: Follow the instructions provided by the installation wizard. You can typically leave the default settings as they are, but you may choose to customize the installation if needed.
4. Choose Components: You may be asked to choose components to install. It's recommended to leave the default components selected unless you have specific requirements.
5. Choose Start Menu Folder: Select the Start Menu folder where you want Git shortcuts to be placed.
6. Adjusting your PATH environment: When asked, it's recommended to select "Use Git from the Windows Command Prompt" to ensure that Git commands are accessible from the command prompt.
7. Choose HTTPS transport backend: It's recommended to select "Use the OpenSSL library" for secure connections.
8. Configuring line ending conversions: Select the appropriate option for line ending conversions based on your preference.
9. Complete Installation: Once you've made your selections, proceed with the installation. Once completed, Git should be installed on your Windows system.
macOS:
1. Install Homebrew (Optional): If you use Homebrew, you can install Git by running brew install git in the terminal. Otherwise, proceed to the next step.
2. Download Git: You can download the latest version of Git for macOS from the Git website.
3. Run the Installer: Once the download is complete, open the downloaded .dmg file.
4. Follow Installation Instructions: Drag the Git icon into the Applications folder or follow any additional installation instructions provided.
5. Open Terminal: Once installed, open the Terminal application.
6. Verify Installation: Type git --version into the terminal and press Enter. You should see the Git version information if the installation was successful.
Linux:
1. Package Manager Installation: Use your distribution's package manager to install Git. For example, on Ubuntu, you can use apt:
sudo apt update
sudo apt install git
2. Verify Installation: After installation, open a terminal and type git --version. You should see the Git version information if the installation was successful.
Once Git is installed on your system, you can configure it with your name and email using git config, and you're ready to start using Git for version control on your projects.
GitHub Account Signup:
Signing up for a GitHub account is a straightforward process. Here's how you can sign up for a GitHub account:
1. Visit GitHub: Go to the GitHub website at [https://github.com/](https://github.com/).
2. Sign Up: On the GitHub homepage, you'll see a "Sign up" button at the top-right corner of the page. Click on it.
3. Provide Information: You'll be directed to the sign-up page where you need to provide the following information:
- Username: Choose a unique username for your GitHub account. If the username is already taken, GitHub will prompt you to choose a different one.
领英推荐
- Email Address: Enter your email address. This will be used for account notifications and communication from GitHub.
- Password: Choose a strong password to secure your account.
- Captcha: Complete any captcha or verification process to confirm that you're not a robot.
4. Click on "Create Account": Once you've filled in all the required information, click on the "Create Account" button.
5. Verify Email Address (Optional): GitHub may ask you to verify your email address by sending a verification email to the email address you provided. Check your email inbox for a message from GitHub and follow the instructions to verify your email address. If you don't see the email in your inbox, check your spam or junk folder.
6. Complete Account Setup: After verifying your email address (if required), you may be prompted to provide additional information, such as your profile picture or a brief bio. You can choose to complete this step or skip it for now.
7. Explore GitHub: Once your account is created and set up, you can start exploring GitHub, discovering repositories, following other users, and contributing to projects.
You've successfully signed up for a GitHub account. You can now use your GitHub account to create and manage repositories, collaborate with other developers, and contribute to open-source projects.
Git Commands with Simple example:
Let's create a simple Git sample program and make changes to it step by step, while describing the relevant Git commands.
Step 1: Initialize Git Repository
First, let's initialize a new Git repository in our project directory.
$ mkdir my_project
$ cd my_project
$ git init
Initialized empty Git repository in C:/Users/priyanka/my_project/.git/
Step 2: Create a Sample File
Let's create a simple Python file named hello.py with a "Hello, world!" message.
# hello.py
print("Hello, world!")
We can list all files in the directory using the ls -la command.
$ ls -la
total 29
drwxr-xr-x 1 priyanka 197609 0 Feb 2 20:17 ./
drwxr-xr-x 1 priyanka 197609 0 Feb 2 20:17 ../
drwxr-xr-x 1 priyanka 197609 0 Feb 2 20:16 .git/
-rw-r--r-- 1 priyanka 197609 23 Feb 2 20:17 hello.py
Step 3: Check the Status
Let's check the status of our Git repository to see the current state of our files.
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.py
nothing added to commit but untracked files present (use "git add" to track)
Step 4: Add the File
We need to add the hello.py file to the staging area to prepare it for committing.
$ git add hello.py
Step 5: Check the Status Again
After adding the file, let's check the status again to see the changes.
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.py
Step 6: Commit the Changes
Now, let's commit the changes along with a descriptive message.
$ git commit -m "Add hello.py file"
[master (root-commit) a05d70f] Add hello.py file
1 file changed, 1 insertion(+)
create mode 100644 hello.py
Step 7: Make Changes to the File
Let's make some changes to the hello.py file, for example, adding an additional print statement.
# hello.py
print("Hello, world!")
print("Welcome to Git!")
$ cat hello.py
print("Hello, world!")
print("Welcome to Git!")
Step 8: Check the Difference
We can check the difference between the current file and the last commit using the git diff command.
$ git diff
diff --git a/hello.py b/hello.py
index f7cf60e..745639c 100644
--- a/hello.py
+++ b/hello.py
@@ -1 +1,2 @@
print("Hello, world!")
+print("Welcome to Git!")
Step 9: Check Commit History
We can view the commit history to see the changes made over time.
$ git log
commit a05d70f4232922e2acb0bb9d87519e68ee50f9d7 (HEAD -> master)
Author: priyankakanna <[email protected]>
Date: Fri Feb 2 20:24:14 2024 +0200
Add hello.py file
Step 10: Push Changes to Remote Repository
If we have a remote repository set up, we can push our changes to it using the git push command.
git push origin main
[main c0bfe53] Add hello.py file
1 file changed, 2 insertions(+), 1 deletion(-)
PS C:\Users\priyanka\DevOps-Article> git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 301 bytes | 301.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/priyankakanna/DevOps-Article.git
7bf8722..c0bfe53 main -> main
Step 11: Pull Changes from Remote Repository
If there are changes in the remote repository that we want to incorporate into our local repository, we can pull them using the git pull command.
$ git pull origin master
These are some of the basic Git commands used in a typical development workflow. They help manage changes, track history, collaborate with others, and maintain version control effectively throughout the development process.
In future articles will delve into more Git commands and provide detailed explanations about Git branching.