版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38069632/article/details/82391117
建立 .gitignore文件(定义 Git 提交时要忽略的文件)。一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式。
touch .gitignore
建立 README.md(项目介绍文件)
touch README.md
初始化Git 仓库
git init
添加当前项目的所有文件中有变动的文件添加到本地的暂存区
git add .
查看Git 工作区的状态信息
git status
将暂存区的数据提交到本地仓库,并加入提交的信息
git commit -m "init Git"
Git 提供了一个跳过使用暂存区域的方式, 只要在提交的时候,给 git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤。
git commit -a -m "init Git"
添加远程仓库( 格式为 git remote add [shortname] [url] ),这里origin是为这个远程仓库添加一个引用别名, [email protected]:suinichange/JConcurrency.git 是你GitHub 远程仓库的地址
git remote add origin git@github.com:suinichange/JConcurrency.git
将本地仓库推送到远程仓库,也就是你的GitHub 上
git push -u origin master
执行此命令可能出现的问题:
- 首次整合远程代码,需要先把远程仓库的代码先拉取下来再提交
报错代码:
To github.com:suinichange/JConcurrency.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]:suinichange/JConcurrency.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方案:执行 git pull ,再执行 git push -u origin master
- 当前的分支版本落后于远程仓库分支版本
报错代码:
To github.com:suinichange/JConcurrency.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:suinichange/JConcurrency.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方案:如果远程仓库刚创建并没有代码文件,我们可以采取强制覆盖的方式,将本地项目直接覆盖远程仓库项目。
执行 git push -u -f origin master