美文网首页
终端和vs code 同步代码到github/gitLab

终端和vs code 同步代码到github/gitLab

作者: 一个摸鱼AI喵 | 来源:发表于2021-11-03 10:36 被阅读0次

    一、创建项目

    首先要在码云中创建项目



    项目中也有命令行指令指示:

    Git 全局设置

    git config --global user.name "杨健"
    git config --global user.email "604637883@qq.com"
    

    创建新版本库(空白)

    git clone http://192.168.46.233/face/seetafacePython.git
    cd seetafacePython
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master
    

    已存在的文件夹

    cd existing_folder
    git init
    git remote add origin http://192.168.46.233/face/seetafacePython.git
    git add .
    git commit -m "Initial commit"
    git push -u origin master
    

    已存在的 Git 版本库

    cd existing_repo
    git remote rename origin old-origin
    git remote add origin http://192.168.46.233/face/seetafacePython.git
    git push -u origin --all
    git push -u origin --tags
    

    二、配置Git bash

    在git-bash终端输入:

    git config --global user.name "yangjian" 
    git config --global user.email "604637883@qq.com"
    

    上面配置一次就可以了,会在C:\Users\用户下生成.giconfig ,内容如下

    [user]
        name = 杨健
        email = 604637883@qq.com
    [core]
        excludesfile = .gitignoreglobal
    

    三、提交代码到码云/Github

    3.1 终端提交代码到码云/Github

    cd D:\AI\Face\seetafaceMy
    git init
    git remote add origin http://192.168.46.233/face/seetafacePython.git
    git add .
    git commit -m "Initial commit"
    git push -u origin master   
    # 默认为master ,也可以自定义如:git branch -M main
    #git push -u origin main
    

    这里我把公司的设置为全局,所以不再需要设置账户,个人github设置为局部.如果要上传代码到个人gitbub执行:

    git init
    git config --local user.name "杨健"
    git config --local user.email "604637883@qq.com"  
    
    git config --local credential.helper store   
    

    如果已经传过giLab,要切换到github,要么删除.init文件夹,要么先执行:

    git remote rm origin  #删除此仓库配置
    

    在提交时可能报错:

    $ git push -u origin main
    fatal: unable to access 'https://github.com/yangjian1218/seetaface6ToPy.git/': Failed to connect to github.com port 443: Timed out
    // 或者:
    fatal: unable to access 'https://github.com/yangjian1218/seetaface6ToPy.git/': OpenSSL SSL_read: Connection was reset, errno 10054
    

    网络问题,再次提交

    3.2 VScode 提交代码到码云

    1. 对于第一次提交, 在Git界面,点击初始化存储库
    
    image-20211014162937485.png

    2.点击推送到,并设置远程库地址跟名称


    image-20211014163021978.png
    image-20211014163049811.png

    添加库名称: seetafaPython

    3. 点击更改,第一次选择更改的目录 + :暂存更改,之后可以选择单个文件的+。 并在上面写上描述
    
    image-20211014163521178.png

    4.提交已暂存文件


    image-20211014163644407.png

    点击后,会弹出让你输入账号密码,把你托管平台的账号密码输入上去就行了。。。

    不出问题的话你整个项目就会提交到云端上了。

    在vs中每次更新代码都会要输入账号密码,方便起见,可以配置一下让GIT记住密码账号。

    git config --global credential.helper store   //在Git Bash输入这个命令就可以了
    
    1. 点击$(云上传)发布更改


      image-20211014163730639.png

    四、克隆代码

    首先你电脑还是的有vscode 和 git,,然后用git把上面那些全局配置再执行一次,如下

    git config --global user.name "杨健"
    git config --global user.email "604637883@qq.com"  
    
    git config --global credential.helper store    
    

    然后打开Git Bash输入以下命令

    cd D:\AI\Face\seetafaceMy   //指定存放的目录
    git clone https://git.oschina.net/name/test.git   //你的仓库地址
    

    五、取消某部分文件上传

    有些文件,比如个人的设置, 数据集等不想共享,所以要取消上传, 设置方式如下:

    方法一: .gitignore

    新建.gitignore,内容如下:

    # 此为注释 – 将被 Git 忽略
    *.a       # 忽略所有 .a 结尾的文件
    !lib.a    # 但 lib.a 除外
    /TODO     # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
    build/    # 忽略 build/ 目录下的所有文件
    doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
    

    .gitignore 还有个有意思的小功能,一个空的 .gitignore 文件可以当作是一个placeholder。如当你需要为项目创建一个空的log目录时,这就变的很有用。使用方法:创建一个log目录在里面放置一个空的.gitignore文件,当你clone这个repo的时候git会自动的创建好一个空的log目录了

    另外 git 提供了一个全局的 .gitignore,
    你可以在你的用户目录下创建 .gitignoreglobal 文件,以同样的规则来划定哪些文件是不需要版本控制的。
    需要执行
    git config --global core.excludesfile ~/.gitignoreglobal
    来使得它生效。 此方法适合针对某个类型的文件进行设置,比如datasets文件,以下为例子:

    # Compiled source #
    ###################
    *.com
    *.class
    *.dll
    *.exe
    *.o
    *.so
     
    # Packages #
    ############
    # it's better to unpack these files and commit the raw source
    # git has its own built in compression methods
    *.7z
    *.dmg
    *.gz
    *.iso
    *.jar
    *.rar
    *.tar
    *.zip
     
    # Logs and databases #
    ######################
    *.log
    *.sql
    *.sqlite
     
    # OS generated files #
    ######################
    .DS_Store
    .DS_Store?
    ._*
    .Spotlight-V100
    .Trashes
    Icon?
    ehthumbs.db
    Thumbs.db
    

    方式二:.git/info/exclude

    # git ls-files --others --exclude-from=.git/info/exclude
    # Lines that start with '#' are comments.
    # For a project mostly in C, the following would be a good set of
    # exclude patterns (uncomment them if you want to use them):
    # *.[oa]
    # *~
    datasets/
    test.py
    

    如上 取消dataset文件夹下的所有文件、test.py上传

    区别:.git/info/exclude 这里设置的 则是你自己本地需要排除的文件。 他不会影响到其他人。也不会提交到版本库中去

    该方法有可能因为之前如果已经执行了add . 跟commit 那么将失效.

    补充:取消/删除

    1、如果已经git add之后想删除某条记录,可以使用git rm来从记录文件里删除记录避免被commit执行。
    2、删除以前提交的commit,可push push -f origin。

    相关文章

      网友评论

          本文标题:终端和vs code 同步代码到github/gitLab

          本文链接:https://www.haomeiwen.com/subject/ytohzltx.html