GithubHelp home page GithubHelp logo

git's Introduction

General Assembly Logo

Git Basics

Objectives

  • Initialize a git repository in order to track changes.
  • Create a new branch to isolate your changes.
  • Place new or changed files into the staging area to prepare them for a commit.
  • Remove files from the staging area before a commit.
  • Commit new and changed files to a git repository.

Prerequisites

Why Git

Version control! As developers our code is our livelihood so it's important that we safely store our work... frequently. Not only that we want to track our changes as we make them. If we make a feature that ends up breaking the rest of our app we want to be able to go back to a point when our app was last working.

Code Along: Making a Local Repository

Let's initalize a local repository.

  1. In your training directory create a subdirectory called game-of-gits.

  2. Inside of the game-of-gits directory create a file called sad-tale.md.

  3. Opening the file with Atom copy in the following lines:

House Stark of Winterfell is led by the just Eddard "Ned" Stark, Lord of
Winterfell, Warden of the North, Hand of the King, Protector of the Realm,
Regent.  He is surely honorable and will lead a long and prosperous life.
  1. Save the file.

  2. Inside of the game-of-gits directory type git status. Did anything happen?

  3. Again, inside the game-of-gits directory type git init.

  4. Type git status again. Did anything happen this time?

Code Along: Staging and Commiting

Using git add <"name_of_file"> we are going to add our story to the staging area.

There are 3 states that your file can reside in committed, modified and staged. These states map to the different sections of a Git project.

  • Modified means that you have changed the file but have not committed it to your database yet.
  • Staged means that you have marked a modified file in its current version to go into your next commit snapshot.
  • Committed means that the data is safely stored in your local database.

Git Basics

Git Sections

When we add a file we are moving it from the working directory to the staging area.

Now that our file is staged let's commit our file by typing git commit, Atom should open. Do Not Use git commit -m <message>

When you use -m to create an inline commit you are doing yourself and others a disservice. Your commit will be inherently poor due to the short nature of inline commits, and the lack of a body description to it. This is surely a sign of a poor developer and one that does not respect his or her teammate's time.

Lab: Crafting A Commit

Read over the following blog posts and carefully think about what a good commit message would be. Take some time to come up with your own. Be ready to share your commit with the rest of the class.

Now that we've made our first commit, let's see what happens when we type git log... We see our previous commit! This typically shows all of our previous commits, but since we just have one, that's all we see. Feel free to play around with options for git log, like --oneline, --name-status, and --graph for example. For all options click here.

Staging: And He Lived Happily After

Together, let's continue our story.

In our sad-tale.md, we'll tell the rest of Ned Stark's story. Paste this in below our current description and save:

Ned Stark went to King's landing where he made lots of friends and lived
happily ever after...  He definitely didn't get axe murdered.

Now using what we learned earlier stage this change. To figure out the status of your files you can type git status in the terminal at any time.

Remember: Staging isn't commiting

Unstaging: Maybe We Jumped the Gun

It turns out Ned actually did get axe murdered. So we probably want to unstage our file.

Unstage the file with git reset <"filename">

Delete the last thing we wrote in sad-tale.md.

We know that Ned's story doesn't have a happy ending but let's dream big. We're going to create a dream-story branch and write what we would have wanted to happen.

Removing: Now we need to remove files previously added

  1. Inside of game-of-gits create a file called the-stark-bunch.md.

  2. Type This is a story... of a man named Neddy... and three very badass really awesome girls.

  3. Save the file.

  4. git add the-stark-bunch.md.

  5. git rm --cache the-stark-bunch.md.

  6. git add the-stark-bunch.md.

  7. git rm -f the-stark-bunch.md.

What's the difference? What is actually happening with the rm command?

Branching: Multiple Stories, One Main Plot

Similar to having one main story and various sub-plots--a branch lets us effectively duplicate and section off the code we have writte thus far, make alterations to it, and if we would like at some point we can join it back to the main branch (typically called master).

Create a branch called dream-story by typing git branch dream-story. You can see all your current branches at any time by tying git branch.

Now that we've created our branch--in order to use it we have to switch to it. We can do this with the command git checkout <"branch_name">.

Lab: Branching Your Dreams

