git快速入门 push/clone/reset/merge/切换分支全都有

本文介绍git快速入门,从安装/创建init / 发布push/版本回退reset / branch分支切换/合并分支merge 这些基本的操作都有涉及,方便新人快速入手,有需要的朋友mark一下。首先安装git,本教程以git for windows为例。安装比较简单,这里就不累述了。

安装完成以后进行配置
$ git config –global user.name “Your Name”
$ git config –global user.email “email@example.com”
———

创建版本库
进入目录$ cd /d/ (将指定目录变成git库:打开文件夹,右键点击 git bash here)
创建新目录 $ mkdir learngit
进入刚刚创建的目录$ cd learngit
查看目录列表$ pwd
将目录变成git可以管理的仓库 $ git init
这时会多了一个隐藏的.git目录,用ls -ah命令就可以看见。

《git快速入门 push/clone/reset/merge/切换分支全都有》

 

———
添加文件/提交文件

在目录手动添加一个readme.txt文件
在git bash编辑器中 $ git add readme.txt
查看状态 $ git status
用notepad修改readme.txt文件
再查看状态 $ git status,红色部分表示修改的地方
添加修改记录日志文件 $ git add -A
注释修改了哪些地方 $ git commit -m”all”

——–
远程上传到github
头像 – settings – SSH and GPG keys – generating SSH keys链接
1.打开git bash
2.黏贴如下命令,将邮箱改成你的github邮箱
$ ssh-keygen -t rsa -b 4096 -C “your_email@example.com”
3.回车,回车,回车
4.出现一串气泡的代码,表示创建成功

打开文件夹C:\Users\Administrator\.ssh
id_rsa是私有钥匙,不要公开给别人
id_rsa.pub是共有钥匙
将公有钥匙添加到generating SSH keys进行创建

如果是第一次提交:在git bash中输入
git remote add origin https://github.com/ytkah/learngit.git
git push -u origin master
会弹出github登陆窗口,输入账号密码登录

《git快速入门 push/clone/reset/merge/切换分支全都有》

修改readme文档
添加修改记录日志文件 $ git add -A
注释修改了哪些地方 $ git commit -m”all”
然后提交 $ git push

(忘记修改哪里了?
$ git diff)
———
远程下载
打开要下载的文件夹,右键点击 git bash here
输入
$ git clone https://github.com/ytkah/learngit.git

———
版本回退
查看版本$ git log –pretty=oneline
回退一个版本$ git reset –hard HEAD^
回退两个版本$ git reset –hard HEAD^^
。。。
如果要回退几十个版本很麻烦,可以用
$ git reset –hard HEAD ab37336eeed(版本号的前几位,一般为7位)

回退完版本后,比较新的版本号看不到了怎么办?
$ git reflog

———–
分支
查看分支 $ git branch
创建分支 $ git branch aaa
切换分支 $ git checkout aaa/master
合并分支 $ git merge aaa
删除 $ git branch -d aaa(delete的缩写)

可以fork 别人的项目进行修改

———
保持更新(作者的项目更新了,你本地项目还没更新)
查看更新 $ git remote -v
添加作者源 $ git remote add upstream https://github.com/ytkah/learngit.git
更新 $ git fetch upstream
合并 $ git merge upstream/master

———

git常用的命令

usage: git [--version] [--help] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

The most commonly used git commands are:(用法比如:git add readme.txt)
   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty Git repository or reinitialize an existing one
   log        Show commit logs
   merge      Join two or more development histories together
   mv         Move or rename a file, a directory, or a symlink
   pull       Fetch from and merge with another repository or a local branch
   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG

'git help -a' and 'git help -g' lists available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

  

一般操作方式:

1本地修改文件

2 git add -A 注意A是大写

3 git commit -m”这里写修改了哪些内容”

4 git push origin master

如果出现错误,可以考虑用

:quit!

然后重新push

更多操作
https://v.qq.com/x/page/q05092u1lp7.html
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

    原文作者:ytkah
    原文地址: https://www.cnblogs.com/ytkah/p/git.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