Git进行代码管理

在新公司上班一个月了,原来用惯了svn,这回的公司用git,还很不习惯,还好网上资料很多,同时公司也有关于git的书籍,可以拿来看,总算能进行简单的代码管理了。

他的工作流程与svn还是蛮像的:取代码 → 每次工作前更新代码到最新版本 → 修改代码 → 提交代码到服务器

只不过他是用命令行的,当然了git也有可视化的操作工具,但是我没有使用。想使用的可以自己去下载:sourcetree

1. 取代码及修改全局设置

a. 设置用户名与邮箱

git config --global user.name "My Name"
git config --global user.email "[email protected]"
b. 从已有的git库中提取代码
git clone git@server:app .git myrepo

2. 每次更改代码的操作

a. 更新本地代码到最新版本(需要merge才能合到本地代码中)

git fetch

b. 合并更新后的代码到本地

git merge

c. 更新代码方式的另一种方法(git pull是git fetch和git merge命令的一个组合)

git pull

d. 修改代码后,查看已修改的内容

git diff --cached

e. 将新增加文件加入到git中

git add file1 file2 file3
也可以 git add .

f. 从Git中删除文件

git rm file1
git rm -r dir1

g. 提交修改

git commit -m 'this is memo'

如果想省掉提交之前的 git add 命令,可以直接用

git commit -a -m 'this is memo'

commit和commit -a的区别, commit -a相当于:

  • 第一步:自动地add所有改动的代码,使得所有的开发代码都列于index file中
  • 第二步:自动地删除那些在index file中但不在工作树中的文件
  • 第三步:执行commit命令来提交

h. 提交所有修改到远程服务器,这样,其它团队成员才能更新到这些修改

git push

3. 附注

遇到过的一些错误:

Git – fatal: Unable to create ‘/path/my_project/.git/index.lock’: File exists.

fatal: Unable to create ‘/path/my_proj/.git/index.lock’: File exists.

If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.

可以试着删除 index.lock

rm -f ./.git/index.lock




你可能感兴趣的:(git,代码管理)