COSC Git Workshop

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Artificial Intelligence Lab (20CSC29)

Objectives of the workshop:

● Understanding of GitHub environments


● Using GitHub packages and libraries to frame a standard project and commit back to
GitHub

What are Version Control Systems?

A version control system tracks any kind of changes made to the project file, why these changes
were made, and references to the problems fixed or enhancements introduced. It allows
developer teams to manage and track changes in code over time. It allows a person to switch to
the previous states of the file, compare the versions and helps to identify issues in a file in a
more efficient way.

What Is Git?

● Git is one of the ways of implementing the idea of version control. It is a Distributed Version
Control System (DVCS).
● Unlike a Centralized Version Control System that uses a central server to store all files and
enables team collaboration, DVCS just can be implemented just with the help of a desktop,
single software available at a command line. So, the failure of the central server does not
create any problems in DVCS. So, a lot of operations can be performed when you are offline.

How to install Git?

Installing on Linux
If you want to install the basic Git tools on Linux via a binary installer, you can generally do so
through the package management tool that comes with your distribution. If you’re on Fedora
(or any closely-related RPM-based distribution, such as RHEL or CentOS), you can use dnf:

$ sudo dnf install git-all

If you’re on a Debian-based distribution, such as Ubuntu, try apt:

$ sudo apt install git-all

For more options, there are instructions for installing on several different Unix distributions on
the Git website, at https://git-scm.com/download/linux.
Installing on macOS
There are several ways to install Git on macOS. The easiest is probably to install the Xcode
Command Line Tools. On Mavericks (10.9) or above you can do this simply by trying to run git
from the Terminal the very first time.

$ git --version

If you don’t have it installed already, it will prompt you to install it.

If you want a more up to date version, you can also install it via a binary installer. A macOS Git
installer is maintained and available for download at the Git website, at
https://git-scm.com/download/mac.

Installing on Windows
There are also a few ways to install Git on Windows. The most official build is available for
download on the Git website. Just go to https://git-scm.com/download/win and the download
will start automatically. Note that this is a project called Git for Windows, which is separate
from Git itself; for more information on it, go to https://gitforwindows.org.

To get an automated installation you can use the Git Chocolatey package. Note that the
Chocolatey package is community-maintained.
Creating an account on GitHub

To get started with GitHub, you'll need to create a free personal account on GitHub.com and
verify your email address.

Every person who uses GitHub.com signs in to a personal account. Your personal account is
your identity on GitHub.com and has a username and profile.
How to use Git?

Step 1: git config

→ git config --global user.name "My Name"


→ git config --global user.email "[email protected]"

In Git, the git config command is used to configure settings specific to Git operations. These
settings are stored in three distinct scopes: system, global, and local.

System Configuration: This scope applies system-wide, affecting all users and repositories on
the system.
Configuration settings at this level are stored in the /etc/gitconfig file.

Global Configuration: Global configurations are specific to a single user and apply to all
repositories associated with that user.

Local Configuration: Local configurations are specific to a single repository and override
settings from higher levels.
Step 2: Create a folder for your project and add the required files

● Create a new folder and open in on your code editor and initialize it using git init.
● Add the necessary files for your project into the folder

waterjug.py

class Node:
def _init_(self, state, parent):
self.state = state
self.parent = parent

def get_child_nodes(self, capacities):


a, b = self.state
max_a, max_b = capacities
children = []

children.append(Node((max_a, b), self))


children.append(Node((a, max_b), self))
children.append(Node((0, b), self))
children.append(Node((a, 0), self))

if a + b >= max_b:
children.append(Node((a - (max_b - b), max_b), self))
else:
children.append(Node((0, a + b), self))

if a + b >= max_a:
children.append(Node((max_a, b - (max_a - a)), self))
else:
children.append(Node((a + b, 0), self))
return children

def dfs(start_state, goal_state, capacities):


start_node = Node(start_state, None)
visited = set()
stack = [start_node]

while stack:
node = stack.pop()

if node.state == goal_state:
path = []
while node.parent:
path.append(node.state)
node = node.parent
path.append(start_state)
path.reverse()
return path

if node.state not in visited:


visited.add(node.state)
for child in node.get_child_nodes(capacities):
stack.append(child)

return None

start_state = (0, 0)
a, b = map(int, input("Enter the capacities of jugs: ").split())
c, d = map(int, input("Enter the capacities of goal state: ").split())
goal_state = (c, d)
capacities = (a, b)

path = dfs(start_state, goal_state, capacities)


print(path)
Step 3: git init

→ git init

It is used to create an empty Git repository or reinitialize an existing one

Step 4: git add

The git add command is used to stage files in your project directory for the next commit. It
takes one or more paths as arguments and adds the current content of those paths to the
staging area. The staging area is a file that Git uses to keep track of which files are going to be
included in the next commit.

● git add <file> - Stages all changes in <file> for the next commit.
● git add <directory> - Stages all changes in <directory> for the next commit.
● git add . - Stages all changes in the current working directory for the next commit.
● git add -A - Stages all changes in the current working directory and all of its
subdirectories for the next commit.

