0% found this document useful (0 votes)
0 views25 pages

Git and GitHub

This document is a practical guide on using Git and GitHub for test automation, detailing the installation, setup, and usage of Git commands. It covers creating local projects, managing repositories, branching, merging changes, and resolving conflicts. Additionally, it provides a quick revision of essential Git terminologies and commands with examples.

Uploaded by

harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views25 pages

Git and GitHub

This document is a practical guide on using Git and GitHub for test automation, detailing the installation, setup, and usage of Git commands. It covers creating local projects, managing repositories, branching, merging changes, and resolving conflicts. Additionally, it provides a quick revision of essential Git terminologies and commands with examples.

Uploaded by

harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Git and GitHub for Test Automation:

A Practical Self-Explanatory Guide

What Are Git and GitHub?

• Git: A tool that keeps track of changes in your code and test scripts. It allows you to save
different versions of your work and go back to older ones if needed.

• GitHub: A website where you can store and share your Git projects with others. It’s great for
collaboration and working with a team.

Step 1: Installing Git

To use Git:

1. Download Git:
I went to git-scm.com and downloaded the latest version for my operating system.

2. Install Git:
I ran the installer and kept the default options.

3. Check Installation:
After installation, I opened the terminal (or Command Prompt) and typed:

git –version

If a version number appeared, it meant Git was installed successfully.

Step 2: Setting Up Git

Before using Git, I configured my name and email. This information is needed to identify who made
changes.

1. Set Your Name: git config --global user.name "Your Name"

2. Set Your Email:

git config --global user.email your.email@example.com

3. Check Your Settings:

git config –list


This shows the name and email I just set.

Step 3: Creating a Local Project

I created a new folder on my system called TestingWithGitHub to store my test scripts. Then,
I opened this folder in IntelliJ IDEA so I could work on it directly.

Next, I opened IntelliJ IDEA, navigated to File > Open, and selected the TestingWithGitHub folder.
This made it easy to work within the folder directly from IntelliJ.
Before

After

To start using Git commands, I opened the built-in terminal in IntelliJ. I navigated to the path where
the TestingWithGitHub folder was located using the cd command:
Step 4: Creating a GitHub Repository

Next, I logged into my GitHub account and created a new repository named TestingGitRepo. In
this repository, I added a file called TestInfo.txt with the following content:

This file contains information about test automation:

- Tools: Selenium, Postman

- Frameworks: TestNG, Cucumber

GitHub provided a URL for this repository, which I copied.

Step 5: Cloning the Repository

To connect my local project with the GitHub repository, I used the clone command. In the terminal, I
typed: git clone <GitHub-Repo-URL>
This command downloaded the GitHub repository into my local system. Now, I could see the
TestingGitRepo folder and the TestInfo.txt file inside IntelliJ.

This copied all the files from the GitHub repository into my local folder.

Step 6: Adding and Committing Files

I navigated into the newly cloned repository: cd TestingGitRepo

In IntelliJ, I created a new file named TestCases.txt with this content:

Test Case 1: Verify login functionality.

Test Case 2: Validate form submission.


The red line of the file indicates the file is untracked which means it is not in git

I saved the file, then checked its status with:

git status

The file showed as "untracked," meaning Git was not tracking it yet.

Staging the File

To tell Git to start tracking the file, I staged it using the following command: git

add TestCases.txt

This moved the file to the staging area, which is where changes are prepared before being
committed.

Now the colour of the file changed to green which indicates it is staged now
To confirm check the git status

git status

The file TestCases.txt is now in the staging area, which means it is ready to be committed to the local
repository.

Additional Point: When we refer to the local repository, it is stored in the .git folder, which is created
when we clone or initialize a repository. This folder is usually hidden in the project directory.

Once the TestCases.txt file is committed, it will be saved as part of the version history in the .git
folder (local repository).
Then, I committed the changes with:

git commit -m "Added TestCases.txt"

This saved the changes in my local Git repository.

Here’s what happened:

• The commit saved the changes (adding the new file) in my local repository.
• It created a new version of my project’s history, but the changes were still local and not yet
visible on GitHub.

Step 7: Pushing Changes to GitHub

To upload the changes to GitHub, I used:

git push origin master


Now, the TestCases.txt file appeared in my GitHub repository.

Step 8: Creating a New Branch

To organize my work, I created a new branch named feature-test-cases: git

checkout -b feature-test-cases

This command created and switched to the new branch.

To verify which branch it points

git branch -a

Push the branch to the remote repository:


git push origin feature-test-cases

Note:

Creating a branch only? No need to stage or commit before pushing.

Made changes in the branch? You must stage and commit before pushing.

I then added a file called AdvancedTestCases.txt on GitHub with the content:

Test Case 3: Verify user profile updates.

Test Case 4: Validate logout functionality.


