git 基础操作

git 基础操作

克隆一个仓库到本地:

git clone git@github.com:RyanFu999/layout.git layout2

git 查看远程仓库:

git remote -v

git 删除远程分支:

git push origin --delete branchName

git 更新远程代码到本地

git pull origin master
  1. 查看本地分支文件信息,确保更新时不产生冲突

    git status
    
  2. 查看当前分支情况

    git branch
    
  3. 若分支为本地分支,则需切换到服务器的远程分支

    git checkout remote branch
    
  4. 拉去远程分支并合并

    git pull # fetch and merge
    

若命令执行成功,则更新代码成功!

快速流程:

上面是比较安全的做法,如果你可以确定什么都没有改过只是更新本地代码

git pull # fetch and merge

git 添加代码到远程仓库

要在当前仓库的根目录执行

git init # 初始化本地仓库
git add . # 添加当前文件夹下所有文件到版本库
git commit -m "first commit" # 把添加的文件提交到版本库,并填写提交备注

git remote add origin 你的远程库地址  # 把本地库与远程库关联

git push -u origin master    # 第一次推送时

git push origin master  # 第一次推送后,直接使用该命令即可推送修改

添加一个新的目录到已有仓库:

git add newDirectory
git status # 查看当前仓库状态
git commit -m "add newDirectory"
git push origin master

git 忽略目录或文件

# 导航到仓库根目录
cd path/to/your/repository

touch .gitignore

echo "ignored_directory/" >> .gitignore

# 将已经提交到git的要忽略的目录从暂存区中移除,但不会删除实际的文件或目录。
git rm -r --cached ignored_directory

git commit -m "Ignore all .gitignore files in subdirectories"

# 推送更改到远程仓库(例如main分支)
git push origin main

要忽略的目录可能位于子目录中

cd path/to/your/repository

touch .gitignore
echo "**/build-*/" >> .gitignore

# 将所有以 build- 开头的目录从暂存区中移除
find . -type d -name 'build-*' -exec git rm -r --cached {} +

要忽略的文件位于子目录中:

cd path/to/your/repository

# 查找所有子目录中的 .gitignore 文件
find . -type f -name '.gitignore'

touch .gitignore

echo "**/.gitignore" >> .gitignore

# 将所有子目录中的 .gitignore 文件从暂存区中移除
# 执行这个命令时,如果有不在仓库中的 .gitignore 文件被找到,命令会停止执行
# 不忽略根目录中的 .gitignore 命令,find 后加 -mindepth 2 参数
find . -type f -name '.gitignore' -exec git rm --cached {} +
posted @ 2024-06-20 16:16  卑以自牧lq  阅读(1)  评论(0编辑  收藏  举报