git registration and basic commands

Register an account and create a warehouse

To use github first step of course is a registered github account. Then you can create a simple tutorial warehouse (free users can only build a public warehouse), Create a New Repository, fill it out name Create, then there will be a number of warehouse configuration information, which is of a git. Proceed as follows:

1. Open https://github.com  registered directly on the home page

2. After successful registration point of the button, select the "+" on the top of the page User Menu -> New repository to create a new warehouse

 As a remote respository a name, description (for remote warehouse to do a simple description), click on the Create button warehouse you will successfully created a warehouse

Install git client

github is server-side, in order to use on their computers, we also need a git git client,

The Internet to download the installation package is good, all the way next, the installation is successful, return to the C drive, or any folder, right-click the mouse will be more than some of the menu
such as Git Init Hear, Git Bash, Git Gui, the installation was successful.

Configuration Git

Let's find a place in the computer's hard drive to store local repository, such as our local warehouse built on the d: \ git folder

Git folder into the right-mouse the following steps:

1. In the local warehouse right choice Git Init Here, it will be more a .git folder, which represents the local git created successfully. Right into the git Git Bash command line, capture effect is as follows:

For insurance purposes, we first perform git init command

$ Git init # directory management

2. Create ssh key locally

$ ssh-keygen -t rsa -C "[email protected]"

Back [email protected] changed your mailbox. My mail is [email protected], also registered on the github-mail:

$ ssh-keygen -t rsa -C "[email protected]"

Enter the direct point, description will generate ssh key on the default file id_rsa. 

If you have generated ssh key, execute the following command to find

Copy the code
63298@DESKTOP-H891TLB MINGW64 /d/git (master)
$ cd ~

63298@DESKTOP-H891TLB MINGW64 ~
$ pwd
/c/Users/63298

63298@DESKTOP-H891TLB MINGW64 ~
$ ls -la
63298@DESKTOP-H891TLB MINGW64 ~
$ cd .ssh

63298@DESKTOP-H891TLB MINGW64 ~/.ssh
$ ls
id_rsa  id_rsa.pub  known_hosts

63298@DESKTOP-H891TLB MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDliYIwmfzlevLOhS1hBkawsgk6w4eqMnoJ4tXRlEr0Ds4LYzKTJzjfBfwzPfvDvuySr82f6Ycg+vb/PU2gB9bg++dfodFYUXbh9XpiU17rI/H2KvkywTUhf8pK7Hgqyjp9bGozOhUY/IoQdGLxjYRqDWuvWD8QdGmuYplrbjUP0xWJdD/1/Hj9iz9r/wCoCmaXRaVIviMpkFSyG590+o1VCg+lb5TIQ/bh/KF4KM802S0t7dPZJd1z+c+bVRiZm5OADxm/0rxsFgiys8mXzB3AONF8AxtF52ahp+f88aYYIG6fv7GKIo24NhYv9f0fkGk9NIVk
Copy the code

Is then asked to enter a password, press enter directly indicates no password

Repeated password is also directly enter, then prompt you to shh key has been generated successfully.

And then view the ssh key file address we enter the prompt. My computer's address is C: \ Users \ 63298 \ .ssh, where 63298 is the name of my computer

3. Open id_rsa.pub, copy inside the key. Inside the key is alphanumeric-one pairs do not understand, do not ignore it, directly copied.

Back github website, enter Account Settings, select the left side of SSH Keys, Add SSH Key,

4. Verify successful, enter at git bash

$ ssh -T [email protected]

Enter you will see: You've successfully authenticated, but GitHub does not provide shell access. This means that has been successfully connected to the github.

5. Next we need to do is go to a local warehouse spread to github, also you need to set the username and email Prior to this, because every time github commit them will be recorded

$ git config --global user.name "your name"
$ git config --global user.email "[email protected]"

6. Go to upload warehouse, associated with the remote warehouse

Execute: $ git the Add Remote Linux  [email protected]: tbxk / linux.git

Copy the code
git remote does not respond, indicating no correlation remote repository 

Linux: the name of the remote repository 

ssh Address: [email protected]: tbxk / linux.git 

re-execute remote, returns associated with the success of Linux instructions
Copy the code

After completion of entering the .git, open the config, there will be more of a content, which is the remote address just added, can also be modified directly config to configure the remote address.

Upload submitted

