Git学习笔记


git大概是每个程序员必须要掌握的工具之一

常用

git init ——初始化,创建git仓库
git add filename ——添加文件
git commit -m “日志说明” ——提交文件
git log / git log –pretty=oneline
git reset –hard commit_id ——回滚到指定版本
git reflog ——命令历史
git checkout – filename ——工作区撤销,与暂存区相同
git reset HEAD filename——暂存区file撤回到工作区
git rm filename ——删除文件

版本回退

HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset –soft/–mixed/–hard id
回退,用git log可以查看提交历史
重返未来,用git reflog查看命令历史

避免重复劳动

在master分支上修复的bug,想要合并到当前dev分支,可以用git cherry-pick commit命令,把bug提交的修改“复制”到当前分支

分支

查看分支:git branch
创建分支:git branch name
切换分支:git checkout name*或者git switch *name
创建+切换分支:git checkout -b name*或者git switch -c *name
合并某分支到当前分支:git merge name
删除分支:git branch -d name

查看分支图
git log –graph –decorate –oneline –simplify-by-decoration –all
说明:
–decorate 标记会让git log显示每个commit的引用(如:分支、tag等)
–oneline 一行显示
–simplify-by-decoration 只显示被branch或tag引用的commit
–all 表示显示所有的branch,这里也可以选择,比如我指向显示分支ABC的关系,则将–all替换为branchA branchB branchC###打包
git stash 打包现场
git stash list 查看stash
git stash pop 回到工作现场

本地与远程

关联一个远程库git remote add origin git@server-name:path/repo-name.git;
git push -u origin master第一次推送master分支的所有内容
git push origin master此后推送最新修改
从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
建立本地分支和远程分支的关联,使用git branch –set-upstream branch-name origin/branch-name;
从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

#
You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×