git常用命令 (自用)

自己用的

<!-- (一) -->
    git clone "url"     将代码克隆到本地


<!-- (二)常用的四步命令(建议这个顺序,(先 commit 后 pull)) -->
    1. git add .       添加到暂存
    2. git commit -m "miaoshu"     提交暂存区的文件到本地仓库(添加文件描述信息)
    3. git pull        拉取
    4. git push        推送


<!-- (三) git 首次推送到远程流程 -->
    1. git init //初始化仓库
    2. git add .(文件name)      添加文件到本地仓库
    3. git commit -m "first commit"     提交暂存区的文件到本地仓库(添加文件描述信息)
    4. git remote add origin + 远程仓库地址         链接远程仓库,创建主分支
    5. git pull origin master --allow-unrelated-histories       把本地仓库的变化连接到远程仓库主分支 (:q) 退出 
    6. git push -u origin master        把本地仓库的文件推送到远程仓库
        不是首次推送的话,git push origin master



<!-- (四)Git强制覆盖本地代码,保持与远程仓库一致 -->
    1. git fetch --all
    2. git reset --hard origin/master
    3. git pull


<!-- (五) git切换仓库 -->

    1. git remote -v        查看
    2. git remote set-url origin "自己的git仓库地址"
    3. git remote -v        (查看是否更改了)
    4. git add . 
    5. git commit -m ""
    6. git push  

  

<!--(六) git 忽略推送文件  .gitignore -->
    语法:
        # 代表注释
        /bin/*          忽略跟目录下bin文件
        *.class         忽略所有.class文件 (.class )
        !/bin/*.java    不要忽略根目录下bin文件夹中.java文件
        -----------------------------------
        Git忽略规则:
            #               注释,内容被 Git 忽略
            .sample       忽略所有 .sample 结尾的文件
            !lib.sample     但 lib.sample 除外
            /TODO         仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
            build/        忽略 build/ 目录下的所有文件
            doc/.txt      会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
            []              匹配字符列,如 [Ll]ibrary 意为Library或library均满足条件
        ------------------------------
    1.打开命令框,创建.gitignore文件 (touch .gitignore)
        将要忽略的文件放进去就可以了 例如:
        node_modules
        .vscode

<!-- (七) 如果如果已经push了之后,要再往 .gitignore 中添加文件-->
    按照上述方法定义后发现并未生效,
    原因是.gitignore只能忽略那些原来没有被track的文件,
    如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
    那么解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:

    git rm -r --cached .
    git add .
    git commit -m 'update .gitignore'
    

  

<!-- (八)  git Please tell me who you are解决方法 -->

   https://blog.csdn.net/qq_26540999/article/details/53445589

  

<!-- (九)git批量处理命令 -->  
    在Git本地仓库目录下新建一个bat文件,如 gitpush.bat

        git add .
        git commit -m "ss"
        git pull origin master
        git push origin master
        (在命令框注入 ./gitpush 运行 或者 直接双击.bat文件运行)

  

原文地址:https://www.cnblogs.com/xiaoyaolang/p/14638316.html