Intro to

- Sumith1896

- martiansideofthemoon

What git?

  • What is git?
  • What is version controlling?
  • Why version controlling?

What is git?

Git  is a distributed revision control software. Git was initially designed and developed by Linus Torvalds for Linux

What is version controlling?

Having infinite version of the same  file.

#CS101_nostalgia

Basically...

...the purpose Git was designed to serve. When you have a Microsoft Word file, you either overwrite every saved file with a new save, or you save multiple versions. With Git, you don’t have to. It keeps “snapshots” of every point in time in the project’s history, so you can never lose or overwrite it.

In case things go wrong, revert back to the most recent working version

Why version controlling?

  • File names and directory structures that are consistent for all team members.
  • Making changes with confidence, and even reverting when needed.
  • Easily deploying different versions of your code to staging or production servers.
  • Understanding who made a change and when it happened.

Why git?

  • Revert back to the most recent working one.
  • Try out new experimental features.
  • Collaborating with other people.
  • Statistics of contribution.
  • Easily find the people responsible for certain part of code :P.
  • Productivity.
  • Sanity!!

Today's Agenda!

  • How to start using Git / Github for your personal projects.
  • How to collaborate among friends using Git / Github
  • How to contribute to an open source organization
  • Surprise? :D

Using Git in your Projects

  • Setting up git
  • Terminologies
  • Basic git commands

Setting up git

1. Installation

$ sudo apt-get install git

2. Personalization

$ git config --global user.name "Lorem Ipsum"
$ git config --global user.email loremipsum@foo.com

3. Initialization of a project

$ git init

Terminologies

Repository: A directory or storage space where your projects can live. Sometimes GitHub users shorten this to “repo.” It can be local to a folder on your computer, or it can be a storage space on GitHub or another online host. You can keep code files, text files, image files, you name it, inside a repository.

Commit: This is the command that gives Git its power. When you commit, you are taking a “snapshot” of your repository at that point in time, giving you a checkpoint to which you can re-evaluate or restore your project to any previous state.

 

Basic git commands

git init: Initializes a new Git repository. Until you run this command inside a repository or directory, it’s just a regular folder. Only after you input this does it accept further Git commands.

git config: Short for “configure,” this is most useful when you’re setting up Git for the first time.

git help: Forgot a command? Type this into the command line to bring up the 21 most common git commands. You can also be more specific and type “git help init” or another term to figure out how to use and configure a specific git command.

Basic git commands(cont..)

git status: Check the status of your repository. See which files are inside it, which changes still need to be committed, and which branch of the repository you’re currently working on.

git add: This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.

git commit: Git’s most important command. After you make any sort of change, you input this in order to take a “snapshot” of the repository. Usually it goes git commit -a -m “Message here.” The -m indicates that the following section of the command should be read as a message.

git diff: See the changes you have made.

git mv, git rm

Basic git commands(cont..)

git log: Used to get a list of all commits done in this version of the repository.

git reset: Every commit is referred to by a unique commit ID. At any point of time, we may revert all our progress to a given commit using git reset --hard <commit_id>. The latest commit is called HEAD, and all changes after the latest commit will be deleted using git reset --hard HEAD.

git grep <some_regex>: Helps search the entire project for lines of text / code matching the regex provided.

git stash: helps store un-committed work temporarily which may be recovered using git stash pop.

.gitignore: Git never reads changes in these files and ensures they aren't pushed to any remote. Makes use of wildcards

Want a demo?

An excellent list of commands (multilingual :P ) is complied here https://github.com/arslanbilal/git-cheat-sheet

Moving Projects to Github!

  • What is GitHub?
  • hello world in GitHub
  • Features specific to GitHub

What is GitHub?

GitHub is a web-based Git repository hosting service, which offers source code management functionality of Git as well as adding its own features. Unlike Git, which is strictly a command-line tool, GitHub provides a web-based graphical interface. It also provides access control and several collaboration features such as wikis, task management, and bug tracking and feature requests for every project.

hello world in GitHub

1. Create an account in GitHub.

2. Create a repository. Preferrably, uncheck "Initialize with a README.md"

3. Browse around! There are a lot of interesting things happening on Github all the time! 

Official GitHub hello-world tutorial here

Features specific to GitHub

  • Issues - To keep track of  the bug.
  • Fork - Own a copy of else's project.

  • Pull request - Send in your contribution for review.

  • Other features - follow, star, watch, etc.

Pushing the Project!

​So we need to specify this Github project URL to our local folder. This is done via "remotes". Remotes are like public / private copies of the project, not on our machine.

git remote add <remote_name> <remote_url>

origin is the most common name, and the default remote of cloned repositories.

See all your remotes by using git remote -v

Push the commits by using git push <remote_name> <branch_name>, which translates to git push origin master in our case. (Branches explained later).

So putting it simply,

  • git add
  • git commit
  • git commit
  • ....
  • git push
  • git commit
  • git commit
  • ....
  • git push

Collaborating in Projects

  • Branches
  • Pulling / Merging
  • Resolving Merge Conflicts

Terminologies

Branch: How do multiple people work on a project at the same time without Git getting them confused? Usually, they “branch off” of the main project with their own versions full of changes they themselves have made. After they’re done, it’s time to “merge” that branch back with the “master,” the main directory of the project. master is the default branch name.

You can make a new branch using git checkout -b <branch_name>. 

You may use git checkout <branch> to switch to that branch.

git branch: Get a list of all your branches along with your active branch. git branch -D <branch_name> to force delete a branch.

