【git基础】git常用操作及配置

git操作阶段

 

 git操作

复制代码
git 操作:
git config --list # 查看git配置属性 git init git clone git pull origin branch_name(更新本地仓库) 修改本地仓库 git status git
diff git add filename git config --global user.name "gitlab" git config --global user.email "xxx@yy.com" git commit -m "description" git status git log --oneline
git log
git log --oneline /path/to/file # 查看某个文件的log git push origin branch_name(将本地分支push到远程分支) git branch(查看本地分支) git branch
-a (查看远程所有分支) git checkout -b dev origin/dev(创建并切换分支branchname=dev) git branch branch_name (创建本地分支)
# 在删除本地分支之前,请确保使用git checkout命令切换到不想删除的另一个分支 git branch
-d branch_name(删除本地分支) git checkout branchname(切换本地分支) git push origin --delete branch_name(删除远程分支) git branch -n oldname newname(重命名本地分支) git remote add origin git@gitlab.uu.ai:xxx/pytorch_semantic_segmentation.git(关联远程仓库) git checkout commit_id(回退到指定版本) git rebase -i (commit-id) (commit-id 为需要删除版本的前一个commit版本的hash code,然后pick->drop)
git restore --staged filename (对于已经add到暂存区的某文件filename,撤销已暂存的文件变更)
复制代码

本地有修改,远程仓库也有修改,如何合并远程仓库

复制代码
# 如果本地有修改,但需要合并远程仓库代码。
# 使用git stash命令来保存本地的更改。git stash命令可以将当前的工作目录和暂存区的更改保存起来,以便稍后恢复。
# 使用git stash pop命令来恢复之前保存的更改。这样就可以避免覆盖本地更改。
#  先将修改的代码保存到暂存区,再拉取、合并最新的远程仓库,之后将暂存区的更改恢复到工作区,最后上传修改内容。
    1)git stash 将已经修改的代码保存到git暂存区
    2) git fetch origin master
    3) git diff origin/master
    4) git merge origin/master
    5) git stash pop 暂存的更改重新应用到当前工作目录
    6) git add .
    7) git commit -m "mark"
    8) git push origin master
复制代码

新建分支,且新建分支的版本是本地master当前版本

# 新建分支,且新建分支的版本是本地master当前版本
1)git checkout master # 确保你当前在master分支上
2)git branch -a
3)git checkout -b newbranch # 从master分支创建一个新分支newbranch, 版本是本地master版本
4)git branch # 确认newbranch分支已经正确创建
5)git push -u origin newbranch # 将newbranch分支推送到远程仓库

git configuration

复制代码
git global setup:
git config --global user.name "xxx"
git config --global user.email "xxx@uu.com"

create a new repository
git clone git@gitlab.uu.ai:xxx/pytorch_semantic_segmentation.git
cd pytorch_semantic_segmentation
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

existing folder
cd existing_folder
git init
git remote add origin git@gitlab.uu.ai:xxx/pytorch_semantic_segmentation.git
git add .
git commit -m "Initial commit"
git push -u origin master

existing git repository
cd existing_repo
git remote add origin git@gitlab.uu.ai:xxx/pytorch_semantic_segmentation.git
git push -u origin --all
git push -u origin --tags
复制代码

创建新的仓库,并推送内容

复制代码
首先需要在远程仓库上创建项目
git init
git remote add origin 远程仓库链接  # 将远程仓库和本地目录关联起来
git add . # 添加所在目录的所有文件
git commit -m "comment content" 
git push -u origin 远程仓库分支名
如果远程有文件,需要将远程仓库的文件更新到本地
git pull --rebase origin 远程仓库分支名
View Code
复制代码

如何不添加注释?

通过shell脚本,在脚本里面写git命令,add commit push 等等,commit时获取git的status;

#!/bin/bash
 
#execute git command
note=`git status`
git status
git add .
git commit -am "$note"
git pull --rebase

 删除远程仓库的文件或目录

复制代码
#删除远程文件,需要先删除本地文件,然后再push;
所有操作的前提是本地和远程的文件内容是完全一样的;
git rm filename # 删除本地文件
git rm -r dir # 删除本地目录

# shell脚本以目前仓库的状态来注释
note=`git status`
git status
git add .
git commit -am "$note"

# 推送到远程分支
git push -u origin 远程仓库分支名
View Code
复制代码

 修改分支名称

复制代码
# 修改分支名
git branch -m oldbranch newbranch  # 重命名本地分支
git push --delete origin oldbranch  # 删除远程分支
git push origin newbranch  # 上传新命名的本地分支
git branch --set-upstream-to origin/newbranch  # 本地分支与远程分支关联
git push -u origin newbranch  # 可以替代以上两条命令
View Code
复制代码

 LFS大文件系统

复制代码
sudo apt-get install git-lfs  # install
git lfs install  # lfs env
git lfs track "*.iso"
git add .gitattributes
git add .
git commit -m "add lfs"
git push
复制代码

 

 

参考

1. git-如何不写注释能自动带上修改文件信息

2. 【Git】如何修改分支名

3. 如何使用git-lfs上传大文件

4. gitpull不覆盖本地_千锋教育

posted on   鹅要长大  阅读(79)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2019-03-24 【leetcode】476. Number Complement

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示