GitHub使用笔记2:github常用操作

时间:2022-07-26 23:32:02

1: 绑定ssh keys

GitHub使用笔记2:github常用操作

2:github新建仓库

GitHub使用笔记2:github常用操作

echo "# SpringStack" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin git@github.com:xidianzxm/SpringStack.git

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git;

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

git push -u origin master

把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。

由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

MacBookPro:SpringStack zhangxm$ **git init**
Reinitialized existing Git repository in /data/github/SpringStack/.git/
MacBookPro:SpringStack zhangxm$ **git add README.md**
MacBookPro:SpringStack zhangxm$ **git commit -m "first commit"**
[master (root-commit) 2c1be6c] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
MacBookPro:SpringStack zhangxm$ **git remote add origin git@github.com:xidianzxm/SpringStack.git**
MacBookPro:SpringStack zhangxm$ **git push -u origin master**
Counting objects: 3, done.
Writing objects: 100% (3/3), 229 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:xidianzxm/SpringStack.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
MacBookPro:SpringStack zhangxm$
MacBookPro:SpringStack zhangxm$ **git status**
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed) .idea/
SpringStack.iml
pom.xml
src/ nothing added to commit but untracked files present (use "git add" to track) MacBookPro:SpringStack zhangxm$ **git add pom.xml **
MacBookPro:SpringStack zhangxm$ **git add src**
MacBookPro:SpringStack zhangxm$ **git commit -m "Commit pom.xml and src file folder"**
[master a93e5eb] Commit pom.xml and src file folder
3 files changed, 142 insertions(+)
create mode 100644 pom.xml
create mode 100644 src/main/java/com/test/App.java
create mode 100644 src/test/java/com/test/AppTest.java
MacBookPro:SpringStack zhangxm$ **git push -u origin master**
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (14/14), 1.77 KiB | 0 bytes/s, done.
Total 14 (delta 0), reused 0 (delta 0)
To github.com:xidianzxm/SpringStack.git
2c1be6c..a93e5eb master -> master
Branch master set up to track remote branch master from origin.
MacBookPro:SpringStack zhangxm$

3:github 管理修改

MacBookPro:SpringStack zhangxm$ 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) renamed: src/main/java/com/test/App.java -> src/main/java/com/test/helloworld/App.java
renamed: src/test/java/com/test/AppTest.java -> src/test/java/com/test/helloworld/AppTest.java 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: pom.xml
modified: src/main/java/com/test/helloworld/App.java
modified: src/test/java/com/test/helloworld/AppTest.java Untracked files:
(use "git add <file>..." to include in what will be committed) .idea/
SpringStack.iml
src/main/java/com/test/helloworld/HelloName.java
src/main/java/com/test/helloworld/Main.java
src/resources/
src/test/java/com/test/helloworld/HelloNameTest.java
target/ MacBookPro:SpringStack zhangxm$
MacBookPro:SpringStack zhangxm$ git add pom.xml
MacBookPro:SpringStack zhangxm$ git commit -m "First modify pom.xml do git commit"
[master cf0b54a] First modify pom.xml do git commit
3 files changed, 7 insertions(+)
rename src/main/java/com/test/{ => helloworld}/App.java (100%)
rename src/test/java/com/test/{ => helloworld}/AppTest.java (100%) MacBookPro:SpringStack zhangxm$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '0.0.15.44' to the list of known hosts.
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (12/12), 789 bytes | 0 bytes/s, done.
Total 12 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:xidianzxm/SpringStack.git
a93e5eb..cf0b54a master -> master
Branch master set up to track remote branch master from origin.

那怎么提交第二次修改呢?

你可以继续git add再git commit,也可以别着急提交第一次修改,先git add第二次修改,再git commit,就相当于把两次修改合并后一块提交了:

第一次修改 -> git add -> 第二次修改 -> git add -> git commit

现在,你又理解了Git是如何跟踪修改的,每次修改,如果不add到暂存区,那就不会加入到commit中

4:查看工作区与版本库的变化

MacBookPro:SpringStack zhangxm$ git diff HEAD -- src/main/java/com/test/helloworld/App.java
diff --git a/src/main/java/com/test/helloworld/App.java b/src/main/java/com/test/helloworld/App.java
index 7c4ff03..57c24c2 100644
--- a/src/main/java/com/test/helloworld/App.java
+++ b/src/main/java/com/test/helloworld/App.java
@@ -1,4 +1,4 @@
-package com.test;
+package com.test.helloworld; /**
* Hello world!
MacBookPro:SpringStack zhangxm$

5:继续更改提交其它文件

MacBookPro:SpringStack zhangxm$ git add src/test/java/com/test/helloworld/AppTest.java
MacBookPro:SpringStack zhangxm$ 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) modified: src/main/java/com/test/helloworld/App.java
modified: src/test/java/com/test/helloworld/AppTest.java Untracked files:
(use "git add <file>..." to include in what will be committed) .idea/
SpringStack.iml
src/main/java/com/test/helloworld/HelloName.java
src/main/java/com/test/helloworld/Main.java
src/resources/
src/test/java/com/test/helloworld/HelloNameTest.java
target/ MacBookPro:SpringStack zhangxm$ git add src/main/java/com/test/helloworld/App.java MacBookPro:SpringStack zhangxm$ git add src/test/java/com/test/helloworld/AppTest.java MacBookPro:SpringStack zhangxm$ git commit -m "Modify App.java and AppTest.java"
[master 3cfa22f] Modify App.java and AppTest.java
2 files changed, 2 insertions(+), 2 deletions(-)
MacBookPro:SpringStack zhangxm$ git push -u orgin master
fatal: 'orgin' does not appear to be a git repository
fatal: Could not read from remote repository. Please make sure you have the correct access rights
and the repository exists.
MacBookPro:SpringStack zhangxm$ git push -u origin master
Counting objects: 15, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (15/15), 883 bytes | 0 bytes/s, done.
Total 15 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:xidianzxm/SpringStack.git
cf0b54a..3cfa22f master -> master
Branch master set up to track remote branch master from origin.
MacBookPro:SpringStack zhangxm$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed) .idea/
SpringStack.iml
src/main/java/com/test/helloworld/HelloName.java
src/main/java/com/test/helloworld/Main.java
src/resources/
src/test/java/com/test/helloworld/HelloNameTest.java
target/ nothing added to commit but untracked files present (use "git add" to track)