git merge: Once we committed all work in <branch_name> we can head back to master and merge in our changes using git merge <branch_name>

Collaborating using Github

The first step involved in adding the person involved to the repository on Github. This gives him access to make changes in the repository.

  • git clone <remote_URL>: Used to pull an entire project as a folder locally. First step to a contribution! Saves a lot of work (git init, git remote add, git pull origin master)
  • git push: Once changes have been committed, they could be pushed to the remote as described earlier. However, now the chances of conflict arise. (You may use --force, to overwrite the remote. Beware!!)
  • git pull: Used to fetch all the latest changes in the remote to our local repository. git pull <remote_name> <branch_name>. 

So putting it simply,

  • Person A: git checkout -b pikachu
  • Person A: git add / commit ...
  • Person A: git checkout master
  • Person A: git merge pikachu
  • Person A: git push origin master
  • Person B: git pull origin master
  • Person B: (same as above)
  • Person B: git push origin master
  • Person A: git pull origin master
  • Person A: (same as above)

We wish we could all work together :(

Working Together!

Well you definitely can! Here is where the magic of "merging" comes into play! As long as you work on different files, or edit the different parts of the same file, git will bring it all together during the pull operation!

 

(Merging here works similar to the merging of branches)

Merge Conflicts

So here comes the tricky part, when two people decide to edit the same part of the same file at the same time.

There might arise the need to resolve conflicts manually.

Fix conflicts and run a git commit -a to end the merge.

Contributing to Open Source

  • Github Forks
  • Issues and Pull Request
  • Code Review, Changes
  • Rebase, Squashing
  • Patch Submission

Github Forks

  • So unlike your own repositories, or repos where you have been added as a collaborator, you CANNOT push directly to open source repositories. You may clone them though.
  • You must create a "Fork", or a new remote containing the open source project, owned by you.
  • Changes made to the "Fork" are not directly added to the original repository. It goes through Pull Requests and code reviews.

Issues and Pull Requests

  • Issues are a set of bugs / problems with the project that need to be rectified. Every open source project has it's own issue tracker and it need not implemented on Github.
  • Pull Requests: A request to add the changes you have made to an open source project. (Don't get confused with git pull). Pull Requests compare your code with the original code and are ideal for reviews.

Making a Pull Request

  • Start off by forking the project.
  • git clone the forked version of the project.
  • Add a new remote (preferrably name it upstream). This remote refers to the original project and is mainly used to pull changes made over time. (git remote add upstream <remote_url>)
  • Make a new branch, add commits and push the branch to your fork using git push origin <branch_name>
  • Make a Pull Request on Github

Code Review & Changes

  • Code added via Pull Requests generally go through very rigorous automated tests and manual code reviews. This is because the owners of the repository have to verify that the change actually does what it promises.
  • Requested changes (if any) are automatically added to the PR by making commits and pushing the same branch.

Rebasing

  • Rebasing: As you spend some time making changes to your PR, a lot of changes go on in the original repository. You can pull them using git pull --rebase upstream <branch_name>.
  • The --rebase flag adds all your commits on top of the latest commit to the repository. Rebasing could need solving of merge conflicts.
  • Push to your PR using git push origin <branch_name> --force.

Squashing

  • A lot of open source projects require you to combine all your changess into one neat commit with a decent description. This is done to keep the commit history clean and logical.
  • So start off by finding the commit ID just before your commits in git log
  • Run the command git rebase -i <commit_ID>
  • Leave the first commit as pick and replace all others with squash.
  • Save and close the file, enter a commit message.
  • Push to your PR using git push origin <branch_name> --force. The force flag is necessary here.

Patch Submission

  • Some open source organizations prefer using patches, files storing all your commits that you must upload somewhere.
  • Start off by making a new branch, creating changes in that branch and committing.
  • git format-patch master --stdout > patch_name.patch
  • To apply a patch, use git am --signoff < patch_name.patch This adds all the commits in the patch to the repository.

Surprise?

Let's do a LIVE contributon to Mozilla!

Hope you enjoyed!

"People who say alcohol is addictive, probably haven't tried their hands at FOSS." - Ranveer

Catch us at

Open Source, SymPy <3

  • Fork an interested repo.
  • Work on your fork.
  • Send in a PR, get it merged.

Ta-da you are an open-source contributor

Fundae for open source.

  • Pick the right organization. "Enthu" matters.
  • Thumb rule : Work on easy bugs first.
  • Don't be intimidated by the code base, of the hundreds of contributors, just a handful know the whole thing.
  • Don't be afraid to ask questions. No contributor is there to work. Everybody is there to learn, they will be more than willing to help.

SymPy

A symbolic computational library

What?

Mathematical library that handles symbols than numbers. So you can do things like

integrate(sin(x) + cos(x)**2, x)

and things much more complicated

A simple pythonic open source library alternative for Wolfram

import sympy

And as simple as

Some features!

Example: Laplace Transforms

>>> from sympy.integrals import laplace_transform
>>> from sympy.abc import t, s, a
>>> laplace_transform(t**a, t, s)
(s**(-a)*gamma(a + 1)/s, 0, -re(a) < 1)

Core capabilities|Polynomials |Calculus

         Solving equations |Combinatorics |Discrete math

          Matrices |Geometric Algebra| Geometry |Plotting |Physics 

Statistics| Cryptography| Parsing |Printing

Copy of Intro to git

By Kalpesh Krishna

Copy of Intro to git

Introduction to git by Web and Coding Club, IIT Bombay

  • 1,190
Loading comments...

More from Kalpesh Krishna