Git Demo

Git 由 Linus Torvalds 为管理 Linux 内核源代码而设计。Git 作为分布式的版本控制系统,是国内外互联网开发团队现今所使用的主要版本控制方式。

  • Installation Git For Windows
  • Installation Text Editor
  • The Git Commands
  • Use Git with Github
  • Create a New Repository Locally
  • Add Git to an Existing Project

Installation Git For Windows

1. Download

Open Git for windows, click download button show in your new page,choose the right version of package to download.
For example: I choose Git-2.16.3-64-bit.exe to download for my Win10 64bit System.

2. Install

Blow are some recommend choices for installing Git for windows. (Feel free to make other choice for yourself.)

Double click the package you have download last step
Next
Next
Use Visual Studio Code as Git’s defult editor
Use Git from the Windows Command Prompt
Use the OpenSSL library
Checkout as-is,commit Unix-style line endings
Use MinTTY(the defult MSYS2)
Next

3. Verify

open Git Bash from start menue
type git version after $, then press enter key.
If you Git Bash screen show like blow:

1
2
$ git version
git version 2.16.3.windows.1

You have installed it right.

Installation Text Editor

Here are several widely used text editors,choose one you prefer to download.

The Git Commands

Basic Concepts

In Git Bash,here are some basic concepts:

~ = Home Directory
pwd = Present Working Directory
/c/ = C:\

1
2
3
$ pwd
$ ls
$ ls -l

The command-line above:
ls :used to list the files and folders in current directory.
ls -l :used to list the files and folders in current directory with even more details.

Change Directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pwd
ls -l
cd videos/
pwd
cd ..
pwd
cd My\ Documents/
pwd
cd ../../..
ls -l
cd ~
cd ../..
cd
cd /c/Windows/System32/
pwd
cd

The command-line above all need to run after ~ in Git Bash.
cd Videos/: change directory to ~Videos.
cd ..: back up one directory level.
cd My\ Documents/: In Git Bash, you have to specify a backslash to escape the space charactor.
cd ../../..: back up three directory level.
cd ~: quickly back to user’s home directory.
cd: the same as cd ~
cd /c/Windows/System32/: change directory to System32 folder.

Where is that command?

1
2
which ls
which man

which ls: tells us the location of the ls command.

Echo…Echo

1
2
3
echo "Hello World"
echo $PATH
echo "The current path is $PATH"

echo "Hello World": echo out string “Hello World”.
echo $PATH: output the value of PATH variable.
echo "The current path is $PATH": variable interpolation, combine above two step.

Reviewing Files Contents

Click the link test.txt and you will open it in the new page -> right-click on the page or use the shortcut key ctrl + s -> save it as “test.txt”.

1
2
cat test.txt
less test.txt

cat test.txt: review the file contents in a raw way.

less test.txt: review the file contents in a samrt way, input q then enter key to quit the review.

Create, Rename, and Delete Files

1
2
3
4
5
6
7
8
9
10
11
pwd
ls
touch demo.txt # create a file
ls
mv demo.txt demo-1.txt # quote to quote, change the name of File from nameA to nameB.
ls
rm demo-1.txt # remove the file
ls
ls -l
touch test1.txt
ls -l

Create and Delete Folders

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pwd
mkdir projects # make directory
cd projects/
pwd
cd ..
rmdir projects # remove directory
cd projects/
ls
mkdir -p projects/client-a/awesome-web-project
cd projects/client-a/awesome-web-project
cd
rmdir projects # failed to remove 'projects': Directory not empty
rm -rf projects/ # same as -> rm -r -f projects/
ls

mkdir -p projects/client-a/awesome-web-project: -p: if necessary, create parents directory.
rm -rf projects/: -r: recursive, -f: force.

Clear and Exit

1
2
3
ls -l
clear # clear the output from the screen
exit # exit the terminal

Output to a File

Send the output of a command into a file:

  • >>: either create a new file or append to an existing file.
  • >: override the file, replace the contents entirely.
1
2
3
4
5
6
7
8
9
10
11
pwd
echo "hello there" >> demo.txt # create demo.txt or append to it
ls
cat demo.txt
echo "hello again" >> demo.txt # append "hello again" to it
cat demo.txt
echo "HI" > demo.txt # override the file
cat demo.txt
clear
ls -al > listing.txt # -a, contain hidden files and folders
cat listing.txt

Creating and Executing a Simple Bash Script

1
2
3
4
5
6
7
pwd
which bash # see where bash is installed
notepad++ example.sh # create example.sh
ls -al
chmod +x example.sh # add the executable flag to a file
#example only, not needed this time
./example.sh # execute example.sh

Customizing the Bash Environment

1
2
3
4
5
6
7
notepad++ .bashrc   # rc: Resource configuration; 
source .bashrc # load the file in gitbash
ll # = ls -al
npp # = notepad++
# Close Git Bash
ll
npp

set up aliases, wirte the flowing code in .bashrc above

1
2
alias ll='ls -al'
alias npp='netopad++'

Use Git with Github

Key Concepts

  • Repository contains files, history, config managed by Git.
  • Three States of Git:
    • Working directory
    • Staging area - pre-commit holding area
    • Commit - Git Repository(history)
  • Remote repository(Github)
  • Master branch

git status: monitor changes between four status.
Staging area: Git index, a holding area for queuing up changes for the next commit, files haven’t been commited.

Setup The Project Folder

1
mkdir projects

Git Configuration

Two bits of information:the name, email address.

1
2
3
4
git version
git config --global user.name "Your Name"
git config --global user.email "user@gmail.com"
git config --global --list

Copy the Repository (clone)

1
2
3
4
5
6
# paste in your GitHub HTTPS clone URL
git clone https://github.com/prezlincoln/github-demo.git
ls
cd github-demo
ls
git status # show the state of the repository

The First Commit

1
2
3
4
5
6
7
8
echo "Test Git Quick Start demo" >> start.txt
ls
cat start.txt
git status # untracked file: a file haven't added to Git
git add start.txt # in the staging area awating the commit
git status
git commit -m "Adding start text file" # move to local directory
git status

Publishing Changes to GitHub (push)

1
git push origin master

Create a New Repository Locally

1
2
3
4
5
6
7
8
9
10
cd projects
git init fresh-project ## init the project
cd fresh-project

npp "hipster.txt"
git status
git add hipster.txt
git status
git commit -m "Adding new file with hipster"
git status

remove the local repository:

1
2
3
cd ~/projects
ls
rm -rf fresh-project

Add Git to an Existing Project

Click the link: initializr-verekia-4.0.zip, cilck the download button in the new page to download it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pwd
cd projects
unzip ~/Downloads/initializr-verekia-4.0.zip
ls
mv initializr web-pro1
cd web-pro1
ls

git init
ls -al
git status
git all . # add all these file at once to Git's staging area
# and to the git index
git status
git commit -m "My first commit, inline"

if .git is no longer there,this project would no longer be managed by Git.

1
2
3
ls -al
rm -rf .git # this project would no longer be managed by Git
ls -al
-------------文章结束啦 ฅ●ω●ฅ 感谢您的阅读-------------