Git 入门指南(一):https://developer.aliyun.com/article/1391863
撤销
恢复暂存区的指定文件到工作区
git checkout [file]
恢复某个commit的指定文件到暂存区和工作区
git checkout [commit] [file]
恢复暂存区的所有文件到工作区
git checkout .
重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset [file]
重置暂存区与工作区,与上一次commit保持一致
git reset --hard
回滚到指定版本
rm -rf .git git init git add . git commit -m 'git init first commit' git remote add origin <github_repo_url> git push -f -u origin master
添加远程仓库
cd existing_folder git init git remote add origin git@git.sg-ai.com:ywguo/Test.git git add . git commit -m "Initial commit" git push -u origin master
Stashing
储藏
储藏当前工作区
git stash
查看储藏状态
git stash list
恢复储藏工作区状态
git stash apply
如何创建公钥
- 首先启动一个Git Bash窗口(非Windows用户直接打开终端)
- 执行:
cd ~/.ssh
如果返回“… No such file or directory”,说明没有生成过SSH Key,直接进入第4步 否则进入第3步备份 - 备份:
mkdir key_backup mv id_isa* key_backup
- 生成新的Key:(引号内的内容替换为你自己的邮箱)
ssh-keygen -t rsa -C "your_email@youremail.com"
输出显示:
>Generating public/private rsa key pair. Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):<press enter>
直接回车,不要修改默认路劲。
>Enter passphrase (empty for no passphrase):<enter a passphrase> Enter same passphrase again:<enter passphrase again>
设置一个密码短语,在每次远程操作之前会要求输入密码短语!闲麻烦可以直接回车,不设置。
- 成功:
Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa. Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub. The key fingerprint is: ... ...
- 提交公钥:
6.1 找到.ssh文件夹,用文本编辑器打开“id_rsa.pub”文件,复制内容到剪贴板。
6.2 打开 github.com/settings/ss… ,点击 Add SSH Key 按钮,粘贴进去保存即可。
git设置用户名密码
设置 git 用户名/邮箱
git config --global user.name [username] git config --global user.email [email]
但是这个仅仅是设置用户名密码,如果你的Git 源每次操作需要你输入用户名/密码验证,你依然需要每次设置,那么该如何办呢?
git 保存用户名密码
这里主要是配置一个 config 项
有两个方法,基本上原理都是一样,都是修改 .git/config 文件
1.使用如下命令,修改 config 文件即可保存
echo "[credential]" >> .git/config echo " helper = store" >> .git/config
2.直接修改 .git/config 文件
在 Linux/mac 下可以直接使用 vim 工具修改 config 文件
ubuntu@VM-7-212-ubuntu:~/kernel-code/kernel-netfilter-sample-code$ vim .git/config ##修改成如下 [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = https://github.com/Miss-you/kernel-netfilter-sample-code.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master ##主要是后面这两行,如果不想保存,则删除即可 [credential] helper = store ##保存
这样就可以保存用户名密码,不用每次都输入了!
git config
查看配置
使用 git config --list
查看已设配置
feiqianyousadeMacBook-Pro:xt_GTPU yousa$ git config --list core.excludesfile=/Users/yousa/.gitignore_global user.name=Miss-you user.email=snowfly1993@gmail.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.ignorecase=true core.precomposeunicode=true remote.origin.url=https://github.com/Miss-you/xt_GTPU.git remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* branch.master.remote=origin branch.master.merge=refs/heads/master
Git 修改默认编辑器为 vim
git config --global core.editor vim
操作规范
提交到仓库 description
需要有意义命名
git commit -m [description]
分支请及时合并清理
git merge [branch] git push [remote] [branch] git branch -d [branch-name] git push origin --delete [branch-name]
- 友情提示: 目前只是现在工作中用到的命令整理,如果没有您需要的,请自行 google
- 文档持续更新中,欢迎大家拍砖 ...