Thanks to visit codestin.com
Credit goes to github.com

Skip to content

morris87813/git-workshop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

Git Workshop

Table of Contents

Housekeeping

  • Workshopt repo
  • ~ 1 hour
  • Ask questions. Don’t worry about interrupting the workshop.
  • I’ll also remind myself to stop and ask if you have any question.
  • Be sure to have access to git.

Introduction

Why version control systems?

For the peace of mind.

Why 2-stage commit?

Definitions:

  • Workspace - all unstaged changes.
  • Staging area (a.k.a. index) - a “holding area” for changes that will be committed when you next do git commit.

Further Readings:

git init / git clone

Getting and Creating Projects.

  • init: Create an empty Git repository or reinitialize an existing one.
  • clone: Clone a (remote) repository into a new directory

Exercise: Initialize a git repository in a directory

$ mkdir new-repo
$ cd new-repo
$ git init

Exercise: Clone this repository into your file system

# Go to a different working directory assuming you are still in the new-repo
# directory.
$ cd ..
# if you have ssh setup already, do:
$ git clone [email protected]:dawranliou/git-workshop.git
# otherwise, do:
$ git clone https://github.com/dawranliou/git-workshop.git

Question: What do you think initializing the git repo did to your working directory?

git status

Show the working tree status.

Git status gives you a lot of information about the current state of your git repo. When I screwed up a git repo, I usually could deduce what went wrong based on the git status. You should be doing git status a lot too to get familiar with git.

Exercise:: Check the status of your repo

$ cd git-workshop
$ git status

Question:: What do you see in the git status?

git add

Add file contents to the index.

This is the first stage of the 2-stage commit. We can have multiple dirty files, i.e. files with untracked changes, but only select a couple to be committed.

Exercise: Add a new file into the staging area.

# Create a new empty file
$ touch excercise.txt
$ git add excercise.txt
# Or add everything to the staging area
$ git add .

Question: git status?

$ git status

git commit

Record changes to the repository.

A commit is a single unit in git’s version control system. Every single commit is assigned with a SHA code to preserve its integrity. Traversing the commit history and operating on the commit history is simple and fast in git. You are encouraged to make small, incremental commits rather than a huge one. You are also encouraged to make imperfect commits and clean up the history later.

Exercise: Commit the staging area

$ git commit

# Or attach the commit message in one go
$ git commit -m "Add file - excercise.txt"

Question: git status?

$ git status

Exercise: Add another commit in master

$ echo "# excercise.txt - a file for excercise" >> excercise.txt
$ git add excercise.txt
$ git commit "Add file comment"

git branch

List, create, or delete branches.

Branch is one of the most liberating ability given by a version control system. You are free to track multiple states of your source code at any given time without worrying about screwing it up.

In practice, I rarely use the branch command to create a new branch because of a shortcut we’ll see in the next section.

Question: Can you find an example where branching is used without a version control system?

git checkout

Switch branches or restore working tree files

Exercise: Checkout a new branch

$ git checkout -b develop

Question: git status?

$ git status

Exercise: Add more commits in develop branch

$ echo "This is line one" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the first line"

$ echo "This is line two" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the second line"

$ echo "This is line three" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the third line"

Question: git status?

$ git status

Exercise: Switch back to master branch and add commits

$ git checkout master

$ echo "This is line 0" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the zeroth line"

$ echo "This is line 1" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the first line"

$ echo "This is line 2" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the second line"

git merge

Join two or more development histories together

When we have two versions of our source code, we can then merge them together. A practical case would be merging a feature branch into the master/main branch after the feature branch is fully tested. Imaging we currently have this:

      A---B---C develop
     /
D---E---F---G master

By merging the develop to master, our commit history looks like:

      A---B---C develop
     /         \
D---E---F---G---H master

Exercise: Merge the develop branch into the master

Question: git status?

# Merge command merges the "other" branch into the "current" branch. Therefore,
# we need to switch back to the master branch first.
$ git checkout master
# Merge in the develop brach
$ git merge develop

# ...and a merge conflict!

Question: What does it mean to have conflicts?

Exercise: Resolve the conflict

# Open your text editor and fix the conflicts

$ git merge --continue
# Or abort it
# $ git merge --abort

git rebase

Reapply commits on top of another base tip.

So far, we haven’t encounter any command that is destructive, which isn’t the case for rebase. rebase rewrites the git history and it is irreversible, so use it cautiously. Although you can choose to abort the rebase operation anytime, it’s safest to first create a temporary branch to track the existing state before rebase. However destructive, it is useful to keep the git history clean while collaborating with others. Let’s think of the feature branch again:

      A---B---C feature
     /
D---E---F---G master

When we rebase the feature branch on our master, we rewrite the history on feature to:

              A'--B'--C' feature
             /
D---E---F---G master

The feature’s commits, A, B, and C, becomes A', B', and C'.

Exercise: Rebase the feature on the master

git push

Update remote refs along with associated objects.

Bonus

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published