git简介:
git用于代码管理。在git中,代码首先要存储在远程仓库,本文简单说明如何建立远程仓库,以及push本地代码到远程仓库的过程。主要分为三步:建立本地仓库并comit代码;建立远程仓库;进行仓库关联并提交代码(从本地仓库提交到远程仓库)。这里的远程就是服务器,本地就是你当前的电脑。
git创建远程仓库并进行代码提交使用举例:
(1)建立本地仓库并comit代码:
建立本地仓库:git init
进入到存放代码的目录下,用git init建立本地仓库:
% git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /Users/user/work/src/testsrc/.git/
结果,会在当前文件夹下创建.git目录,内容如下:
% cd .git
.git % ls
HEAD config description hooks info objects refs
提交代码(commit):
% git add . //1.添加代码
% git status //2.查看状态(可以省略者一步)
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.c
% git commit -m "new" //3. 提交代码到本地仓库
[master (root-commit) b9b2f83] new
1 files changed, 7 insertions(+)
% git branch //查看分支
* master
这样,本地代码仓库就建好了,而且,代码也提交到本地仓库了。
(2)建立远程仓库:
在github网站上创建远程仓库:
选择 Repositories,【new】一个仓库,在Create a new repositorie界面,输入repositorie name,创建即可。
(3)将本地仓库和远程仓库进行关联并push代码:
仓库关联,命令如下:
git remote add 别名 远程仓库地址 ,如下:
git remote add auto-policy-python git@192.168.128.211:usercon/test-src-c.git
push代码到远程仓库:
git push -u test-src-c master
% git push -u auto-policy-python master Enter passphrase for key '/Users/user/.ssh/id_rsa': Enumerating objects: 39, done.
总结:
用到的命令如下:
git init //建立本地仓库git add . //将当前文件夹下的所有文件添加到暂存区
git commit -m "message" //提交到本地仓库
在github网站上创建远程仓库
git remote add alias remote_addr //将本地仓库和远程仓库进行关联
git push -u //将文件push到远程仓库