1) Next, add some files to a local warehouse

vi  book.txt

Then enter the following command at the command line

Git the Add book.txt $ 
$ git the commit -m "first submitted to the local repository"

2)上传到github 

本地仓库的一个master和远程的linux分支建立连接,第一次链接要加-u

尽量本地仓库尽量和远程仓库的名字保持一致,不然需要一个命令将他们链接,麻烦

 

 

git push命令会将本地仓库推送到远程服务器。

git pull命令则相反。

注:首次提交,先git pull下,修改完代码后,使用git status可以查看文件的差别,使用git add 添加要commit的文件。

大功告成,现在你知道如何将本地的项目提交到github上了。

gitignore文件

.gitignore顾名思义就是告诉git需要忽略的文件,这是一个很重要并且很实用的文件。一般我们写完代码后会执行编译、调试等操作,这期间会产生很多中间文件和可执行文件,这些都不是代码文件,是不需要git来管理的。我们在git status的时候会看到很多这样的文件,如果用git add -A来添加的话会把他们都加进去,而手动一个个添加的话也太麻烦了。这时我们就需要.gitignore了。

 
Git命令

查看、添加、提交、删除、找回,重置修改文件

Copy the code
git help <command> # 显示command的help

git show # 显示某次提交的内容 git show $id

git co -- <file> # 抛弃工作区修改

git co . # 抛弃工作区修改

git add <file> # 将工作文件修改提交到本地暂存区

git add . # 将所有修改过的工作文件提交暂存区

git rm <file> # 从版本库中删除文件

git rm <file> --cached # 从版本库中删除文件,但不删除文件

git reset <file> # 从暂存区恢复到工作文件

git reset -- . # 从暂存区恢复到工作文件

git reset --hard # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改

git ci <file> git ci . git ci -a # 将git add, git rm和git ci等操作都合并在一起做                                    git ci -am "some comments"

git ci --amend # 修改最后一次提交记录

git revert <$id> # 恢复某次提交的状态,恢复动作本身也创建次提交对象

git revert HEAD # 恢复最后一次提交的状态
Copy the code

查看文件diff

Copy the code
git diff <file> # 比较当前文件和暂存区文件差异 git diff

git diff <id1><id2> # 比较两次提交之间的差异

git diff <branch1>..<branch2> # 在两个分支之间比较

git diff --staged # 比较暂存区和版本库差异

git diff --cached # 比较暂存区和版本库差异

git diff --stat # 仅仅比较统计信息
Copy the code

查看提交记录

Copy the code
git log git log <file> # 查看该文件每次提交记录

git log -p <file> # 查看每次详细修改内容的diff

git log -p -2 # 查看最近两次详细修改内容的diff

git log --stat #查看提交统计信息
Copy the code

tig
Mac上可以使用tig代替diff和log,brew install tig

Git 本地分支管理
查看、切换、创建和删除分支

Copy the code
git br -r # 查看远程分支

git br <new_branch> # 创建新的分支

git br -v # 查看各个分支最后提交信息

git br --merged # 查看已经被合并到当前分支的分支

git br --no-merged # 查看尚未被合并到当前分支的分支

git co <branch> # 切换到某个分支

git co -b <new_branch> # 创建新的分支,并且切换过去

git co -b <new_branch> <branch> # 基于branch创建新的new_branch

git co $id # 把某次历史提交记录checkout出来,但无分支信息,切换到其他分支会自动删除

git co $id -b <new_branch> # 把某次历史提交记录checkout出来,创建成一个分支

git br -d <branch> # 删除某个分支

git br -D <branch> # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)
Copy the code

 分支合并和rebase

git merge <branch> # 将branch分支合并到当前分支

git merge origin/master --no-ff # 不要Fast-Foward合并,这样可以生成merge提交

git rebase master <branch> # 将master rebase到branch,相当于: git co <branch> && git rebase master && git co master && git merge <branch>

 Git补丁管理(方便在多台机器上开发同步时用)

git diff > ../sync.patch # 生成补丁

git apply ../sync.patch # 打补丁

git apply --check ../sync.patch #测试补丁能否成功

 Git暂存管理

Copy the code
git stash # 暂存

git stash list # 列所有stash

git stash apply # 恢复暂存的内容

git stash drop # 删除暂存区

Git远程分支管理

git pull # 抓取远程仓库所有分支更新并合并到本地

