Github 基本操作

时间:2023-03-08 18:08:47

.配置Git

首先在本地创建ssh key;
$ ssh-keygen -t rsa -C "your_email@youremail.com"

后面的your_email@youremail.com改为你的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。

回到github,进入Account Settings,左边选择SSH Keys,Add SSH Key,title随便填,粘贴key。为了验证是否成功,在git bash下输入:

$ ssh -T git@github.com

如果是第一次的会提示是否continue,输入yes就会看到:You’ve successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。

接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们。

$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"

-------------

接下来在本地新建一个文件。放在你想要放的目录下面

$ mkdir TestProject

进入到刚创建的目录下面

cd TestProject/

新浪一个测试文件。等会把这个测试文件传到github 上面去

$ vi test.txt

初始化这个文件

$ git init

查看状态

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt

输入如下命令

$ git add test.txt

再次查看状态的时候发现跟上面的有所不同

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test.txt

提交

$ git commit -m "add a new file"
[master (root-commit) 45e61bc] add a new file
1 file changed, 4 insertions(+)
create mode 100644 test.txt

OK本地仓库已建好。接下来登录github 在上面新建一个仓库,建好后就把刚才本地的文件push 上去。

选择右上角的create a new repo

建好后上面会有提示命令

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:qingjoin/TestProject.git
git push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:qingjoin/TestProject.git
git push -u origin master
回到本地Terminal

输入命令

$ git remote add origin git@github.com:qingjoin/TestProject.git

再查看一下

$ git remote -v
origin git@github.com:qingjoin/TestProject.git (fetch)
origin git@github.com:qingjoin/TestProject.git (push)

最后push

$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 246 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:qingjoin/TestProject.git
* [new branch] master -> master

然后回到github看下。刚才就已经上传了一个文件。

通过git diff命令,用户可以查看更改。通过改变一个文件的内容,看看gitdiff命令输出什么,然后提交这个更改到仓库中

# Make some changes to the file
echo "This is a change" > test01
echo "and this is another change" > test02 # Check the changes via the diff command
git diff # Commit the changes, -a will commit changes for modified files
# but will not add automatically new files
git commit -a -m "These are new changes"
查看更新内容 git diff

提交更新  git commit -a -m "These are new changes"

上传 git push origin master