git baisc
1. generate ssh-key and copy public key to reposity
ssh-keygen [-t rsa] [-C "XXXX@XXXX.com"]
cat ~/.ssh/id_rsa.pub
copy public key to github or oschina
ssh -T git@git.oschina.net
if return below ,means sucessed!
Welcome to Git@OSC,your name!
2.clone and push
git clone git@git.oschina.net:wzw/demo.git
cd demo
ls -la
hello >world.txt
echo hello,world >world.txt
git status
git add .
git commit -m "hello,world"
git push origin master
$ git push <远程主机名> <本地分支名>:<远程分支名>
事实上 git push origin master 的意思是 git push origin master:master (,将本地的master分支推送到origin主机的master分支,如果没有就新建一个)
3. create and switch branch
创建分支git branch new_branch_name
切换分支 git checkout target_branch_name
删除分支是git branch -D target_branch_name
git checkout – file 可以丢弃工作区的修改
git checkout – file命令中的–很重要,没有–,就变成了“切换到另一个分支”的命令
如果已经提交到的暂存区,如何撤销?
在commit之前,你发现了这个问题。用git status查看一下,修改只是添加到了暂存区,还没有提交:
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#
Git同样告诉我们,用命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区:
git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。
再用git status查看一下,现在暂存区是干净的,工作区有修改:
git checkout – readme.txt
$ git status
# On branch master
nothing to commit (working directory clean)
cd demo
git branch
git branch new_branch
git branch
git checkout new_branch
git branch
echo newbranch>new_file.txt
ls
git add .
git commit -m "someting to new_branch"
git log
git checkout master
ls
4. push remote branch
git push origin local_branch_name:remote_branch_name
cd demo
ls
git branch
git checkout new_branch
git push origin new_branch:new_branch
5. merge
cd demo
git branch
git log
git checkout new_branch
git checkout master
echo somting >newfile2.txt
git add .
git commit -m "somtting to master"
git checkout new_branch
git merge master
git status
git log
6.tag
cd demo
git status
git tag v1.0
git tag
git push origin v1.0:v1.0
7.add remote repository
add one:
git remote add alias remote_repository_url
git remote add origin git@code.aliyun.com:huifeidexiaxia2017/QLExpress.git
add another
git remote set-url --add origin git@git.oschina.net:sywan/qlexpress.git
check the remote repository git 中的origin指的是远程主机的名字。
$ git remote -v
origin git@code.aliyun.com:huifeidexiaxia2017/QLExpress.git (fetch)
origin git@code.aliyun.com:huifeidexiaxia2017/QLExpress.git (push)
origin git@git.oschina.net:sywan/qlexpress.git (push)
cat ./git/config
[remote "origin"]
url = git@code.aliyun.com:huifeidexiaxia2017/QLExpress.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@git.oschina.net:sywan/qlexpress.git
push local repository to the remote
git push origin --all
if you spell the wrong url
git remote set-url origin URL
git remote set-branches [--add] <name> <branch>...
git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add <name> <newurl>
git remote set-url --delete <name> <url>
$ git remote set-url origin git@git.oschina.net:sywan/qlexpress.git
Administrator@luying-PC MINGW64 /e/Git/QLExpress/QLExpress (master)
$ git push origin --all
Counting objects: 138, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (98/98), done.
Writing objects: 100% (138/138), 50.84 KiB | 0 bytes/s, done.
Total 138 (delta 63), reused 0 (delta 0)
remote: Resolving deltas: 100% (63/63), completed with 45 local objects.
To git.oschina.net:sywan/qlexpress.git
* [new branch] 1.4.1 -> 1.4.1
* [new branch] 2.1.0 -> 2.1.0
* [new branch] 2.2.8 -> 2.2.8
* [new branch] 3.0.3 -> 3.0.3
* [new branch] trunk -> trunk
if appera the below wrong
...
! [rejected] master -> master (fetch first)
the hint means you mush fetch the remote project fisrt,you can “pull” first
if you don not need to pull ,you may use the flow:
git push origin --all -f
git config
- /etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’–system’ 给 git config,它将明确的读和写这个文件。
- ~/.gitconfig 文件 :具体到你的用户。你可以通过传递–global 选项使Git 读或写这个特定的文件。
- 位于git目录的config文件 (也就是 .git/config) :无论你当前在用的库是什么,特定指向该单一的库。每个级别重写前一个级别的值。因此,在.git/config中的值覆盖了在/etc/gitconfig中的同一个值。
二. 配置你的用户名和密码
当你安装Git后首先要做的事情是设置你的用户名称和e-mail地址。这是非常重要的,因为每次Git提交都会使用该信息。它被永远的嵌入到了你的提交中:
$ git config --global user.name "wirelessqa"
$ git config --global user.email wirelessqa.me@gmail.com
五.检查你的配置
如果你想检查你的设置,你可以使用 git config --list 命令来列出Git可以在该处找到的所有的设置:
$ git config --list
user.name=wirelessqa
user.email=wirelessqa.me@gmail.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
版本回退
git log
git reflog
可以查看版本的id
git reset --hard HEAD 当前版本
git reset --hard HEAD^ 当前版本的上一个版本
git reset --hard HEAD^^当前版本的上上个版本
git reset --hard HEAD~2 与上一条等价
也可以git reset --hard commitid
之后再推送到远程仓库即可。
查看所有分支,远程分支标红,当前分去用*
$ git branch -a
confict
* feature/dev
master
remotes/origin/feature/dev
remotes/origin/master
删除远程仓库
$ git push <远程主机名> <本地分支名>:<远程分支名>
如果把本地分支名设置为空,则会删除远程分支
本地仓库 执行 git push origin :{远程分支名}
如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程master分支上,即就是删除远程的master分支。
$ git push origin :master
等同于
$ git push origin --delete master