nicejade / jadeblog-backups

The use of Gitalk, record the visitor comments on the blog(晚晴幽草轩) content.
https://www.jeffjade.com
5 stars 1 forks source link

Git常用命令备忘 | 晚晴幽草轩 #84

Open nicejade opened 6 years ago

nicejade commented 6 years ago

https://jeffjade.com/2014/12/22/2014-12-22-gitmemo/

Git配置12345678910git config --global user.name "robbin" git config --global user.email "fankai@gmail.com"git config --global color.ui truegit config --global alias.co checkoutgit config --global alia

nicejade commented 6 years ago

Git 同时 push 到多个仓库(同步)

为防止一个git仓库由于各种原因造成无法访问,可以将代码push到多个仓库。编辑本地仓库目录下面的 .git 目录下的 config 文件(命令:vim ./git/config)。添加类如以下命令:

[remote "all"]
url = git@github.com:licess/licess.git
url = git@gitcafe.com:licess/licess.git

再 push 时,运行如下命令即可:

git push all master
nicejade commented 6 years ago

git 如何删除本地所有未提交的更改?

如果所有更改,没有加入暂存区的话,可以:

git checkout . && git clean -xdf

如果你有的修改以及加入暂存区的话,那么 :

git reset --hard 
git clean -xdf 
nicejade commented 6 years ago

Mac必备软件渐集之ZSH-终极Shell 一文的评论 issues37 中有强求的推荐使用 oh-my-zsh; 如果您使用了她,那么这份 git.plugin.zsh 配置, 对你来说,肯定也会是让你欣喜的礼物 ✅✔️✅。

nicejade commented 6 years ago

从存储库中删除敏感数据

git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "your-secret-email" ];
then
        GIT_AUTHOR_NAME="your-can-be-known-name";
        GIT_AUTHOR_EMAIL="your-can-be-known-email";
        GIT_COMMITTER_NAME="your-can-be-known-name";
        GIT_COMMITTER_EMAIL="your-can-be-known-email";
        git commit-tree "$@";
else
        git commit-tree "$@";
fi' HEAD

具体可参见:Removing sensitive data from a repository

nicejade commented 6 years ago

如何切换多个 GitHub 账号

Git 共有三个级别的 config文 件,分别是 system、global和 local。可以使用 git config --list | grep user (windows 需用 sls 替换 grep)来查看配置;当 git commit 时,Author 信息依次读取local、global和system的配置,如果找到则不再继续读取。其他配置的读取顺序也是如此。更改配置可运行:git config --local user.name your-name ,更多详情可查看 这里

nicejade commented 6 years ago

Problem:You have not concluded your merge (MERGE_HEAD exists) Solve:

git merge --abort [Since git version 1.7.4] git reset --merge [prior git versions]

nicejade commented 6 years ago

如何修改提交的 Message

nicejade commented 6 years ago

git stash用于保存和恢复工作进度

nicejade commented 6 years ago

如何删除远程分支

git push origin --delete branch-name
// eg:
git push origin --delete develop
nicejade commented 5 years ago

解决升级 Mac 系统后 git: error: unable to locate xcodebuild

git: error: unable to locate xcodebuild, please make sure the path to the Xcode folder is set correctly! git: error: You can set the path to the Xcode folder using /usr/bin/xcode-select -switch

解决办法如下:

xcode-select --install
sudo xcode-select --switch /Library/Developer/CommandLineTools/
nicejade commented 5 years ago

如何解决 .gitignore 不起作用问题

.gitignore 仅适用于未跟踪的文件。如果您正在跟踪,那么 .gitignore 将不适用;如果想暴力的解决,可以使用如下方法,予以修复:

git rm --cached -r .
git add .

更详细的答案,可参见: ignore-files-that-have-already-been-committed-to-a-git-repository

nicejade commented 5 years ago

remote add vs remote set-url

git remote add origin git@github.com:User/UserRepo.git

git remote add is used to a add a new remote.

git remote set-url origin git@github.com:User/UserRepo.git

git remote set-url is used to change the url of an existing remote repository.

nicejade commented 5 years ago

Git 创建干净的空分支

git checkout --orphan empty-branch
git rm -rf .
git commit --allow-empty -m "root commit"
git push origin empty-branch

更详细的内容,可以参见 Github create empty branch

nicejade commented 4 years ago

如何检查当前分支中没有什么要提交的?

手动更新代码(包括子模块),不利于有些项目自动化执行;而利用脚本执行,需要执行检查是否存在有需要提交的内容,否则会产生错误,导致脚本执行中断;如何检查当前分支中没有什么要提交的?下面这段代码可以参考:

#!/usr/bin/env sh

# 确保脚本抛出遇到的错误
set -e

# 更新项目下所用到的所有子模块 & 提交
git submodule foreach git pull

if [ -n "$(git status --porcelain)" ]; then
  git add tools
  git commit -m '📌 uodate tools submodule'
  git push
else
  echo "there no changes about submodule 🖥";
fi
nicejade commented 4 years ago

git shortlog -sn 命令可以列出代码仓库的提交者统计;参见 GitHub Protips: Tips, tricks, hacks, and secrets from Lee Reilly

nicejade commented 2 years ago

How to Clone a Specific Branch?

git clone --branch <branchname> <remote-repo-url>
#OR
git clone -b <branchname> <remote-repo-url>
nicejade commented 2 years ago

git 如何快速拉取远程分支到本地?

git fetch origin LOCAL_BRANCH_NAME  ROMOTE_BRANCH_NAME
#OR
git checkout -b LOCAL_BRANCH_NAME origin/ROMOTE_BRANCH_NAME