1 环境的构建:
使用 Mac 系统自带的 Git 进行版本管理存在, Git 是系统的 Xcode 集成的
查看版本的命令:
- $ git --version
- git version 2.14.3 (Apple Git-98)
查看 git 的安装目录:
- $ which git
- /usr/bin/git
2 常用命令
- # 使用 git 命令初始化文件夹, 即创建 git 本地仓库
- $ git init
- Initialized empty Git repository in /Users/Mac/Desktop/myapp/.git/
- #配置全局变量
- $ git config --global user.name "***"
- $ git config --global user.email ****@qq.com
- #将 index.jsp 添加到 git 仓库中
- $ git add index.jsp
- #获取 git 本地仓库状态
- $ git status
- #使用通配符添加文件到本地仓库, 此命令表示将需要添加的本地仓库的所有 html 文件添加到本地仓库
- $ git add *.html
- #此命令表示将需要添加的本地仓库的所有文件添加到本地仓库
- $ git add .
- #从 git 本地仓库中移除 index.jsp 文件
- $ git rm --cached index.jsp
- #将已添加到本地仓库的文件全部提交, 当输入此命令时, 会切换到一个编辑页面, 此时需输入此次提交的备注信息, 保存退出即可
- $ git commit
- #此命令中参数 - m 后添加备注信息
- $ git commit -m another change
- #创建. gitignore 文件, 该文件的作用是忽略文件中的指定文件, 即不用添加提交的文件
- $ touch .gitignore
- #演示创建分支的方法, 在分支下修改文件, 除非合并, 否则不会实质性修改主线的内容
- $ git branch login
- #切换到 master 角色下
- $ git checkout master
- #在 master 角色下合并分支命令
- $ git merge login
- #以下演示同步到 GitHub 上
- #查看是否已有远程连接
- $ git remote
- #创建远程连接命令,"***" 此内容需要在 GitHub 指定查看上复制
- $ git remote add origin git@github.com:****/***.git
- #将本地仓库中的文件推送到 GitHub 仓库上
- $ git push -u origin master
- #由于之前已建立连接, 故此时只需直接即可 push
- $ git push
- #从 GitHub 仓库中克隆项目到本地仓库
- $ git clone **@github.com:***/homework.git
3 示例
- Last login: Fri Mar 9 16:47:24 on ttys000
- #配置 git, 生成公钥密钥 (输完命令需要敲四次空格, 后三次为提示一行敲一次), 运行完之后会在~/.shh 文件内生成 id_rsa 和 id_rsa.pub 两个文件,
- #复制 id_rsa.pub 内容复制黏贴到 GitHub 的指定位置, 此操作用于 git 连接 github 网站, 获取读写权限
- Mac$ ssh-keygen -t rsa -b 4096 -C "1037345628@qq.com"
- Generating public/private rsa key pair.
- Enter file in which to save the key (/Users/Mac/.ssh/id_rsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /Users/Mac/.ssh/id_rsa.
- Your public key has been saved in /Users/Mac/.ssh/id_rsa.pub.
- --------------------------------------
- #使用 git 进行项目版本管理.
- #1. 创建 myapp, 并切换至该目录下
- :~ Mac$ cd /Users/Mac/Desktop/myapp
- #创建 index.jsp app.jsp 文件
- :myapp Mac$ touch index.jsp
- :myapp Mac$ touch app.jsp
- #使用 git 命令初始化文件夹, 即创建 git 本地仓库
- :myapp Mac$ git init
- Initialized empty Git repository in /Users/Mac/Desktop/myapp/.git/
- #配置全局变量
- :myapp Mac$ git config --global user.name "****"
- :myapp Mac$ git config --global user.email ****@qq.com
- #将 index.jsp 添加到 git 仓库中
- :myapp Mac$ git add index.jsp
- #获取 git 本地仓库状态
- :myapp Mac$ git status
- #表示正在以 master(项目创建者或主线管理) 角色操作
- On branch master
- #表示没有提交过
- No commits yet
- Changes to be committed:
- (use "git rm --cached <file>..." to unstage)
- #新文件, 表示需要提交的文件
- new file: index.jsp
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- app.jsp
- #从 git 本地仓库中移除 index.jsp 文件
- :myapp Mac$ git rm --cached index.jsp
- rm index.jsp
- #获取 git 本地仓库状态
- :myapp Mac$ git status
- On branch master
- No commits yet
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- #表示需要添加 git 仓库的文件
- app.jsp
- index.jsp
- #没有可提交的文件
- nothing added to commit but untracked files present (use "git add" to track)
- #创建 index.html 文件
- :myapp Mac$ touch index.html
- #查看 git 本地仓库状态
- :myapp Mac$ git status
- On branch master
- No commits yet
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- app.jsp
- index.html
- index.jsp
- nothing added to commit but untracked files present (use "git add" to track)
- #使用通配符添加文件到本地仓库, 此命令表示将需要添加的本地仓库的所有 html 文件添加到本地仓库
- :myapp Mac$ git add *.html
- :myapp Mac$ git status
- On branch master
- No commits yet
- Changes to be committed:
- (use "git rm --cached <file>..." to unstage)
- new file: index.html
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- app.jsp
- index.jsp
- #此命令表示将需要添加的本地仓库的所有文件添加到本地仓库
- :myapp Mac$ git add .
- :myapp Mac$ git status
- On branch master
- No commits yet
- Changes to be committed:
- (use "git rm --cached <file>..." to unstage)
- new file: app.jsp
- new file: index.html
- new file: index.jsp
- #修改 index.jsp 文件
- :myapp Mac$ git status
- On branch master
- No commits yet
- Changes to be committed:
- (use "git rm --cached <file>..." to unstage)
- new file: app.jsp
- new file: index.html
- new file: index.jsp
- Changes not staged for commit:
- (use "git add <file>..." to update what will be committed)
- (use "git checkout -- <file>..." to discard changes in working directory)
- #表示该文件已被修改过, 需要重新添加本地仓库
- modified: index.html
- :myapp Mac$ git add .
- :myapp Mac$ git status
- On branch master
- No commits yet
- Changes to be committed:
- (use "git rm --cached <file>..." to unstage)
- new file: app.jsp
- new file: index.html
- new file: index.jsp
- #将已添加到本地仓库的文件全部提交, 当输入此命令时, 会切换到一个编辑页面, 此时需输入此次提交的备注信息, 保存退出即可
- :myapp Mac$ git commit
- [master (root-commit) f81a0ad] first commit;
- 3 files changed, 10 insertions(+)
- create mode 100644 app.jsp
- create mode 100644 index.html
- create mode 100644 index.jsp
- :myapp Mac$ git status
- On branch master
- #表示没有需要提交的文件
- nothing to commit, working tree clean
- #修改 app.jsp 文件, 获取本地仓库状态
- :myapp Mac$ git status
- On branch master
- Changes not staged for commit:
- (use "git add <file>..." to update what will be committed)
- (use "git checkout -- <file>..." to discard changes in working directory)
- #表示需要添加的文件
- modified: app.jsp
- #表没有已经改变的已添加文件
- no changes added to commit (use "git add" and/or "git commit -a")
- :myapp Mac$ git add .
- :myapp Mac$ git status
- On branch master
- Changes to be committed:
- (use "git reset HEAD <file>..." to unstage)
- #需要提交的已经添加到本地仓库的修改文件
- modified: app.jsp
- #提交, 输入备注信息
- :myapp Mac$ git commit
- #change is app.jsp 添加备注信息
- [master 24e3cd2] changed is app.jsp
- 1 file changed, 1 insertion(+)
- :myapp Mac$ git commit
- On branch master
- nothing to commit, working tree clean
- #创建 log.txt 文件
- :myapp Mac$ git status
- On branch master
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- log.txt
- nothing added to commit but untracked files present (use "git add" to track)
- #创建. gitignore 文件, 该文件的作用是忽略文件中的指定文件, 即不用添加提交的文件
- :myapp Mac$ touch .gitignore
- #在. gitignore 文件输入 log.txt
- :myapp Mac$ git status
- On branch master
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- #需要添加文件, 已忽略 log.txt 文件
- .gitignore
- nothing added to commit but untracked files present (use "git add" to track)
- :myapp Mac$ git add .
- :myapp Mac$ git status
- On branch master
- Changes to be committed:
- (use "git reset HEAD <file>..." to unstage)
- new file: .gitignore
- #创建文件夹 dir1,dir2
- :myapp Mac$ git status
- On branch master
- Changes to be committed:
- (use "git reset HEAD <file>..." to unstage)
- new file: .gitignore
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- dir1/
- dir2/
- #在. gitignore 中添加 "/dir1" 内容
- :myapp Mac$ git status
- On branch master
- Changes to be committed:
- (use "git reset HEAD <file>..." to unstage)
- new file: .gitignore
- Changes not staged for commit:
- (use "git add <file>..." to update what will be committed)
- (use "git checkout -- <file>..." to discard changes in working directory)
- modified: .gitignore
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- dir2/
- :myapp Mac$ git add .
- #此命令中参数 - m 后添加备注信息
- :myapp Mac$ git commit -m another change
- [master 50d5a2f] another change
- 2 files changed, 3 insertions(+)
- create mode 100644 .gitignore
- create mode 100644 dir2/app2.js
- #演示创建分支的方法, 在分支下修改文件, 除非合并, 否则不会实质性修改主线的内容
- #创建 login 分支命令
- :myapp Mac$ git branch login
- :myapp Mac$ git status
- On branch master
- nothing to commit, working tree clean
- #切换到 login 分支下
- :myapp Mac$ git checkout login
- Switched to branch login
- #在分支下创建文件
- :myapp Mac$ touch login.html
- :myapp Mac$ git status
- On branch login
- Changes not staged for commit:
- (use "git add <file>..." to update what will be committed)
- (use "git checkout -- <file>..." to discard changes in working directory)
- modified: dir2/app2.js
- modified: index.html
- Untracked files:
- (use "git add <file>..." to include in what will be committed)
- login.html
- no changes added to commit (use "git add" and/or "git commit -a")
- :myapp Mac$ git commit -m login form
- On branch login
- Changes not staged for commit:
- modified: dir2/app2.js
- modified: index.html
- Untracked files:
- login.html
- no changes added to commit
- :myapp Mac$ git add .
- :myapp Mac$ git commit -m login form
- [login 57202e2] login form
- 3 files changed, 12 insertions(+), 1 deletion(-)
- create mode 100644 login.html
- :myapp Mac$ git status
- On branch login
- nothing to commit, working tree clean
- #切换到 master 角色下
- :myapp Mac$ git checkout master
- Switched to branch master
- #在 master 角色下合并分支命令
- :myapp Mac$ git merge login
- Updating 50d5a2f..57202e2
- Fast-forward
- dir2/app2.js | 2 +-
- index.html | 1 +
- login.html | 10 ++++++++++
- 3 files changed, 12 insertions(+), 1 deletion(-)
- create mode 100644 login.html
- #以下演示同步到 GitHub 上
- #查看是否已有远程连接
- :myapp Mac$ git remote
- #创建远程连接命令,"git@github.com:****/homework.git" 此内容需要在 GitHub 指定查看上复制
- :myapp Mac$ git remote add origin git@github.com:*****/homework.git
- #查看是否已有远程连接
- :myapp Mac$ git remote
- origin
- :myapp Mac$ git push -u origin master
- git@github.com: Permission denied (publickey).
- fatal: Could not read from remote repository.
- Please make sure you have the correct access rights
- and the repository exists.
- :myapp Mac$ git remote add origin https://github.com/*****/homework.git
- fatal: remote origin already exists.
- #将本地仓库中的文件推送到 GitHub 仓库上
- WGB:myapp Mac$ git push -u origin master
- Counting objects: 18, done.
- Delta compression using up to 4 threads.
- Compressing objects: 100% (11/11), done.
- Writing objects: 100% (18/18), 1.46 KiB | 213.00 KiB/s, done.
- Total 18 (delta 3), reused 0 (delta 0)
- remote: Resolving deltas: 100% (3/3), done.
- To github.com:gdwkong/homework.git
- * [new branch] master -> master
- Branch master set up to track remote branch master from origin.
- #切换到 master 角色下
- :myapp Mac$ git checkout master
- A README.md
- Already on master
- Your branch is up-to-date with origin/master.
- #创建文件 README.md 文件
- :myapp Mac$ touch README.md
- :myapp Mac$ git add .
- :myapp Mac$ git status
- On branch master
- Your branch is up-to-date with origin/master.
- Changes to be committed:
- (use "git reset HEAD <file>..." to unstage)
- new file: README.md
- :myapp Mac$ git commit -m README.md
- [master 2dcd73c] README.md
- 1 file changed, 1 insertion(+)
- create mode 100644 README.md
- #由于之前已建立连接, 故此时只需直接即可 push
- WGB:myapp Mac$ git push
- Counting objects: 3, done.
- Delta compression using up to 4 threads.
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
- Total 3 (delta 1), reused 0 (delta 0)
- remote: Resolving deltas: 100% (1/1), completed with 1 local object.
- To github.com:gdwkong/homework.git
- 57202e2..2dcd73c master -> master
- #从 GitHub 仓库中克隆项目到本地仓库
- :myapp Mac$ cd /Users/Mac/Desktop/myapp2
- :myapp2 Mac$ ls
- :myapp2 Mac$ git clone git@github.com:***/homework.git
- Cloning into homework...
- remote: Counting objects: 21, done.
- remote: Compressing objects: 100% (9/9), done.
- Receiving objects: 100% (21/21), done.
- Resolving deltas: 100% (4/4), done.
- remote: Total 21 (delta 4), reused 21 (delta 4), pack-reused 0
- $
4 提交后 GitHub 中相应的仓库中的内容
来源: http://www.bubuko.com/infodetail-2521985.html