Work Locally with Git from the Command Line (Activity)

(Difference between revisions)
Jump to: navigation, search
(Created page with "__NOTOC__ {| border="1" |- |'''Title''' || Git Activity 2 |- |'''Overview''' || Students install and use git. |- |'''Prerequisite Knowledge''' || * Bash skills: mani...")
 
(reformatted directions)
Line 30: Line 30:
 
=== Background: ===
 
=== Background: ===
  
=== Directions: ===
 
  
  
==== Setup ====
+
=== Directions: ===
 
+
==== Git ====
 
+
===== Git =====
+
  
 
Download and install git for your operating system:
 
Download and install git for your operating system:
Line 43: Line 40:
 
* Mac OSX or Linux: http://git-scm.com/
 
* Mac OSX or Linux: http://git-scm.com/
  
===== SSH =====
+
==== SSH ====
  
 
You might want to setup an SSH key if you plan to work with remote repositories
 
You might want to setup an SSH key if you plan to work with remote repositories
 
a lot. See https://help.github.com/articles/generating-ssh-keys
 
a lot. See https://help.github.com/articles/generating-ssh-keys
  
 
+
==== Help ====
 
+
===== Help =====
+
  
 
Open a terminal (git-bash on Windows) and run the following commands.
 
Open a terminal (git-bash on Windows) and run the following commands.
  
<nowiki>
+
  git help
    git help
+
  git help -ag
    git help -ag
+
  git help init
    git help init
+
</nowiki>
+
  
 
# What does `git help` do?
 
# What does `git help` do?
Line 67: Line 60:
 
activity.
 
activity.
  
 
+
==== Identify yourself ====
 
+
===== Identify yourself =====
+
  
 
Run the following commands, replacing BOGUS NAME and BOGUS@EMAIL with your name
 
Run the following commands, replacing BOGUS NAME and BOGUS@EMAIL with your name
 
and email.
 
and email.
  
<nowiki>
+
  git config --global user.name 'BOGUS NAME'
    git config --global user.name 'BOGUS NAME'
+
  git config --global user.email 'BOGUS@EMAIL'
    git config --global user.email 'BOGUS@EMAIL'
+
</nowiki>
+
  
 
# What are these commands doing?
 
# What are these commands doing?
 
# What is the purpose of `--global`?
 
# What is the purpose of `--global`?
  
 +
==== Create repository ====
  
===== Create repository =====
+
  mkdir project
 
+
  cd project
<nowiki>
+
  git init
    mkdir project
+
    cd project
+
    git init
+
</nowiki>
+
 
+
  
 
# What was created by `git init`?
 
# What was created by `git init`?
Line 97: Line 82:
 
# You find an old project on your hard drive. You do not remember if it is a under version control by git. How can you find out?
 
# You find an old project on your hard drive. You do not remember if it is a under version control by git. How can you find out?
  
 
+
==== Basic commands ====
 
+
===== Basic commands =====
+
  
 
Use a plain text editor to create `names.txt` inside the `project` folder. Put the names of your team in the file. Save and exit.
 
Use a plain text editor to create `names.txt` inside the `project` folder. Put the names of your team in the file. Save and exit.
Line 105: Line 88:
 
Run `git status` before and after each of these commands.
 
Run `git status` before and after each of these commands.
  
<nowiki>
+
  git add names.txt
    git add names.txt
+
  git commit -m'Add our names.'
    git commit -m'Add our names.'
+
  git log
    git log
+
</nowiki>
+
 
+
  
 
# What kind of information does `git status` report?
 
# What kind of information does `git status` report?
Line 125: Line 105:
 
Run `git status` before and after each of these commands.
 
Run `git status` before and after each of these commands.
  
<nowiki>
+
  git add .
    git add .
+
  git commit  # You are in vim; write a multi-line commit message, save and quit.
    git commit  # You are in vim; write a multi-line commit message, save and quit.
+
  git log
    git log
+
</nowiki>
+
  
 
# What does `git add .` do? What do you think `.` means?
 
# What does `git add .` do? What do you think `.` means?
Line 135: Line 113:
 
# If you want to write a more detailed commit message (which is good practice) what command would you use?
 
# If you want to write a more detailed commit message (which is good practice) what command would you use?
  
 
+
==== Stage/Cache/Index ====
 
+
===== Stage/Cache/Index =====
+
  
 
Do the following:
 
Do the following:
Line 147: Line 123:
 
Run the following commands:
 
Run the following commands:
  
<nowiki>
+
  git add names.txt
    git add names.txt
+
  git status
    git status
+
</nowiki>
+
  
 
# Below write each file name under the state that its changes are currently in. Compose a definition for each state.
 
# Below write each file name under the state that its changes are currently in. Compose a definition for each state.
Line 162: Line 136:
 
Run the following commands:
 
Run the following commands:
  
<nowiki>
+
  git diff
    git diff
+
  git diff --cached
    git diff --cached
+
</nowiki>
+
  
 
# What does `git diff` display?
 
# What does `git diff` display?
Line 178: Line 150:
  
  
===== Undo =====
+
==== Undo ====
  
<nowrap>
+
  git log
    git log
+
  git status
    git status
+
  git reset --soft HEAD^
    git reset --soft HEAD^
+
  git log
    git log
+
  git status
    git status
+
</nowrap>
+
  
 
# What does `git reset --soft HEAD^` do?
 
# What does `git reset --soft HEAD^` do?
  
   <nowrap>
+
   git commit -m'Redo'
    git commit -m'Redo'
