git clone:
如果我们从github上复制HTTPS clone URL,然后 git clone默认是checkout主分支,如果需要切换分支:
git clone https://github.com/emlid/linux-rt-rpi
git checkout [按 TAB 键] //看到你所要的分支,然后输入就行
或者
git checkout -b [分支的名字] //新建一个分支然后checkout
git checkout -f [其他分支] // 丢弃本分支修改,相当于:git reset --hard HEAD 然后再 checkout
如果只需要clone某个分支(省时间和空间)则可以:
git clone -b branch_name https://github.com/emlid/linux-rt-rpi //只克隆 branch_name这个分支
// 可以结合 --depth <depth>
//Create a shallow clone with a history truncated to the specified number of revisions.
如果是clone 包含子项目目录:
// With version 1.6.5 of Git and later, you can use:
git clone --recursive git://github.com/foo/bar.git
cd bar
// For already cloned repos, or older Git versions, just use:
git clone git://github.com/foo/bar.git
cd bar
git submodule update --init --recursive
如果需要多个管理多个仓库,分别使用不同的ssh-key
新建ssh-key(将xxx替换为你想要的名字):
ssh-keygen -t rsa -f ~/.ssh/xxx_rsa -C "Key for another repo"
// 或者默认:
ssh-keygen -t rsa
在$HOME/.ssh
目录下新建config
文件,添加如下内容( 这里新建一个配置文件,使得 xxx.host_name.xxx 这样的网站使用 xxx_rsa 和 xxx_rsa.pub,而不是默认的 id_rsa和 id_rsa.pub, ):
Host *.host_name.* // 如 *.github.* 或者其他仓库的host_name
User xxx //用户名,不用User只写Host也行,关于如何得到host 或者 user 名字,请往下看
IdentityFile ~/.ssh/xxx_rsa // 这个很关键
Host 和 User 可以根据使用clone的仓库的地址得到,其中 ssh、git、https 有点微小区别:
ssh://[user@]host.xz[:port]/path/to/repo.git/
git://host.xz[:port]/path/to/repo.git/
http[s]://host.xz[:port]/path/to/repo.git/
ftp[s]://host.xz[:port]/path/to/repo.git/
rsync://host.xz/path/to/repo.git/
git commit -a --amend
如果需要修改最新的一次commint信息,如下:
git commit --amend #即用当前的commit去替换上一次的commit.
记住,--amend
参数最好只在你本地有了新的 commit 之后,并且是 push 之前操作!如果你已经 push
了(即同步了服务器跟本地代码),然后你再运行 git commit --amend
,新的 --amend
会用一个新的提交 替换 服务器上之前的提交(如果这个服务器上的最新的commit是别人提交的,那别人的这个提交记录将不复存在。。。),而在服务器上看来就是你与本地对应的最新commit 消失了(因为已经被替换了)——这会导致一些问题,不要这么做!!
如果你执意要这么做, git push -f
可以实现这个操作。但是,请确保这时的服务器上的最新提交是你的commit,不是其他人的。
Amending doesn’t just alter the most recent commit—it replaces it entirely, so Don’t Amend Public Commits ( amend 自己在同步了服务器上的代码之后,本地的新的 commit (还没有push) 是可以的).
或者使用下述命令:
git commit -a --amend -C HEAD // 用当前的提交替换上一次提交(添加修改,并保持上一次的提交信息)
-a // 自动 stash 修改和删除操作,不会自动commit新加入的文件。相当于: git add -u
--amend // 覆盖上一次commit记录
-C // 添加commit注释,并保持上一次的提交注释信息
如果你上一次的commit已经push到服务器了,然后你又需要提交这次 --amend
的commit,如下:
git push <remote> <branch> --force
# Or
git push <remote> <branch> -f // 强制提交,覆盖服务器上的记录。