git pull --no-ff # 抓取远程仓库所有分支更新并合并到本地,不要快进合并

git fetch origin # 抓取远程仓库更新

git merge origin/master # 将远程主分支合并到本地当前分支

git co --track origin/branch # 跟踪某个远程分支创建相应的本地分支

git co -b <local_branch> origin/<remote_branch> # 基于远程分支创建本地分支,功能同上
Copy the code

git push # push所有分支

Copy the code
git push origin master # 将本地主分支推到远程主分支

git push -u origin master # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)

git push origin <local_branch> # 创建远程分支, origin是远程仓库名

git push origin <local_branch>:<remote_branch> # 创建远程分支

git push origin :<remote_branch> #先删除本地分支(git br -d <branch>),然后再push删除远程分支
Copy the code

Git远程仓库管理

Copy the code
git remote -v # 查看远程服务器地址和仓库名称

git remote show origin # 查看远程服务器仓库状态

git remote add origin git@ github:robbin/robbin_site.git # 添加远程仓库地址

git remote set-url origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址(用于修改远程仓库地址) git remote rm <repository> # 删除远程仓库
Copy the code

创建远程仓库

Copy the code
git clone --bare robbin_site robbin_site.git # 用带版本的项目创建纯版本仓库

scp -r my_project.git git@ git.csdn.net:~ # 将纯仓库上传到服务器上

mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在服务器创建纯仓库

git remote add origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址

git push -u origin master # 客户端首次提交

git push -u origin develop # 首次将本地develop分支提交到远程develop分支,并且track

git remote set-head origin master # 设置远程仓库的HEAD指向master分支
Copy the code

也可以命令设置跟踪远程库和本地库

git branch --set-upstream master origin/master

git branch --set-upstream develop origin/develop

使用git基本流程

管理工作目录

-- mkdir xxx
-- cd xxxx
-- git init (.git)  # 管理工作目录,有.git文件,说明管理成功
-- git status # 查看工作树状态
-- git log # 查看提交记录
-- git reflog # 查看之前所有的操作记录

Git 三个状态切换

Copy the code
工作区   暂存区  本地仓库
    -- git add ./<filename>  # 工作区到暂存区
    -- git commit -m ""   # 暂存区到本地仓库
    -- git checkout ./<filename>  # 丢弃工作区的内容
    -- git reset ./<filename>  # 从暂存区退到工作区
    -- git reset --hard/--soft <commit_id>  # 版本的回退  
            -- hard # 丢弃内容
            -- soft #  把内容放入暂存区
Copy the code

分支管理

Copy the code
-- git branch <name> # 创建分支
-- git branch# 查看分支
-- git branch -d <name> # 删除分支
-- git checkout <name> # 切换分支
-- git merge <name> # 将name分支合并到当前分支
-- 冲突只能手动解决
-- 解决完冲突记得要提交
Copy the code

标签管理

-- git tag <name> <commit_id> # 给指定的版本加标签
-- git tag # 查看标签
-- git tag -d <name> # 删除标签

远程仓库建立连接

Copy the code
-- SSH 
-- 1 本地生成公钥私钥
在主用户的根目录下
-- 2 把公钥放入GitHub中
-- 建立连接
git remote add "远程仓库的别名" "远程仓库的地址https/ssh"
-- 查看所有的远程仓库
git remote
-- 向远程仓库提交代码
git push -u 远程仓库别名 分支名
注意 -u 第一次提交代码的时候本地分支跟远程仓库的分支建立起连接
-- 从远程仓库拉代码
git pull 远程仓库别名 分支名
-- 远程仓库的代码跟你最后push的代码有变化的时候 push会失败
-- 先从远程仓库拉取代码
-- 手动解决冲突
-- 提交到本地仓库
-- git push ....
Copy the code

公司应用流程

Copy the code
-- 拉取自己分支的代码
-- 提交到测试分支
-- 提交到远程仓库自己的分支
-- 提交合并请求
-- 由领导把你的分支合并到master
-- 提交到保险分支
-- 提交线上分支
当线上出bug
-- 在master分支建立bug分支
-- 在线上分支版本回退 
-- 在bug分支 解决bug
rebase 合并提交记录 保持记录的整洁性
Copy the code

Cloning project
- git clone address

Guess you like

Origin www.cnblogs.com/daofaziran/p/11572408.html