About GIT
Git is an essential tool for managing code versions, collaborating with teams, and keeping track of
changes in your project’s files. While Git Bash is popular among developers, Windows Command
Prompt (CMD) can also be used to run Git commands effectively. This guide will walk you through
how to set up Git in CMD, common commands you can use, and helpful tips for navigating Git from
the command line.
Git
How to Run Git Commands in CMD?
Git is an essential tool for managing code versions, collaborating with teams, and keeping track of
changes in your project’s files. While Git Bash is popular among developers, Windows Command
Prompt (CMD) can also be used to run Git commands effectively. This guide will walk you through
how to set up Git in CMD, common commands you can use, and helpful tips for navigating Git from
the command line.
Prerequisites: Installing Git on Windows
https://youtu.be/kY5HtrkjSj0?si=7vE891XpkH0SeH_y
If you haven’t installed Git on your Windows machine, follow these steps:
1. Download Git: Go to the official Git website and download the latest version for Windows.
2. Run the Installer: Open the downloaded file, and follow the installation prompts. Ensure you
check the option “Add Git to your PATH environment variable” during setup.
3. Verify the Installation: Open CMD and type the following command:
git --version
If Git is installed correctly, this command will return the installed version of Git.
Basic Git Commands to Get Started in CMD
Using Git Commands
git init: Initializes a new Git repository.
git clone <repository>: Clones an existing repository.
git status: Displays the status of your working directory and staging area.
git add <file>: Adds a file to the staging area.
git commit -m "message": Commits the changes in the staging area with a message.
git push: Pushes changes to a remote repository.
git pull: Pulls changes from a remote repository.
git log: Shows the logs of all the commits made.
git branch: Creates a new branch.
git merge: Merges changes from one branch to another branch.
git config: Configures the username and email id.
git tag: Displays all tags
Various Approaches To Use Git For Version
Control
1.GIT Install
2.GIT config
3. Create GIT repository
4. GIT INIT(initialization)
5.GIT workflow
6. Staging area
7. Adding changes to the Stage area
1: Git via Command Line
This is the most common method where developers use terminal commands to interact with Git.
By utilizing git through command prompt, one has exhaustive authority over git functions.
Step 1: Install Git.
1. Download and install Git from the official website : Git Downloads.
2. After Installation, verify Git by running the following command in your terminal.
Step 2: Initialize a Git Repository
1. Navigate to your project folder in the terminal.
2. Initialize Git in the project folder by running:
Step 3: Staging Changes
To start tracking files, you need to stage them. This moves the files into a "staging area" before
committing them:
GIT WORKFLOW
Git is a distributed version control system that allows developers to track changes, collaborate, and
manage code efficiently. Understanding its basic process flow is essential for effective usage. Below is
a step-by-step explanation of the Git workflow:
Git Workflow Commands Overview
Working Directory - Where you make changes
git add - Stage changes
git commit - Save changes to your repository
git push - Share changes with others
git status - Check what's going on
Undo/Amend - Fix mistakes (git restore, git reset, git commit --amend)
1. Working Directory
The working directory is where you make changes to your files. These changes can include creating,
modifying, or deleting files. However, Git does not track these changes until they are staged.
Example:
# Check the current status of your working directory
git status
2. Staging Area (Index)
The staging area is where you prepare changes before committing them. You can selectively add files
to the staging area using the git add command.
Example:
# Stage a specific file
git add filename.txt
# Stage all changes
git add
3. Committing Changes
Once changes are staged, they can be committed to the local repository. A commit represents a
snapshot of your project at a specific point in time. Always include a meaningful commit message to
describe the changes.
Example:
# Commit staged changes with a message
git commit -m "Add feature X"
4. Pushing to a Remote Repository
After committing changes locally, you can push them to a remote repository (e.g., GitHub, GitLab) to
share with others. This ensures your changes are backed up and accessible to collaborators.
Example:
# Push changes to the main branch of the remote repository
git push origin main
5. Pulling Changes
To sync your local repository with the latest changes from the remote repository, use the git
pull command. This fetches and merges updates from the remote branch into your local branch.
Example:
# Pull updates from the remote main branch
git pull origin main
6. Resolving Conflicts
If multiple developers modify the same lines of code, Git may encounter conflicts during a merge or
pull. You must manually resolve these conflicts, stage the resolved files, and complete the merge.
Example:
# Resolve conflicts, then stage the resolved file
git add resolved_file.txt
# Continue the merge process
git commit
7. Branching and Merging
Branches allow you to work on features or fixes independently. Once the work is complete, you can
merge the branch back into the main branch.
Example:
# Create and switch to a new branch
git checkout -b feature-branch
# Merge the feature branch into main
git checkout main
git merge feature-branch
Best Practices
Commit frequently with clear messages to maintain a clean history.
Use git status often to track changes and avoid mistakes.
Push changes regularly to keep the remote repository updated.
Use feature branches for isolated development and merge them after review.
This process flow ensures a structured and collaborative approach to managing code with Git.
Practical 2
How to Set Public SSH Key on Windows 10
What is an SSH Key?
An SSH key is a pair of cryptographic keys used for secure communication between
systems. The key pair consists of:
Public Key: Shared with the remote system (like GitHub).
Private Key: Kept secret and stored securely on your local machine.
Public vs. Private Keys
The public key is added to your GitHub account, while the private key remains on your
machine. When you initiate a Git operation, GitHub verifies that the private key on your
system matches the public key associated with your account.
Security Considerations
Private Key Protection: Keep your private key secure; if compromised,
unauthorized users could access your repositories.
Passphrases: Adding a passphrase to your SSH key adds an extra layer of security.
Benefits of Using SSH over HTTPS for Git Operations
Security: SSH keys provide encrypted communication, making them more secure
than using a username and password with HTTPS.
Convenience: Once set up, SSH keys do not require you to re-enter credentials each
time you interact with a remote repository.
Automation: SSH keys are ideal for automated deployments and scripts, as they
allow non-interactive authentication.
Generating and retrieving your public SSH key on Windows 10 is a straightforward process. SSH keys
are essential for secure connections between your local machine and remote servers. Here are the
steps to generate and view your public SSH key using different methods.
Using Command Prompt or PowerShell
Windows 10 includes the OpenSSH client by default, which allows you to generate SSH keys directly
from the Command Prompt or PowerShell.
1. Open Command Prompt or PowerShell: Press the Windows key, type cmd or powershell,
and press Enter.
2. Generate SSH Key Pair: Type the following command and press Enter: ssh-keygen You will be
prompted to enter a file name to save the key. Press Enter to use the default location (C:\
Users\[YourUserName]\.ssh\id_rsa).
3. Enter a Passphrase (optional but recommended): Enter a passphrase for added security, or
press Enter to skip.
4. Locate the Public Key: The public key will be saved as id_rsa.pub in the .ssh directory. To
view it, use the following command: type C:\Users\[YourUserName]\.ssh\id_rsa.pub
To copy and generate SSH go to your github account and create new ssh key
Test the SSH Connection: Verify that the SSH key was added successfully by connecting to your Git
service: ssh -T git@github.com If this is the first connection, you may be prompted to verify the host.
Type "yes" to proceed. You should receive a welcome message confirming the connection.
Follow this video
https://youtu.be/itU8KBuE8jk?si=ollDKI9tMtoThlDG
Go to Github account-Settings-SSH and GPS keys New SSH key-put username-Copy the
key that you have saved in notepad---Add SSH Key
How to add remote repository as origin
Steps to Add a Remote Repository as the Origin
1. Initialize the Repository (if not already initialized):
git init
2.Add the Remote Origin
gAit remote add origin your-repository-url
3.Verify the Remote Origin:
git remote -v
4.: Push to the Remote Repository:
git push -u origin main
Practical 4
AIM:- Perform the following in Git
• Removal of Git repository/tracking
• git clone
• gitignore (scenario based usage(e.g.: a f ile/directory))
Removing a Git Repository or Stopping Git Tracking: To remove a Git repository from your project,
you can stop Git from tracking changes within a specific directory. This may be necessary if you
decide to discontinue version control for a particular project, essentially disconnecting the directory
from Git tracking.
git clone:
The git clone command creates a local copy of an existing Git repository from a remote server, such
as GitHub or GitLab, on your machine. This operation is essential for collaborative work, enabling
multiple users to work on the same codebase by cloning a shared repository and contributing to it
locally.
gitignore:
The .gitignore file tells Git to exclude specific files or directories from being tracked. It’s particularly
helpful for files generated by the system, temporary files, or sensitive information that shouldn’t be
committed to the repository. By adding files or patterns to .gitignore, you ensure that they won’t be
included in any commits.
Practical 5
AIM: Show the use of –
• Git fetch
• git diff command,
• git difftool
• git restore command (to Restore files(e.g.: 2 out of total 5 files) from staging area that you don’t
want to commit)
1. git fetch: The git fetch command downloads updates, including new commits, files, and branches,
from a remote repository to your local repository without merging them into your current branch. It
allows you to view changes that others have made without affecting your local working directory,
letting you decide when to merge them.
2. git diff: The git diff command compares different states of your files, showing differences between
the working directory and the staging area, between branches, or between specific commits. It
provides a detailed view of changes, making it easier to review and analyze modifications before
committing or merging them.
3. git difftool: The git difftool command serves as an alternative to git diff by allowing you to compare
file changes using an external diff tool of your choice. This can be particularly useful for complex
modifications, as external tools often offer a more visual comparison experience.
4. git restore: The git restore command is used to discard changes in the working directory or to
revert files to a previous state from the staging area or a specific commit. This command is helpful
when you want to undo unwanted changes before committing or if you need to retrieve a specific file
version.
Practical 6
AIM: Perform following code changes using Git commands (Undoing/ Reverting/Resetting)
• git checkout (undo uncommitted changes)
• git revert (undo committed changes)
• git reset (resetting changes to a previous timestamp)
git checkout: The git checkout command allows you to discard any uncommitted changes in your
working directory, reverting files to their previous committed state. It’s useful for undoing
modifications you don’t wish to keep, especially if they haven’t been staged yet.
git revert: The git revert command creates a new commit that undoes the effects of a previous
commit, without modifying the existing commit history. This command is ideal for reversing specific
changes while preserving a clear record of all commits, making it easy to maintain a transparent
history.
git reset: The git reset command moves the current branch to a specific previous commit, effectively
removing all commits made after that point. This can be used to discard recent changes permanently,
either in the working directory, the staging area, or both, depending on the reset mode selected.
Git Branching Operations:
1. Creating Branches: Branches allow you to work on new features, bug fixes, or experimental ideas
in isolation from the main codebase. By creating a branch, you can make changes independently
without impacting the main project.
2. Switching Branches: The ability to switch branches lets you move between different states of your
project. Before switching, it’s important to commit or stash any changes to prevent data loss,
allowing you to work on multiple tasks in parallel.
3. Merging Branches: Merging integrates changes from one branch into another, typically bringing a
feature branch into the main branch once work is complete and tested. This ensures that updates
from the feature branch are applied to the main codebase.
4. Deleting Branches: ◦ Local Deletion: Removing a branch from your local repository clears
unnecessary branches without affecting the remote. ◦ Remote Deletion: Deleting a branch from a
remote repository removes it from the shared server, keeping the repository clean and organized.