git常用命令

来源:互联网 发布:three.js 球体 编辑:程序博客网 时间:2024/06/05 16:17

1.新建本地仓库

git init

2.配置相关

(1)查看配置:git config --list

(2)查看某一项配置:git config user.name

(3)设置配置:git config user.name "your name"

3.从远程下载

git clone url

4.从远程更新

git pull origin master    //将远程更新下载到本地仓库并且合并到本地项目

或者

(1)git fetch origin master    //将远程更新下载到本地仓库

(2)git merge origin/master    //将本地仓库资源合并到本地项目

pull虽然方便省事,但不建议使用。

5.添加到版本控制

将新增或者修改的文件添加到版本控制:git add filepath

将删除的文件添加到版本控制:git rm filepath(只能删除指定的文件)

如果你删除了一个在版本控制之下的文件,那么使用git add .不会在索引中删除这个文件。需要通过带-a选项的git commit命令和-A选项的git add命令来完成:

# Create a file and put it under version controltouch nonsense.txtgit add . && git commit -m "a new file has been created"# Remove the filerm nonsense.txt# Try standard way of committing -> will not work git add . && git commit -m "a new file has been created"# Now commit with the -a flaggit commit -a -m "File nonsense.txt is now removed"# Alternatively you could add deleted files to the staging index viagit add -A . git commit -m "File nonsense.txt is now removed"

6.提交到本地仓库

git commit -m "comment"

7.提交到远程

git push origin master

8.查看状态

git status

9.忽略文件

在项目根目录下新建文件.gitignore,然后将需要忽略的文件或文件夹添加至新建的文件里。

需要注意的是,如果需要忽略的文件已经加入了版本控制,则需要先让其脱离版本控制,命令是:git rm --cached filename。

如果要让一个目录及其子目录下所有的文件脱离版本控制,则使用命令:git rm --cached -r filepath

10.将本地项目托管到远程服务器

(1)在远程服务器上新建项目<产生项目url, 如git@code.csdn.net:xxx/xxx.git>

(2)将代码提交到git本地仓库

(3)$ git remote add origin <项目url, 如git@code.csdn.net:xxx/xxx.git>

(4)$ git push -u origin master



0 0