What is Git? The 5 Minute Tutorial.

It’s been about one week since I started the iOS immersive class at The Flatiron School in NYC. To say I’ve learned a lot in 7 days is an understatement. The content, instructors, and classmates are all top-notch and I’m so grateful that I have this opportunity. One of the first topics we tackled was Git and GitHub.

Any programming knowledge I had prior to Flatiron was self-taught and there were plenty of holes in the shaky house of knowledge that I had built. This became apparent whenever my Google results presented StackOverflow answers referring to ideas and concepts that were completely foreign to me. Git was one of these!

What is Git?

Git is not GitHub. That’s first, but we’ll get back to this.

Let me give you the official definition of Git:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Now…let me give you my definition:

Git is a crazy piece of software that allows you to save and work with your files in a way that allows you to go back in time when you break your program.

This will make more sense as I walk through how you can use it in your project. Tutorial time!

Before we begin, install Git from https://git-scm.com/ Also, I’ll be using a Mac for this tutorial. ALSO?—?my screen shots might look different than yours because I’ve added some customizations to my Terminal. It’s all good.

Ok, let’s start!

1] Create a folder on your Desktop called “TestProject”. Open Terminal and navigate to this folder! Type in the following and then press Enter:

cd /Users/YOURUSERNAME/Desktop/TestProject

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2Nkbi1pbWFnZXMtMS5tZWRpdW0uY29tL21heC84MDAvMSowS1NoNGlKWnh2UllqdTZRb0ZsSXB3LnBuZw==.jpg">

“cd” means “Change Directory”

2] Now that you’re in the folder, enter the following into Terminal and press Enter:

git init 

This creates a hidden folder inside your project folder that you don’t have to worry about. This hidden folder contains the secret sauce. It will actually keep track of all the changes you make to your project, and allow you to go back in time to different versions of your project.

3] We have an empty project. So let’s add a new file!

touch newFile.txt

Touch creates a new, blank file (If the touch command doesn’t work for you, simply create a new file using any text editor and save it as newFile.txt into our TestProject folder). Hooray?—?you’ve successfully made a change to your project. And guess what? Git is aware of this. And to prove it, enter this into your Terminal and press enter:

git status

You should see something like this:

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2Nkbi1pbWFnZXMtMS5tZWRpdW0uY29tL21heC84MDAvMSowRHQxdXNUNl8wQjEyV3VrYVZSelJ3LnBuZw==.jpg">

See that? Git knows that you made a change to your project and it shows that in red. Good stuff!

Now let’s tell Git to remember this version of your project, so if you ever want to go back to this point in time in your project’s life, you can!

4] The way you tell Git to remember this version of your project, is a two step process. First, you must “add” the files. Then you “commit” these added files. For the sake of this tutorial, just know that you have to do these two steps and in that order to tell Git, “Hey?—?remember this version of my project for me!” This is how we will do it:

git add .

The “.” means “all the files in my project’s folder”. This is convenient when you’ve made changes to a bunch of different files. You could also do:

git add newFile.txt

Now, the next and last, important step is to commit these added files!

git commit -am ‘Add a new text file‘

This attaches a “message” to your commit and the text in the quotes following it, is your message?—?for you?—?to remind you of what you did.

Why is the mssage in present tense and more info on making good commits can be seen here: https://github.com/erlang/otp/wiki/Writing-good-commit-messages

Congrats! You made a commit! What does that mean? It means you can basically time travel.

Note: when you “add” files, you are adding them to a “staging area”. This staging area is where your files sit until you commit them. If you change your files after you add them but before you commit them, then you must re-add them for the commit to work correctly!

5] Let’s make a change to your project. Open up newFile.txt and add some text to it and save. Ok, go do it.

Did you change up your text file? Good. Now back in your Terminal, type:

git status

You should see….

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2Nkbi1pbWFnZXMtMS5tZWRpdW0uY29tL21heC84MDAvMSpUNjdKMC1OWE9POWhNMXpCNWFoRXR3LnBuZw==.jpg">

Git is so smart! See that red text? It knows that you made changes to your newFile.txt. So, not only does it know when you add or delete files, but it knows if you add or delete what’s inside those files! This is why you should add/commit all the time, because if you end up making changes to some files that breaks your program?—?you can always just go back in time to a previous commit when your project was actually working!

Ok, now that we’ve modified our newFile.txt, let’s commit it! But don’t forget?—?first add it!

git add .
git commit -m “Modified text file”

6] Let’s go back in time.

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2Nkbi1pbWFnZXMtMS5tZWRpdW0uY29tL21heC84MDAvMSp5S29mSl9pZDROWjU1ejJQRDBRMWd3LmpwZWc=.jpg">

Type this into Terminal and press Enter:

git log