+
  git log
    git log
+
  git status
    git status
+
  git reset --hard HEAD^
    git reset --hard HEAD^
+
  git log
    git log
+
  git status
    git status
+
</nowrap>
+
 
+
  
 
# What does `git reset --hard HEAD^` do?
 
# What does `git reset --hard HEAD^` do?
Line 204: Line 171:
 
# What do you think `HEAD^` means?
 
# What do you think `HEAD^` means?
 
# What do you think `HEAD` means?
 
# What do you think `HEAD` means?
 +
  
  

Revision as of 21:04, 16 July 2015

Title Git Activity 2
Overview Students install and use git.
Prerequisite Knowledge
  • Bash skills: manipulate and navigate a filesystem.
  • Vim skills: edit and exit (with or without changes).
Learning Objectives Able to ...
  • Download git.
  • Install git.
  • Configure git.
  • Configure SSH.
  • Make and commit changes to repository.
  • Add new files to repository.
  • Remove file from repository.
  • Modify a file and commit the change.
  • Stage changes for commit.
  • Un-stage changes for commit.
  • Explain the purpose of the stage/cache/index.
  • Inspect the state of a repository.
  • Undo a commit.

Background:

Directions:

Git

Download and install git for your operating system:

SSH

You might want to setup an SSH key if you plan to work with remote repositories a lot. See https://help.github.com/articles/generating-ssh-keys

Help

Open a terminal (git-bash on Windows) and run the following commands.

 git help
 git help -ag
 git help init
  1. What does `git help` do?
  2. What does `-ag` cause `git help` to do?
  3. What does `git help command` do?

You may use `git help` at any time to help you answer the questions in this activity.

Identify yourself

Run the following commands, replacing BOGUS NAME and BOGUS@EMAIL with your name and email.

 git config --global user.name 'BOGUS NAME'
 git config --global user.email 'BOGUS@EMAIL'
  1. What are these commands doing?
  2. What is the purpose of `--global`?

Create repository

 mkdir project
 cd project
 git init
  1. What was created by `git init`?
  2. Any file that starts with `.` is hidden in Linux. How do you display a hidden file in Linux?
  3. What would happen if you delete `.git`?
  4. You find an old project on your hard drive. You do not remember if it is a under version control by git. How can you find out?

Basic commands

Use a plain text editor to create `names.txt` inside the `project` folder. Put the names of your team in the file. Save and exit.

Run `git status` before and after each of these commands.

 git add names.txt
 git commit -m'Add our names.'
 git log
  1. What kind of information does `git status` report?
  2. What does `git add names.txt` do?
  3. What does `git commit -m'Add our names.'` do?
  4. What does `git log do`?


Use a plain text editor to create the following files:

  • `birthdays.txt` - Put your birthdays in this file.
  • `movies.txt` - Put the last movie each of you watched.

Run `git status` before and after each of these commands.

 git add .
 git commit  # You are in vim; write a multi-line commit message, save and quit.
 git log
  1. What does `git add .` do? What do you think `.` means?
  2. What does `git commit` (without -m) do?
  3. If you want to write a more detailed commit message (which is good practice) what command would you use?

Stage/Cache/Index

Do the following:

  • Modify `names.txt` so that names are listed in _Last, First_ format, one per line.
  • Modify `movies.txt` so they are in reverse alphabetical order by title.
  • Create a new file `foods.txt` that contains your favorite foods (one for each team member).

Run the following commands:

 git add names.txt
 git status
  1. Below write each file name under the state that its changes are currently in. Compose a definition for each state.
    • Staged
    • Unstaged
    • Untracked
  2. If you run `git commit` what changes will be committed (___don't do it___)?
  3. What command do you run to stage changes?
  4. What command do you run to unstage changes?

Run the following commands:

 git diff
 git diff --cached
  1. What does `git diff` display?
  2. What does `git diff --cached` display?
  3. Formulate a sequence of commands to unstage changes to `names.txt`, and stage the changes to `movies.txt`. Execute your commands and confirm they worked.
  4. Edit `movies.txt`, change any one of the movies, and save it. Then run `git status`. What do you observe? Explain what you think is going on.
  5. Delete `names.txt`. Then run `git status`. What do you observe? Explain what you think is going on.
  6. Rename `movies.txt` to `last-movies`. Run `git status`. Observe and explain.
  7. Formulate a sequence of commands to stage all changes including the untracked file and commit (with any reasonable message you like). Execute them.
  8. In git vernacular, `index`, `cache`, and `stage` all refer to the same thing. What does it hold?
  9. Why have a `stage`? Why not just commit all changes since the last commit?


Undo

 git log
 git status
 git reset --soft HEAD^
 git log
 git status
  1. What does `git reset --soft HEAD^` do?
 git commit -m'Redo'
 git log
 git status
 git reset --hard HEAD^
 git log
 git status
  1. What does `git reset --hard HEAD^` do?
  2. What is the difference between `--hard` and `--soft`?
  3. What do you think `HEAD^` means?
  4. What do you think `HEAD` means?


Deliverables:

Assessment:

Comments:

Additional Information:

Knowledge Area/Knowledge Unit
Topic Git
Level of Difficulty Beginner
Estimated Time to Completion 50 min
Materials/Environment
Author Stoney Jackson
Source
License CC BY-SA 4.0


Suggestions for the Open Source Project:


This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

CC license.png

Personal tools
Namespaces
Variants
Actions
Events
Learning Resources
HFOSS Projects
Evaluation
Navigation
Toolbox