记录 github 操作:命令行创建仓库、过滤上传文件、删除仓库目录

记录 github 操作:命令行创建仓库、过滤上传文件、删除仓库目录


1. 如何使用命令行创建github新仓库(new repository)

链接:Start a new git repository

创建一个新仓库的基本步骤

  1. 在github(browser)上创建一个仓库
  2. 进入这个仓库
  3. git init
  4. 写一些代码
  5. git add xxx.cpp 添加代码
  6. git commit (recoard change记录改变的代码)

git过滤器 .gitignore

  1. 进入包含项目的目录
  2. git init
  3. git add 所有相关文件
  4. 创建一个.gitignore文件,用来指明所有不想被追踪的文件
  5. git add .gitignore
  6. git commit

用 bash 连接到 github

当在本地创建了一个 git repository(仓库)后,如果想要将其放在 github 上,所需要做的步骤:

  1. 打开并登录 github(app 或 browser)
  2. 点击 new repository 按钮(右上),可以一并创建 README.md
  3. 点击 Create repository 按钮

接下来,在 bash 中关联刚才在 github 上创建的仓库

# ssh 方式关联
$ git remote add origin git@github.com:username/new_repo
$ git push -u origin master

或者是

# http 方式关联
$ git remote add origin https://github.com/username/new_repo

使用 ssh 方式关联 GitHub,在提交代码时不需要每次都输入密码;使用 http 方式关联 GitHub 时,需要每次都输入密码。


2. Setting up a new Git Repo

链接:Setting up a new Git Repo

使用命令行创建一个新仓库

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:账户名称/仓库名称.git
git push -u origin master

子命令解释

# push
git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
           [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
           [-u | --set-upstream] [-o <string> | --push-option=<string>]
           [--[no-]signed|--signed=(true|false|if-asked)]
           [--force-with-lease[=<refname>[:<expect>]]]
           [--no-verify] [<repository> [<refspec>…​]]
-u
--set-upstream
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).

# commit
git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
           [--dry-run] [(-c | -C | --fixup | --squash) <commit>]
           [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
           [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
           [--date=<date>] [--cleanup=<mode>] [--[no-]status]
           [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
           [-S[<keyid>]] [--] [<pathspec>…​]
           
           
           
-m <msg>
--message=<msg>
Use the given <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

The -m option is mutually exclusive with -c, -C, and -F.

使用命令行提交现有的仓库

git remote add origin git@github.com:账号名称/仓库名称.git
git push -u origin master
上面方法会遇到的问题
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
解决办法

You would need to create the repo on GitHub before pushing to it.
You may create a repository on GitHub either from an internet browser or using the GitHub command line API as follows:
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
Credits to bennedich

需要在推送代码之前创建这个仓库
创建仓库的两种方法

  1. 在 GitHub 网络浏览器上直接创建仓库
  2. 使用 GitHub 命令行 API 创建仓库
# 我使用的命令
$ curl -u 'codeBeefFly' https://api.github.com/user/repos -d '{"name":"bxg_cpp"}'
解决办法(更新 20200924)(更新20201020)*

前面的解决办法依然出现了问题,这个是新的解决办法,参考截图
在这里插入图片描述
这个是实际操作命令截图
在这里插入图片描述
命令结构如下,需要输入密码

# 使用 CLI 创建新的 github 远程仓库
curl -u [用户账户名称]:[用户账户密码] https://api.github.com/user/repos -d '{"name":"创建的仓库名称"}'

# 更新 20201020
在上传代码之前,可能?需要关联新创建的远程仓库
git remote add origin git@github.com:code*****ly/$repo_name$.git

# 上传代码到新的仓库
git push -u origin master

3. Git 过滤文件,控制上传

链接: Git 过滤文件,控制上传

操作方法:

针对单一工程排除文件, 在工程根目录下建立.gitignore文件,将要排除的文件或目录 写到.gitignore这个文件中,有两种写入方法。

  1. 使用命令行增加排除文件
  2. 用记事本打开,增加需要排除的文件或目录,一行增加一个
    *.class
    *.apk
    bin/
    gen/
    .settings/
    proguard/
    build/

4. 删除github中某个文件夹

链接: 删除github中某个文件夹

操作方法:

在github上只能删除仓库,却无法删除文件夹或文件, 所以只能通过命令来解决

$ git --help                      # 帮助命令
$ git pull origin master          # 将远程仓库里面的项目拉下来
$ dir                             # 查看有哪些文件夹
$ git rm -r --cached target       # 删除target文件夹
$ git commit -m '删除了target'     # 提交,添加操作说明
$ git push -u origin master       # 将本次更改更新到github项目上去

子命令解释

git-rm - Remove files from the working tree and from the index

git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​

-r
Allow recursive removal when a leading directory name is given.

参考

  1. 链接:Start a new git repository
  2. 链接:Setting up a new Git Repo
  3. 链接:Git 过滤文件,控制上传
  4. 链接:删除github中某个文件夹
  5. 链接:git 总结 (边工作,边总结,边记录)(20201013帮助解决切换到历史commit推送代码问题).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值