You should see something like this:

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="//pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2Nkbi1pbWFnZXMtMS5tZWRpdW0uY29tL21heC84MDAvMSpNTHZsajY4eUlkSXNiZFlyaUN1Z2R3LnBuZw==.jpg">

That’s your timeline (history) full of commits! Only two at the moment but you can imagine what it might look like after some time. Your “commit number” is the funky combo of numbers and letters on the left. It actually isn’t called a “commit number”, it’s called a hash. In order to go back to a different version of your project, just put this in Terminal and press Enter:

git checkout YOURCOMMITNUMBER

In this case, my hash was de31fbe.

Now open up your newFile.txt

See that magic? It’s blank! Your text is gone because your project was reverted to the first commit, and we had nothing in newFile.txt when we made our first commit.

Want to go back to the future? By that I mean, go back to a time when you had text in your newFile.txt file.

Easy enough! Just do the same command as before except use the other hash. In my case it would look like this…

git checkout cfe715c

Now try that and then open up your newFile.txt. Like magic, your content is back!

You’ve now scratched the surface of how Git works. Start using it right away on any project. It’s pretty amazing.

Next up…how to use Git with GitHub.

时间: 2024-10-14 04:25:55

What is Git? The 5 Minute Tutorial.的相关文章

How to Manage your GIT Repository Tutorial

GIT Repository Management This tutorial explains how to use GIT to create a project, add files, commit modifications and upload them in the remote repository at GitHub. First, you should generate an SSH key. You must have at least one SSH public key

Git简易教程

概述 在说明什么是git之前,我们需要对版本控制(Version Control)做一个基本的概述,一般情况下,我们的源代码都是在时间和空间两个维度上进行管理并维护的,代码本身 以及组织代码的项目文件(如makefile或者vs的项目文件)都是以文件和目录的形式存储在磁盘空间上的,这种文件管理的形式已经被大家所熟悉了,然而,我们在不同时间段里做出的各种修改怎么管理呢?版本控制系统 (Version Control System,以下简称VCS)就是一种记录代码修改演化的系统,他的功能就是方便你今

git版本库

分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库. Git的功能特性: 从一般开发者的角度来看,git有以下功能: 1.从服务器上克隆完整的Git仓库(包括代码和版本信息)到单机上. 2.在自己的机器上根据不同的开发目的,创建分支,修改代码. 3.在单机上自己创建的分支上提交代码. 4.在单机上合并分支. 5.把服务器上最新版的代码fetch下来,然后跟自己的主分支合并. 6.生成补丁(patch),把补丁发送给

A great tutorial with Jupyter notebook for ML beginners

An end to end implementation of a Machine Learning pipeline SPANDAN MADAN Visual Computing Group, Harvard University Computer Science and Artificial Intelligence Laboratory, MIT Link to Github Repo Section 1. Introduction Background In the fall of 20

Thrift数据类型

一.基本数据类型: 1.bool:布尔类型值,只有true和false. 2.byte:8位带符号整数. 3.i16:16位带符号整数. 4.i32:32位带符号整数. 5.i64:64位带符号整数. 6.double:64位浮点数. 7.string:使用utf-8编码的字符串类型. 二.特殊数据类型: 1.binary:一个没有编码的二进制序列,其实就是一个byte数组. 三.结构数据: 1.自定义类型,例如以下代码定义了一个Person的类型: struct Person { 1: i32

boost操作xml 5分钟官方教程

Five Minute Tutorial This tutorial uses XML. Note that the library is not specifically bound to XML, and any other supported format (such as INI or JSON) could be used instead. XML was chosen because the author thinks that wide range of people is fam

计算机类免费电子书共享

列表最早来自stackoverflow上的一个问题:List of freely available programming books 现在在github上进行维护:free-programming-books List of Free Programming Books This list initially was a clone of stackoverflow - List of freely available programming books by George Stocker.

OOP已死,AOP为未来而生(.net+java)

OOP已死,AOP为未来而生 未来用于构建复杂的基于服务的应用将是面向方面编程AOP(Aspect-Oriented Programming),而面向对象编程OOP将成为辅助. ?控制反转(IOC)模式.这个通用模式描述为支持插件架构,其中的对象可以"查询",他们需要的其他对象的实例方法.?依赖注入(DI)模式.这是IoC模式一种特殊情况,是基于改变类行为的接口编程技术,而不改变类的内部.开发人员针对为类的接口编码,并使用一个容器来注入依赖对象实例到基于接口或对象类型的类.该技术注入对

Tutorial for adding a library project as git submodule and then using it as a studio Module

https://gitsubmoduleasandroidtudiomodule.blogspot.in/I'm writing this blog to make the development process easy and faster. This is specific to Android Studio, same can be used for any other project which used Gradle build system and GIT. I want to m