Git入门

来源:互联网 发布:linux新建配置conf文件 编辑:程序博客网 时间:2024/04/29 22:17
Git入门

以备份本地小说为例,记录git初步使用方法

陆续更新

本地仓库(repository,又称版本库)

  1. 在某路径下新建一个novel_uptodate版本库,文件夹内右键选择Git Bash Here,打开git命令行。

  2. 用git init命令把该目录编程git可以管理的仓库,之后加入文件。

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate$ git initInitialized empty Git repository in E:/【ATI】my importance/file/my_world/原创/novel_uptodate/.git/
  3. 使用git add命令将目录中的文件添加到暂存区中。

    • git add *.txt #添加单一文件
    • git add . #添加全部文件
    • git add –all #添加全部文件
    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master)$ git add .           #无提示即为成功
  4. 使用git commit命令把文件提交到仓库中,commit命令可以同时提交多个文件。

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master)$ git commit -m "update all"[master (root-commit) 6313f4c] update all 19 files changed, 1 insertion(+) create mode 100644 README.md ......
  5. 可用git status查看当前版本库变化状态。


远程仓库(github)

* 默认已在github上创建账号,并根据提示进行过SSH Key的设置 *

  1. 在github上创建新的仓库,远程仓库名和本地仓库名字相同。

  2. 使用git remote add origin * 仓库地址 * 命令连接本地和远程仓库。

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master)$ git remote add origin https://gitee.com/shizusilen/novel_uptodate.git
  3. 使用git push -u origin master命令把本地仓库分支master内容推送到远程仓库,并输入用户名和密码
    如果push出错,有两种方法处理:

    • 先使用git push –rebase origin master进行远程和本地代码合并,再重新执行push
    • 使用git push -u origin master -f强制push
    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master|REBASE 1/1)$ git push -u origin master -fUsername for 'https://gitee.com': ********Counting objects: 22, done.Delta compression using up to 4 threads.Compressing objects: 100% (20/20), done.Writing objects: 100% (22/22), 1.07 MiB | 113.00 KiB/s, done.Total 22 (delta 1), reused 0 (delta 0)To https://gitee.com/********/novel_uptodate.git + b09adc6...6313f4c master -> master (forced update)Branch master set up to track remote branch master from origin.
  4. 成功之后,只要本地做了提交,使用git push origin master命令即可


远程仓库(github & 码云)

* 将本地仓库同时关联GitHub和码云 *

  1. 使用多个远程库时要注意,git给远程库起的默认名称是origin,如果有多个远程库,需要用不同的名称来标识不同的远程库。
    因此,先要使用git remote rm origin命令删除已关联的名为origin的远程库

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master|REBASE 1/1)$ git remote rm origin
  2. 使用git remote add github * 远程仓库地址 * 命令关联github的远程库

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master|REBASE 1/1)$ git remote add github https://github.com/*******/novel_uptodate.git
  3. 使用git remote add gitee * 远程仓库地址 * 命令关联码云的远程库

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master|REBASE 1/1)$ git remote add gitee https://gitee.com/*******/novel_uptodate.git
  4. 使用git remote -v命令查看远程库信息

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master|REBASE 1/1)$ git remote -vgitee   https://gitee.com/********/novel_uptodate.git (fetch)gitee   https://gitee.com/********/novel_uptodate.git (push)github  https://github.com/********/novel_uptodate.git (fetch)github  https://github.com/********/novel_uptodate.git (push)
  5. 使用git push github master命令推送到github(出错解决方法见上)

    madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master|REBASE 1/1)$ git push github master -fUsername for 'https://github.com': *********Counting objects: 22, done.Delta compression using up to 4 threads.Compressing objects: 100% (20/20), done.Writing objects: 100% (22/22), 1.07 MiB | 7.00 KiB/s, done.Total 22 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), done.To https://github.com/***********/novel_uptodate.git + db8c8c9...6313f4c master -> master (forced update)
  6. 使用git push gitee master命令推送到码云(出错解决方法见上)

madhattt@madhattt-PC MINGW64 /e/【ATI】my importance/file/my_world/原创/novel_uptodate (master|REBASE 1/1)$ git push gitee masterUsername for 'https://gitee.com': **********Everything up-to-date
原创粉丝点击