Git基本使用

一、基本命令

1、初始化仓库
git init
2、配置仓库
git config user.name xxx
git config user.email xxx
git config --global user.name xxx
git config --global user.email xxx

前两种是配置在项目文件夹下,后两个是配置全局的
3、指南使用
git help 子命令
退出       Q
下一页     空格
上一页     control+B
搜索      / 然后输入要搜索的内容后回车

二、常用指令

git status               查看文件状态
git add                  添加文件的暂存区
git commit 文件名称       添加到本地仓库
    注意:如果没有在commit后面加上 -m说明修改了什么,
    会自动进入vim界面,要求我们输入修改信息
    输入修改信息步骤:
       按键盘上的i
       输入信息
       按键盘上的esc
       输入:wq然后回车
       
git config alias.别名 指令名
    git config alias.st status 这种方法设置的别名是局部的
    git config --global alias.st status 这样设置是全局的
    当然了,不建议去起别名
    
git log           查看所有版本库日志
git log 文件名     查看指定文件名的版本库日志
    git中的版本号是一个哈希值

git reflog        查看版本库所有修改(包括提交和回退)

回退到相应版本号(--hard表示强制)
git reset --hard HEAD   回到当前版本,放弃所有没有提交的修改(git checkout 文件名 具有相同功能)
git reset --hard HEAD^  回到上一个版本
git reset --hard HEAD~(3) 回到之前第三个修订版本
get reset --head 版本号    回到相应的版本号
    (版本号是取前7位,可以通过git reflog指令获得)
    
git diff 文件名    查看文件修改了什么地方

三、远程仓库配置

这里的远程仓库可以是在本地的,也可以是在公司服务器上的。

1、git init --bare

注意:这个仓库是用于管理代码的,和git init生成的是不一样的


Paste_Image.png
2、先克隆一份空的仓库到本地
git clone /Users/username/Desktop/StudyCode/远程仓库
3、忽略不需要加入版本控制的文件以及文件夹(这里的配置方法是在github上搜索的,里面的内容请搜索.gitignore)
.gitignore
echo -e "# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other    
*.moved-aside
*.xccheckout
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md

fastlane/report.xml
fastlane/screenshots > .gitignore

注意:生成好的.gitignore文件一定要和.git隐藏文件夹在同一级目录

4、将.gitignore添加到版本控制
git add .gitignore
git commit .gitignore -m"配置忽略文件"
5、新建项目

新建项目之后,可以直接使用Xcode中的工具提交

source control -> commit    将代码提交到本地仓库
source control -> push      将代码提交到远程仓库
注意:git中默认会创建一个分支,这个分支叫做origin/master
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容