Step 5: git status

The git status command displays the state of the working directory and the staging area. It lets
you see which changes have been staged, which haven't, and which files aren't being tracked by
Git.
Step 6: git commit

A Git commit is a snapshot of your repository at a specific time. Commits are the building
blocks of "save points" in Git's version control. The git commit command takes several
options, but the most important one is the -m option, which allows you to specify a commit
message.

Step 7: Create a remote repository on Github

A remote repository on GitHub serves as a central hub for collaboration, version control, and
project management.

● Go to your GitHub account, click on your profile in the top right corner, and click on Your
repositories.

● Click on the new icon to create a new repository


● Create a new repository with the required specifications.
● Once the repository is created, click on the Code icon and copy the HTTPS code, as we
will use it to push your code.

Step 8: Adding your new remote repository to your local git repository

The git remote command is essentially an interface for managing a list of remote entries that
are stored in the repository's ./. git/config file.

git remote add <name> <url>

Create a new connection to a remote repository. After adding a remote, you’ll be able to use <
name> as a convenient shortcut for <url> in other Git commands.

Step 9: git push

The git push command is used to upload local repository content to a remote repository.
Pushing is how you transfer commits from your local repository to a remote repo. Pushing has
the potential to overwrite changes, caution should be taken when pushing.
Step 10: git pull

The git pull command is used to fetch and download content from a remote repository and
immediately update the local repository to match that content. Merging remote upstream
changes into your local repository is a common task in Git-based collaboration workflows. The
git pull command is a combination of two other commands, git fetch followed by git merge. In
the first stage of operation git pull will execute a git fetch scoped to the local branch that HEAD
is pointed at. Once the content is downloaded, git pull will enter a merge workflow. A new
merge commit will be created and HEAD updated to point at the new commit.

● When you pull changes, you are always prone to encountering Merge conflicts. Often,
merge conflicts happen when people make different changes to the same line of the same
file, or when one person edits a file and another person deletes the same file. You must
resolve all merge conflicts before you can merge a pull request on GitHub.

● Here is an example of encountering a merge conflict.


● You can use git stash to save all the uncommitted changes into a stack, the local becomes
the same as the previous git.

After stashing your changes, you can safely pull remote changes using command git pull and
the reflected changes in the remote repository will appear in your local repo.

To reflect the stashed changes, which are present in a stack like data structure, you can simply
pop the stash and the changes will be reflected. This is done using the command git stash pop.
● You can resolve the merge conflicts as shown below on your code editor.
Branching

A branch represents an independent line of development. Branches serve as an abstraction for


the edit/stage/commit process. You can think of them as a way to request a brand new working
directory, staging area, and project history. New commits are recorded in the history for the
current branch, which results in a fork in the history of the project.

The git branch command lets you create, list, rename, and delete branches. It doesn’t let you
switch between branches or put a forked history back together again. For this reason, git
branch is tightly integrated with the git checkout and git merge commands.

● git branch <branch>

Create a new branch called <branch>.

● git checkout <branchname>

Switching branches is a straightforward operation. Executing the following will point HEAD to
the tip of <branchname>.
● git checkout -b <new-branch>

The above example simultaneously creates and checks out <new-branch>.


● However, you can not directly push the changes made on a branch to your remote
repository, upon attempting to push you will encounter the following error

● To push the branch changes we need to set upstream as follows: git push
--set-upstream origin <branch name>

● Merging branches in Git is a fundamental operation that allows you to integrate changes
from one branch into another. This is often used when you have been working on a
feature or a bug fix in a separate branch and you want to bring those changes into
another branch, typically the main branch.
● You can merge the branches you created by submitting a pull request on github.
● Click on compare and pull request and add the necessary details to submit a pull request.
Once the owners of the repository accept the Pull requests your changes will be merged.
Contributing to Open Source Projects:

● GitHub is an excellent platform for finding and contributing to open-source projects.


You can find many public repositories such as below:
Forking

● Forks let you make changes to a project without affecting the original repository, also
known as the "upstream" repository. After you fork a repository, you can fetch updates
from the upstream repository to keep your fork up to date, and you can propose changes
from your fork to the upstream repository with pull requests. A fork can be owned by
either a personal account or an organization.
● You can fork any repository by clicking on the fork button that’s displayed when viewing
a repository. You can not fork your own repository.
● After clicking on fork, specify the details of the fork and click on create fork.
● Once you have forked the repository, it will be reflected in your repositories.
Cloning:

git clone is a Git command line utility that is used to target an existing repository and create a
clone, or copy of the target repository. In this page, we'll discuss extended configuration
options and common use cases of git clone.

git clone <repo> <directory>

● Once you have completed making changes to the cloned repository, to contribute these
changes you need to submit a Pull Request.
● You can do this by clicking on Contribute and Open pull request
● Compare the changes and click on Create pull request. Once the owners of the repository
approve of the pull request your changes will be made to the original repository.

You might also like