简易的命令行入门教程:(新仓库初次提交)
Git 全局设置:
git config --global user.name "moao.net"
git config --global user.email admin@1okk.com
创建 git 仓库:
mkdir demo
cd demo
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/y141111/demo.git
git push -u origin "master"
已有仓库:
cd existing_git_repo
git remote add origin https://gitee.com/y141111/existing_git_repo.git
git push -u origin "master"
设置用户名和邮箱
# 设置全局用户名称和邮箱
git config --global user.name "moao.net"
git config --global user.email admin@1okk.com
检查配置信息
# 显示所有Git配置项
git config --list
检查特定配置项
# 显示用户名配置
git config user.name
获取帮助
# 查看config命令的帮助文档
git help config
初始化仓库
在现有目录中
# 初始化Git仓库
git init
从远程仓库克隆
# 克隆远程仓库
git clone https://github.com/libgit2/libgit2
# 克隆并指定本地目录名
git clone https://github.com/libgit2/libgit2 mylibgit
记录更新到仓库
检查状态
# 查看文件状态
git status
跟踪新文件
# 添加新文件到暂存区
git add README.txt
暂存修改
# 暂存已修改文件
git add README.txt
查看差异
# 查看未暂存的修改
git diff
# 查看已暂存的修改
git diff --cached # 或 git diff --staged
提交更新
# 提交暂存区的修改并附带提交信息
git commit -m "Add feature XYZ"
快速提交(跳过暂存)
# 提交所有已跟踪文件的修改,跳过暂存步骤
git commit -a -m "Commit all changes"
移除文件
# 从仓库和工作目录中移除文件
git rm <filename>
# 仅从仓库中移除文件,保留工作目录中的文件
git rm --cached <filename>
移动文件
# 重命名文件并将其添加到暂存区
git mv oldfile newfile
忽略文件
创建.gitignore
文件
# 示例`.gitignore`内容
# 忽略所有.o和.a文件
*.[oa]
# 忽略所有以~结尾的备份文件
*~
查看提交历史
# 查看提交历史
git log
撤销操作
修改最近一次提交
# 修正最近一次提交的信息或添加遗漏的文件
git commit --amend
取消暂存文件
# 取消暂存指定文件
git reset HEAD <file>
撤销对文件的修改
# 恢复文件至最近一次提交的状态
git checkout -- <file>
远程仓库操作
查看远程仓库
# 列出所有远程仓库
git remote
# 显示远程仓库的URL
git remote -v
添加远程仓库
# 添加远程仓库并指定别名
git remote add <shortname> <url>
抓取远程仓库
# 从远程仓库下载新数据
git fetch <remote-name>
拉取并合并
# 拉取远程分支并尝试自动合并到当前分支
git pull <remote-name> <branch-name>
推送到远程仓库
# 推送本地分支到远程仓库
git push <remote-name> <branch-name>
重命名与移除远程仓库
# 重命名远程仓库
git remote rename <old-name> <new-name>
# 移除远程仓库
git remote rm <remote-name>
掌握了上述命令,你已经能够应对大多数Git日常操作。继续探索,成为Git高手!
评论 (0)