To pull this file into my local repository, I ran:

git pull origin feature-test-cases

Again, I made changes in AdvancedTestCases and TestCases text file.

Since I Made changes in the branch I did stage and commit before pushing
I could see the changes in feature-test-case branch in GITHUB repo after push. Verify

master branch from GITHUB

From local git

checkout master
The newly added file is present only in new branch which is feature-test-case and not in master.

Step 9: Managing Multiple Repositories

I wanted to practice managing more than one repository.

1. Created a New Repository:


On GitHub, I created another repository called TestingGitRepoForPullRequest.

2. Initialized Git:
Before initialize it have the proper folder structure

In my local folder, I navigated to Base project folder TestingWithGithub

Now I created a new directory mkdir

TestingGitRepoForPullRequest
Then navigate to new repo and initialize git

git init

This created a .git folder to track the project.

1. Adding and Committing Files:


I created a file named TestPlan.txt with this content:

Test Plan: Automation scripts for E2E testing.

Tools: Selenium, Postman, TestNG. Then,

I staged and committed the file: git add .

git commit -m "Added TestPlan.txt"

2. Connected to GitHub:
I linked my local folder to the GitHub repository using:
git remote add origin <GitHub-Repo-URL>

Step 10: Merging Changes

To merge changes from one branch to another:

1. Switched to the master branch:

git checkout master

2. Merged the changes:

git merge feature-test-cases


After merge now the AdvancedTestCases.txt file is merged to master from feature-test-cases branch

3. Created a pull request on GitHub for review and merged it.

Still in GITHUB repo the AdvancedTestCases.txt file is missing even after merge, because we
merged in local repo but to merge in remote repo master or main branch we need to raise a
pull request.
Now I could see the merged file in master branch.

Step 11: Conflict

To demonstrate conflict I have created one more branch from master This

time from GITHUB

Switched to new branch

Updating the file AdvancedTestCases.txt


Now merged this file to master from feature-test-cases-from-yogesh, but still the changes are
reflected in local to make the changes in remote repo. Raise a pull request and merge.

Now the changes merged in master remote repo


Again, make the changes in same file AdvancedTestCases.txt but from different branch feature-
testcases.

Updating the file AdvancedTestCases.txt

Now merged this file to master from feature-test-cases, but still the changes are reflected in local to
make the changes in remote repo. Raise a pull request and merge.

Merge to master and make a pull request


You will instantly see the Conflict message because I tried updating the same file from different
branch and I pushed both of them to master branch.

To resolve this approve the pull request and click on Resolve conflicts button
Resolve manually
Quick Revision: Git Terminologies and Commands (With Examples)

1. Git

• Purpose: Checks if Git is installed on your system.


• Command:
git --version
• Example Output:
git version 2.42.0

2. GitHub

• Purpose: A cloud platform to store, manage, and collaborate on Git repositories.

3. Git Config

• Purpose: Set your identity for commit messages.


• Commands & Examples:

git config --global user.name "Yogesh Pandian"


git config --global user.email "yogesh.pandian@example.com"
git config --list

• Output:

user.name=Yogesh Pandian
user.email=yogesh.pandian@example.com

4. Git Repository

• Purpose: Initialize a Git project in your local folder.


• Command:
git init
• Example:

mkdir my-project
cd my-project
git init

• Output:

Initialized empty Git repository in /Users/yourname/my-project/.git/

5. Clone

• Purpose: Copy an existing GitHub repo to your machine.


• Command:
git clone https://github.com/username/repo.git
• Example:

git clone https://github.com/octocat/Hello-World.git

6. Add

• Purpose: Stage changes (new or modified files) for commit.


• Command:
git add filename
• Example:

git add index.html

7. Commit

• Purpose: Save staged changes with a message.


• Command:
git commit -m "Meaningful commit message"
• Example:

git commit -m "Added login functionality"

8. Push

• Purpose: Upload local commits to GitHub.


• Command:
git push origin branch-name
• Example:

git push origin main

9. Pull

• Purpose: Get the latest changes from GitHub.


• Command:
git pull origin branch-name
• Example:

git pull origin main

10. Branch

• Purpose: Create and switch between versions.


• Commands & Examples:

git branch feature-ui # Create branch


git checkout -b bug-fix # Create and switch
git checkout main # Switch to main
11. Merge

• Purpose: Combine another branch into current one.


• Commands & Example:

git checkout main # Switch to main


git merge feature-ui # Merge feature-ui into main

12. Status

• Purpose: View current file status (tracked, staged, etc.)


• Command:
git status
• Example Output:

Changes to be committed:
modified: index.html

13. Remote

• Purpose: Link your local project to a GitHub repo.


• Command:
git remote add origin <repo-url>
• Example:

git remote add origin https://github.com/johndoe/my-project.git

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy