CSC/ECE 517 Fall 2014/ch1a F1415 rv: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 111: Line 111:




<div class="tab-pane" id="lC">
<div>
<h2>Commands</h2>
<h2>Commands</h2>
<p>For a list of command line commands, please go to <a href="http://gitref.org/">gitref.org</a></p>
<p>For a list of command line commands, please go to [Git-Reference]</p>
<h2>Definitions</h2>
<p>This is not a complete list, it ones that I think are important.</p>
<table class="table table-striped">
<table class="table table-striped">
<tbody>
<tbody>

Revision as of 00:42, 20 September 2014

Introduction

What is Git?

Git (/ɡɪt/) is a locally enabled distributed revision control and source code management (SCM) system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows. Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005, and has since become the most widely adopted version control system for software development. Github, a git repository web-based hosting service is in fact the largest code host on the planet with over 15.7 million repositories. Large or small, every repository comes with the same powerful tools. These tools are open to the community for public projects and secure for private projects. As with most other distributed revision control systems, and unlike most client–server systems, every Git working directory is a full-fledged repository with complete history and full version-tracking capabilities, independent of network access or a central server. Like the Linux kernel, Git is free software distributed under the terms of the GNU General Public License version 2.

Why Distributed Version Control System?

Distributed Revision Control Systems (DRCS) take a peer-to-peer approach, as opposed to the client-server approach of centralized systems. Rather than a single, central repository on which clients synchronize, each peer's working copy of the codebase is a bona-fide repository.Distributed revision control conducts synchronization by exchanging patches(change-sets) from peer to peer. This results in some important differences from a centralized system:

  • No canonical, reference copy of the codebase exists by default; only working copies.
  • Common operations (such as commits, viewing history, and reverting changes) are fast, because there is no need to communicate with a central server.
  • Rather, communication is only necessary when pushing or pulling changes to or from other peers.
  • Each working copy effectively functions as a remote backup of the codebase and of its change-history, providing inherent protection against data loss.

The act of cloning an entire repository gives distributed version control tools several advantages over centralized systems:

  • Performing actions other than pushing and pulling changesets is extremely fast because the tool only needs to access the hard drive, not a remote server.
  • Committing new changesets can be done locally without anyone else seeing them. Once you have a group of changesets ready, you can push all of them at once.
  • Everything but pushing and pulling can be done without an internet connection. So you can work on a plane, and you won’t be forced to commit several bugfixes as one big changeset.
  • Since each programmer has a full copy of the project repository, they can share changes with one or two other people at a time if they want to get some feedback before showing the changes to everyone.

Why Git?

Branching Model Git's most compelling feature that really makes it stand apart from nearly every other SCM out there is its branching model.Git will allow you to have multiple local branches that can be entirely independent of each other and the creation, merging and deletion of those lines of development take seconds. This means that you can do things like:

• Create experimental branches.
• Clear cut Branch delineation into production,Development,Testing,Fixes,Patches,etc.
• Seamless Navigation across feature branches and Code Versions.
• Create and Work on your own patches, features and tests without effecting the Production/Master branch.
• Independent Merging and Management of one's content changes.

Everything is Local There is very little outside of 'fetch', 'pull' and 'push' that communicates in any way with anything other than your hard disk.This not only makes most operations much faster than you may be used to, but it also allows you to work on stuff offline. That may not sound like a big deal, but I'm always amazed at how often I actually do work offline. Being able to branch, merge, commit and browse history of your project while on the plane or train is very productive. local repo to remote repo flowchart. Even in Mercurial, common commands like 'incoming' and 'outgoing' hit the server, whereas with Git you can 'fetch' all the servers data before going offline and do comparisons, merges and logs of data that is on the server but not in your local branches yet. This means that it's very easy to have copies of not only your branches, but also of everyone's branches that are working with you in your Git repository without having to mess your own stuff up. Git is Fast Git is fast. Everyone—even most of the hard core users of these other systems—generally give Git this title. With Git, all operations are performed locally giving it a bit of a leg up on SVN and Perforce, both of which require network access for certain operations. However, even compared to the other DSCMs that also perform operations locally, Git is pretty fast. Part of this is likely because it was built to work on the Linux kernel, which means that it has had to deal effectively with large repositories from day one. Additionally, Git is written in C, reducing the overhead of runtimes associated with higher-level languages. Another reason that Git is so fast is that the primary developers have made this a design goal of the application.

Git is Small Git is really good at conserving disk space. Your Git directory will (in general) barely be larger than an SVN checkout—in some cases actually smaller (apparently a lot can go in those .svn dirs). The following numbers were taken from clones of the Django [1]project in each of its semi-official Git mirrors at the same point in its history. Git Hg Bzr SVN Repo Alone 24M 34M 45M Entire Directory 43M 53M 64M 61M

The Staging Area Unlike the other systems, Git has what it calls the "staging area" or "index". This is an intermediate area that you can setup what you want your commit to look like before you commit it. The cool thing about the staging area, and what sets Git apart from all these other tools, is that you can easily stage some of your files as you finish them and then commit them without committing all the modified files in your working directory, or having to list them on the command line during the commit add commit workflow diagram This also allows you to stage only portions of a modified file. Gone are the days of making two logically unrelated modifications to a file before you realized that you forgot to commit one of them. Now you can just stage the change you need for the current commit and stage the other change for the next commit. This feature scales up to as many different changes to your file as you need. Of course, Git also makes it pretty easy to ignore this feature if you don't want that kind of control—just slap a '-a' to your commit command in order to add all changes to all files to the staging area. commit only workflow diagram

Distributed One of the coolest features of any of the Distributed SCMs, Git included, is that it's distributed. This means that instead of doing a "checkout" of the current tip of the source code, you do a "clone" of the entire repository. This means that even if you're using a centralized workflow, every user has what is essentially a full backup of the main server, each of which could be pushed up to replace the main server in the event of a crash or corruption. There is basically no single point of failure with Git unless there is only a single point. This does not slow things down much, either. On average, an SVN checkout is only marginally faster than any of the DSCMs. Of the DSCMs I tested, Git was the fastest. cloning benchmarks Git 1m 59s Hg 2m 24s Bzr 5m 11s SVN 1m 4s

Any Workflow One of the amazing things about Git is that because of its distributed nature and super branching system, you can easily implement pretty much any workflow you can think of relatively easily. Subversion-Style Workflow A very common Git workflow, especially from people transitioning from a centralized system, is a centralized workflow. Git will not allow you to push if someone has pushed since the last time you fetched, so a centralized model where all developers push to the same server works just fine. subversion-style workflow

Integration Manager Workflow

Another common Git workflow is where there is an integration manager—a single person who commits to the 'blessed' repository, and then a number of developers who clone from that repository, push to their own independent repositories and ask the integrator to pull in their changes. This is the type of development model you often see with open source or GitHub repositories. integration manager workflow Dictator and Lieutenants Workflow

For more massive projects, you can setup your developers similar to the way the Linux kernel is run, where people are in charge of a specific subsystem of the project ('lieutenants') and merge in all changes that have to do with that subsystem. Then another integrator (the 'dictator') can pull changes from only his/her lieutenants and push those to the 'blessed' repository that everyone then clones from again.

Git Work Flow Terminology

Git Data Flow

Understanding the Git and a typical Distributed VCS terminology :

A project is the files/source code a developer is currently writing. A git-project is a project that is using git to track/manage changes to the project. Remote repository A centralize Git server to host all your git-projects. A central repository (repo) is not required but it is the best way to distribute (share) your git-projects to different user/developers. Most would call the remote repository (repo) the Origin and keeping it up-to-date is highly recommended. In our case, GitHub would be our remote repo. They all work together and it all one thing.

Local repository A local repo is a git-project on your computer. If you were to have a Remote repository (GitHub), then the local repo would be a clone (exact copy) of the remote git-project. When you make changes to your local repo like adding code, you will need to update (push) the git-project to remote repo.

Index (cache) - Staging When you make changes to your files, you can not commit (snapshot in time) until you "stage" the files. Once you stage the file, you can then commit the changes.The purpose of this Index (staging area) is that it allows you to control what you want to commit. Typically,any code changes that are not complete can be left out of the commit.

Working directory A working directory is the current git-project, which is your source code with git files. A working directory is a local repo which you can make changes (commit, branches) to it.



Commands

For a list of command line commands, please go to [Git-Reference]

<tbody> <thead> </thead> <tbody> </tbody>
Term Explanation
branches It allows you to create a separate working copy of your code and develop it in a

new way wihtout affect the original code. So its like creating a new project (branch) based on the original, the original is usually called the Master. The branch is completely independent of the Master and you can develop both the Master and branch at the same time. You can also have multiple branches and you can merge any of these together.



Branching, cloning and forking is sometimes confused.
Good discussion on <a href="http://stackoverflow.com/questions/3329943/what-is-the-difference-between-branch-fork-and-clone-in-git">stackoverflow.com</a>

clone

Clone is a copy of a repository. Clone is used when you copy from the remote repository to your local repository.

Branching, cloning and forking is sometimes confused.
Good discussion on <a href="http://stackoverflow.com/questions/3329943/what-is-the-difference-between-branch-fork-and-clone-in-git">stackoverflow.com</a>

collaborator Collaborators are other GitHub users that you’ve given write access to one or more of your repositories.

Collaborators may fork any private repository you’ve added them to without their own paid plan. Their forks do not count against your private repository quota.
This was taken from github.com

commit A commit is like a bookmark/milestone that is recorded in the repository. It creates a revision that you can view and

revert back to at anytime. When using Git, you are encourge to do many small commits since all of these commits are done on your computer. When you update (push) to the remote repository to backup or share with other developers all the commits will be viewable.

In projects with multiple developers, doing many small commits and updating the remote repository many times a day will allow your coworkers to merge your changes into their code and thus resolving discrepancies early which is much easier to resolve than waiting and doing a massive merge.

A commit will always contain the author and committer. Author is usually the creator/owner

of the project and a committer can be a collaborator or the author.
fetch

Fetch is a command in Git or command, you use fetch to synchronize your local repository with the remote repository. Its important to know that fetch does not merge any new changes to your source code (local repository). New changes from the fetch will be considered a "remote branches", you can combine new changes to your code by using the merge command.



When will this happen?

  • When a collaborator/coworker ask you to test the changes with your code
  • Before you do a push (upload to remote repository), you would want to do a fetch, which will synchronize your local repository to the remote repository. So you are making sure your local repository is up-to-date with the remote repository and if new code is downloaded, then you can check out what is new. You will also need to do a merge.

Note: fetch is not a clone, clone is a new copy down from the repository, fetch is to update your local repository.

fork

Fork is NOT a Git term, its a term born from the community. Forking is similar to branching in that you copy the origin (Master) to your account. The difference is that you are copying the source code from the author (owner) to your account. Branching is copying within the same account.

So when does this happen? Forking usually happens for 2 reasons:

  1. You fork it to take the project in a new direction. Obviously you can only do this with open source projects
  2. You fork the project to add to the project and request a merge back to the origin/master. You as a independent developer want to contribute to the code.

Branching, cloning and forking is sometimes confused.
Good discussion on <a href="http://stackoverflow.com/questions/3329943/what-is-the-difference-between-branch-fork-and-clone-in-git">stackoverflow.com</a>

merge

A merge is combining branches with your Master (origin). When to you do pull a fetch and merge will be done to update your local repository. When merging two code sets together, if a conflict were to occur, it will be up to you to resolve it.

pull

A pull is basically 2 command execute together. Pull will do a fetch and merge on your local repository. When using the GitHub Windows Client, you will usually do a pull.



When will this happen?

  • When a collaborator/coworker ask you to test the change with your code
  • Before you do a push (upload to remote repository), you would want to do a pull, which will synchronize your local repository to the remote repository. So you are making sure your local repository is up-to-date with the remote repository and you can test you code with any new code committed to the remote repository.
push

Push is the command to upload your local repository to the remote repository. Any changes that you want to push to the remote repository, you will need to commit on those changes on your local repository.

repository Contains all the history... like commits, branches, tags and your source code. Repository can be remote which is a central

storage for repository. Local repository would be what the user/deverloper works with (make changes to).

Remote repository is a centralize repository for sharing projects.
revision

Revisions represents a version of the source code. Git identifies revisions with SHA1 ids. SHA1 ids are 160 bits long and are represented in hexadecimal. The latest version can be addressed via "HEAD", the version before that via "HEAD~1" and so on.
This was taken from <a href="http://www.vogella.com/articles/Git/article.html">vogella.com</a>



A revision is created when you do a commit. So a commit is a command and a revision is like the bookmark... so when you hear someone say "revert back to the last commit"... technically they should say, "revert back to the last revision".

tags A tag is like a commit, it allows you to permanantly mark a point in your project. A common use of tags is to mark the application version number. This version number is the same displayed in the help/about.
URL Used to tell Git where the remote repository is.


There is more than one type of URL

  • HTTP: Should be a https (secure) https://github.com/<user>/<git_project>
  • GIT: Uses git communication git://github.com/<user>/<git_project>
  • SSH: Uses SSH git@github.com:<user>/<git_project>

Characteristics of Git

Strong support for non-linear development
Git supports rapid and convenient branching and merging, and includes powerful tools for visualizing and navigating a non-linear development history.
Distributed development
Like most other modern version control systems, Git gives each developer a local copy of the entire development history, and changes are copied from one such repository to another. These changes are imported as additional development branches, and can be merged in the same way as a locally developed branch. Repositories can be easily accessed via the efficient Git protocol (optionally wrapped in ssh for authentication and security) or simply using HTTP - you can publish your repository anywhere without any special webserver configuration required.
Efficient handling of large projects
Git is very fast and scales well even when working with large projects and long histories. It is commonly an order of magnitude faster than most other version control systems, and several orders of magnitude faster on some operations. It also uses an extremely efficient packed format for long-term revision storage that currently tops any other open source version control system.
Cryptographic authentication of history
The Git history is stored in such a way that the name of a particular revision (a "commit" in Git terms) depends upon the complete development history leading up to that commit. Once it is published, it is not possible to change the old versions without it being noticed. Also, tags can be cryptographically signed.
Toolkit-based design
Following the Unix tradition, Git is a collection of many small tools written in C, and a number of scripts that provide convenient wrappers. Git provides tools for both convenient human usage and easy scripting to perform new clever operations.

How do I use Git?

Every commit that a user makes will have his/her name and email address to identify the ‘owner’ of the commit, so the user should give these values in a form of configuration.

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

To get started with Git, a user needs to run the git init command in an empty directory; this initializes a Git repository in that folder, adding a .git folder within it. A repository is kind of like a code history book. It will hold all the past versions of your code, as well as the current one.

git init

A project managed by Git can be considered as a tree; a user can create different features on different branches, and everything will stay separate and safe. After the user has created new files in the repository. It is time to commit these files to mark the completion of feature development and ready to push changes to remote repository. However, before that, the user needs to add the files and move it to staging area.

git add .

A user can also more specifically add files.

git add *.js
git add index.php

This will add all the files to staging area. To stage is simply to prepare it finely for a commit. Git, with its index allows you to commit only certain parts of the changes you've done since the last commit.

After a user has staged the required files, they need to commit the files by giving a commit description.

git commit -m "Commit description"

Each commit should have an accompanying message, so everyone in the project knows why that code was committed. A user can also skip the command to add the files in the staging area by directly asking Git to stage and commit with a message.

git commit -am "Commit description"

Git uses the code contents of the commit to create a 40 character SHA1 hash. The neat part about this is that, since it's using the code to create the hash, no two hashes in the user's project will be the same unless the code in the commits is identical. A user can check the status of the git repository by the following command.

git status

To push the changes to a remote git repository, a user can do a git push as following.

git push origin master