1.Switch to your dream-story branch and write a brief description of what you would have wanted to happen to Ned.

2.Save the file, Stage and commit your changes.

3.Switch back to your master branch. (Notice anything?) Add what really happened to Ned.

4.Stage and commit your changes.

(Be ready to talk about any issues you many have encountered or strange things you may have noticed).

Code Along: Learn Git Branching

Now let's take 10-15 minutes and go through Learn Git Branching together.

Git Workflow Checklist

  • git status to confirm clean working directory
  • confirm branch is correct
  • make changes to file
  • git add 'file'
  • git status (to confirm modified files have been staged)
  • git commit
  • git push origin <branchname>

Git Best Practices

  • NEVER use git add .
  • ALWAYS add files explicitly. If you have multiple files, use full paths to refer to each. Example: git add foo/bar.md baz/qux.js
  • NEVER use git commit -m "an example commit message"
  • ALWAYS use git status before any other command
  • NO commit is too small
  • NO commit message is too long
  • NEVER nest repositories

References

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

git's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

git's Issues

Should `git commit -m` be taught or be framed less negatively?

See #23
@nayana487

I was kind of put off by the section that said never user git commit -m. That "This is surely a sign of a poor developer and one that does not respect his or her teammate's time." This is how my instructors taught me to write commit messages and what they and pretty much every other developer I know uses in their work flow. When I delivered the lesson, I told the students that what was written in the lesson plan is the opinion of the instructor who wrote the lesson. I personally don't agree with it and this is why. It's ok and encouraged to have differing opinions as developers, but we should be able to intelligently articulate why we prefer one way over another. I did let Matt know ahead of time that I'd be saying this. That said, I don't think such strongly worded opinions should be written into a lesson plan in this way. I think --generally speaking--it's better to present different options and let students figure out what works for them. At least until the shops they end up at tell them otherwise.

@raq929

I agree the wording should be changed. Calling people bad developers just isn't helpful. I think the reasoning here (and @gaand might have more to say) is that at this level, developers aren't yet good at writing commit messages. Forcing them to open an editor and write a two line commit message makes them think more critically about what they're doing and produces commit messages that are more helpful to them and to us. (Personally, I only use -m when I'm making a fixup commit, for similar reasons.)

Should we explain version control more in the README?

See #23

@nayana487

The lesson plan started off with a section called why git? -- version control. Version control is dev speak and not something that we should expect new students to know. When I delivered, I asked them to think about working on project in a word doc--something most would be familiar with. How did you previously save changes? If someone else was working with you on the doc, how did you collaborate? What would you do if you wanted multiple versions of the project? This set them up to understand why something like git/github would be useful. Perhaps you all do something similar when you actually deliver the lesson, but for someone who was just seeing the lesson plan and doesn't have a sense of the Boston lesson flow, it was kind of confusing.

@raq929

I really like your first point, and it seems like you related it to something they would understand really well. I think we do need to introduce the term version control, because they'll run into it in interviews - but the way you did it sounds great.

Don't use `git add .`

Explain to developers why it's bad practice and tell the never to use this command. Repeat as necessary.

If/when that sinks in, tell them about the one exception (template/generator initial commit).

Developers need a checklist

The lab for this lesson revealed a number of issues.

Students need a checklist for a basic workflow. Perhaps use the checkbox feature of GitHub and really emphasize the typical flow:

  • make changes
  • git add filename
  • git commit filename

Additional checklists, for removals and moves, are a good idea.

Add a step that they must not be in the `git` repository before creating the folders

The developers fork and clone the repository to their training folder. Many of them instinctively then go into the folder. We then have them create a folder called game-of-gits but we do not highlight in the README that they need to not be in the git repository that they just cloned.

I would recommend clarifying just to minimize any chance of error.

- In your training directory create a subdirectory called game-of-gits.
+ In your training directory, NOT the git directory you just cloned, create a subdirectory called game-of-gits. 

https://github.com/ga-wdi-boston/git#code-along-making-a-local-repository

PVD Delivery

