git常用命令

git help config;//git help command

git config --global user.name "Yanlong Wang"

git config --global user.email "wangyanlong0107@gmail.com"

git config --global color.ui true      //增加命令行颜色

git init;

git status;

git add readme.txt license.doc;//当前目录下两个文件

git add ../docs/*.txt;//docs目录下的*.txt文件

git add ../docs/;//add docs目录下所有

git add --all;//add all files

git add "*.txt";//add all txt files in the whole project

git commit -m "message"; //commit 生成一个快照snapshot,并压入snapshot stack

git commit -am "message";//adds and commits in the same step;

git log;//查看snapshot stack,至顶向下

 

git diff (HEAD);//show unstaged differences since last commit

git diff HEAD^;//

git diff HEAD^^;

git diff HEAD~5;//show unstaged differences since last 5th commit

git diff --stage;//view staged differences

git reset HEAD license.txt;//unstage license.txt,HEAD refers to last commit locally.从stage->unstage改变还在

git checkout -- license.txt;//blow away all changes since last commit.unstage状态下blow away改变不在

git reset --soft HEAD^;//undo last commit,put changes into staging.

git commit --amend -m "new message";//overide the last commit. 

git reset --hard HEAD^^;//undo last two(^^) commit and all changes.

git remote add origin https://github.com/.....;增加远程repo origin的url address.

git push -u origin master;origin->remote repo branch,master->local repo branch

git pull <remote name> master;可以省略,默认git pull origin master

git remote add <name> <address>;//add new 远程仓库 

git remote rm <name>;//remove remotes repo branch

git push -u <name> <branch>;//to push to remote repo ;branch usually master

 

git clone https://github.com/YanlongWang/CS472-final-presentation.git (localfoldername);clone远程repo到localfoldername省略则为当前目录

git branch;//show all branches of code

git branch <branch_name>;//create branch

git checkout <branch_name>;//change branch

git merge <branch_name>;//merge branch_name to current timeline branch.每一次mergre都会产生一次新的commit,pull=fetch+merge

git branch -D <branch_name>;//delete branch

git checkout -b <branch_name>;//create and checkout to branch_name branch

git commit -a;//merge commit,修改冲突后的提交

 

git checkout -b shopping_cart;

git push origin shopping_cart;//origin指远方repo,会创建一个远程的shopping_cart branch 并关联local的shopping_cart branch

git commit -am "add some files";

git push;//因为已经tracking了所以直接push。

git pull;//把远程所有分支都下载

git branch;//list all local branches

git branch -r;//list all remote branches

git remote show [remote_name];

git push origin :[branch_name]; //delete remote branch

git branch -d/-D [branch_name];//delete local branch

git remote prun origin;//clean up deleted remote branches.

git push heroku-staging staging:master;//will push and deploy staging on heroku.

git tag;//list all tags which is a reference to a commmit(used mostly for release versioning)

git checkout [tag_name];//checkout to tag_name

git tag -a v0.03 -m "version 0.03";//to add a new tag

git push --tags;//push with tags included

 

git fetch;

git rebase;

 

git log --pretty=oneline;

git log --oneline -p;

git log --oneline --stat;

git log --oneline --graph;

git log --until=1.minute.ago;

git log --since=1.hour.ago;

git log --since=1.month.ago --until=2.weeks.ago;

git diff sha1..sha2;//diff between commits

git blame index.html --date short;//index.html的修改历史

 

 

git rm --cached *.log;//tell git stop tracking *.log files

.gitignore logs/*.log//stop tracking *.log files

git add .gitignore;

git commit -m "Ignore all log files";

 

git config --global alias.mylog \ "log --pretty=format:'%h %s [%an]' --graph"

git config --global alias.lol \ "log --pretty --decorate --pretty=oneline --abbrev-commit --all"

git mylog/lol;//short for

 

<think>我们被要求生成一个关于Git常用命令的汇总列表。根据用户的要求,我们需要提供结构清晰的回答,并尽量保证真实可靠。同时,我们参考了站内引用中提供的Git命令信息。根据引用内容,我们可以整理出以下常用命令:1.基本操作(初始化、添加、提交等)2.分支管理3.远程仓库操作4.标签管理5.历史查看与撤销6.其他实用命令(如删除大文件、清理仓库等)我们将按照这些类别来组织命令。注意:由于引用中提供了一些特定问题的解决方案(如删除大文件),我们也会将其作为实用命令的一部分。另外,注意用户要求使用中文回答,并且行内数学表达式使用$...$格式(但本问题不涉及数学表达式,所以忽略),独立公式使用$$...$$格式(同样不涉及)。同时,在回答末尾生成相关问题。我们开始整理Git常用命令汇总:</think>### Git常用命令汇总 以下是Git常用命令分类整理,帮助您高效管理代码版本: #### 1. 仓库初始化与基础操作 ```bash # 初始化新仓库 $ git init # 克隆远程仓库 $ git clone <远程仓库URL> # 查看当前状态 $ git status # 添加文件到暂存区 $ git add <文件名> # 添加单个文件 $ git add . # 添加所有修改 # 提交更改 $ git commit -m "提交说明" ``` #### 2. 分支管理 ```bash # 创建分支 $ git branch <新分支名> # 切换分支 $ git checkout <分支名> $ git switch <分支名> # Git 2.23+推荐方式 # 创建并切换分支 $ git checkout -b <新分支名> $ git switch -c <新分支名> # 合并分支 $ git merge <要合并的分支名> # 删除分支 $ git branch -d <分支名> # 安全删除 $ git branch -D <分支名> # 强制删除未合并分支 ``` #### 3. 远程仓库操作 ```bash # 添加远程仓库 $ git remote add origin <远程仓库URL> # 查看远程仓库 $ git remote -v # 推送到远程仓库 $ git push -u origin <分支名> # 首次推送 $ git push # 后续推送 # 拉取远程更新 $ git pull origin <分支名> $ git pull --rebase origin <分支名> # 变基式拉取 # 删除远程连接 $ git remote rm origin ``` #### 4. 版本回退与撤销 ```bash # 查看提交历史 $ git log $ git log --oneline # 简洁版日志 # 撤销工作区修改 $ git checkout -- <文件名> # 撤销暂存区文件 $ git reset HEAD <文件名> # 版本回退 $ git reset --hard <commit_id> # 彻底回退 $ git reset --soft <commit_id> # 保留修改 # 恢复删除的文件 $ git restore <文件名> # Git 2.23+ ``` #### 5. 标签管理 ```bash # 查看标签 $ git tag # 创建标签 $ git tag <标签名> # 轻量标签 $ git tag -a v1.0 -m "版本说明" # 附注标签 # 推送标签 $ git push origin <标签名> # 删除标签 $ git tag -d <标签名> # 本地删除 $ git push origin :refs/tags/<标签名> # 远程删除 ``` #### 6. 高级维护 ```bash # 清理历史大文件 $ git filter-branch --force --index-filter \ 'git rm -rf --cached --ignore-unmatch <文件名>' \ --prune-empty --tag-name-filter cat -- --all # 回收存储空间 $ git reflog expire --expire=now --all $ git gc --prune=now # 查看仓库大小 $ git count-objects -vH ``` #### 7. 配置相关 ```bash # 设置用户名/邮箱 $ git config --global user.name "用户名" $ git config --global user.email "邮箱" # 查看配置 $ git config --list # 设置默认编辑器 $ git config --global core.editor vim ``` > **最佳实践提示**: > 1. 频繁提交小变更,保持提交记录清晰 > 2. 使用`.gitignore`文件排除无需版本控制的文件 > 3. 重要分支(如main/prod)设置保护规则 > 4. 推送前先执行`git pull --rebase`避免合并提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值