This was the first lesson I taught and I was unsure about how much I should deviate from the set curriculum, if I could make changes to the repo so I mostly delivered as is. However, there were a couple points mentioned in the lesson that I contradicted in my delivery and some points I added. I also have some thoughts on how this lesson should be restructured.

  • The lesson plan started off with a section called why git? -- version control. Version control is dev speak and not something that we should expect new students to know. When I delivered, I asked them to think about working on project in a word doc--something most would be familiar with. How did you previously save changes? If someone else was working with you on the doc, how did you collaborate? What would you do if you wanted multiple versions of the project? This set them up to understand why something like git/github would be useful. Perhaps you all do something similar when you actually deliver the lesson, but for someone who was just seeing the lesson plan and doesn't have a sense of the Boston lesson flow, it was kind of confusing.

  • I was kind of put off by the section that said never user git commit -m. That "This is surely a sign of a poor developer and one that does not respect his or her teammate's time." This is how my instructors taught me to write commit messages and what they and pretty much every other developer I know uses in their work flow. When I delivered the lesson, I told the students that what was written in the lesson plan is the opinion of the instructor who wrote the lesson. I personally don't agree with it and this is why. It's ok and encouraged to have differing opinions as developers, but we should be able to intelligently articulate why we prefer one way over another. I did let Matt know ahead of time that I'd be saying this. That said, I don't think such strongly worded opinions should be written into a lesson plan in this way. I think --generally speaking--it's better to present different options and let students figure out what works for them. At least until the shops they end up at tell them otherwise.

  • I actually wouldn't have gotten into branching at all in a first time exposure to git lesson. Git is a pretty hard concept for complete newbies to grasp in general, and implementing branching proved very confusing to the students. If I were to deliver this again, I would talk about what branching is at a high level and why it's useful, but I wouldn't teach the implementation just yet. I think having them know about git add, git status, git commit and git log at this point is sufficient. It's what we actually need them to be doing for their homework. They can know how to look at a solution branch on an exercise in Github, but they're probably not going to be creating their own branches for a while. In DC, we do teach branching in week one, but as a separate lesson after git intro and github are introduced. Going into branching at this stage confused the students a lot and I think ultimately made it challenging the rest of the week because they were unsure about how to properly use git and github when submitting homework.

  • The Learn Git Branching tutorial was also super confusing for students. The first section introduction to git commits was good, but having students try to grasp detached heads and rebasing was a bit much. I personally don't mess around with rebasing too much, so I don't think students should try to either. Again, this was another thing that detracted from the fundamentals they need to understand.

Change formatting

The following section isn't code or a direct instruction. I don't think it should be formatted like this

Committed means that the data is safely stored in your local database. Modified
means that you have changed the file but have not committed it to your database
yet. Staged means that you have marked a modified file in its current version
to go into your next commit snapshot.

Just thought of a potentially helpful analogy

I didn't realize I used this until now.
"Think about git history like Hansel and Gretle's breadcrumbs."
IF they had just left a sandwich in the middle of the woods, it wouldn't have been very helpful at all. However, the trail of breadcrumbs allows someone else to figure out exactly what their trail was, and saved them from getting horribly murdered by that crazy old woman.

What is a repository?

Just to note a common confusion about what a repository is.

A student asked if it was ok if they "just pushed a folder". This indicates a good understanding of the purpose of git, but not how it works.

Should `Learn Git Branching` tutorial be used?

See #23
@nayana487

The Learn Git Branching tutorial was also super confusing for students. The first section introduction to git commits was good, but having students try to grasp detached heads and rebasing was a bit much. I personally don't mess around with rebasing too much, so I don't think students should try to either. Again, this was another thing that detracted from the fundamentals they need to understand.

@raq929

I agree that the git branching tutorial can be a bit confusing. It does tend to work well for visual learners and not as well for others. I'm certainly open to considering different material

Difficulties with `git mv`

The lab for this lesson revealed a number of issues.

For moves, we should definitely remind them to use git mv instead of mv. We know it doesn't matter, but it's a source of confusion when students use mv and see that a file has been added or deleted in the staging area.

Checklist:

  • git mv filename newname
  • git commit
  • Write message noting the moved file

A similar checklist for removals might be necessary.

Branching your dreams lab

The value of this lab as I see it, is to show Atom changing in accordance with their branch changes.

The lab should just be switching between the two branches and observing Atom.

Steps 3 on have the developers staging and committing to master. This should be removed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.