####1.解决Git refusing to merge unrelated histories
在git pull origin master后面跟上参数–allow-unrelated-histories
(git pull origin master --allow-unrelated-histories)
####2.关联项目github
git remote add origin (github地址)
####3.强制push
git push -u origin master -f
####4.将本地的分支传到远端
git push (远端名称) (本地分支名称)
git push origin xxxxxxx
####如何删除github上的远程分支
命令: git push origin 【空格】【冒号】【你的分支名字】
比如我github上有master和develop分支,我现在想着删除develop分支,命令如下:
git push origin :develop
####commit提交错误,想要重新commit
将上次提交之后修改的代码全部删除了(慎用)
回退最近一次提交的原始状态 git reset --hard HEAD
####git 删除 错误 提交的 commit
根据--soft --mixed --hard,会对working tree和index和HEAD进行重置:
git reset --mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
git reset --soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
git reset --hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容,此命令 慎用!
HEAD 最近一个提交
HEAD^ 上一次提交
HEAD^ ^ 上一次的 上一次的提交(倒数第三次)
HEAD^^^ 倒数 第四次的 提交
HEAD~0 最近一个提交
HEAD~1 上一次提交
HEAD^2 上一次的 上一次的提交(倒数第三次)
HEAD^3 倒数 第四次的 提交
git push -f origin ${branch_name}
####合并分支
基于 master 分支的紧急问题分支 hotfix branch
你可以运行你的测试,确保你的修改是正确的,然后将其合并回你的 master 分支来部署到线上。 你可以使用 git merge 命令来达到上述目的:
git checkout master
git merge hotfix
####5.Android Studio 的忽略不管用
git rm -r --cached .
git add .
git commit -m "update .gitignore"
####6.简化提交
git commit -a 相当于运行 git add 把所有当前目录下的文件加入暂存区域再运行。git commit
####7.Android Studio忽略文件.gitignore
#built application files
*.apk
*.ap_
# files for the dex VM
*.dex
# Java class files
*.class
# generated files
bin/
gen/
out/
build/
# Local configuration file (sdk path, etc)
local.properties
# Windows thumbnail db
Thumbs.db
# OSX files
.DS_Store
# Eclipse project files
.classpath
.project
# Android Studio
*.iml
.idea/
.settings/
gradle/
# Local IDEA workspace
# Gradle cache
.gradle/
#NDK
obj/
####提交连环套
git status
git add .
git commit -m "xxx"
git pull
git push origin xxx
Git的优势是可以创建不同的branch,然后在每个branch上开发。那么问题是:如果不同的branch之间需要做同步,比如sourceBranch上做的修改也需要同步到targetBranch,改怎么做?
####1. 如果一个branch是有远程Git server管理的,另一个branch是自己本地的
cd <your workspace>
git branch //假定现在所在的branch是targetBranch,并最好保证没有未提交的修改,并且已经更新到最新
git checkout -b sourceBranch //创建一个本地的sourceBranch并切换到sourceBranch
git commit //把sourceBranch上做的修改先提交
git checkout targetBranch //切换回targetBranch
git merge --no-ff sourceBranch //把sourceBranch的修改merge到targetBranch。注意:建议merge的时候总是用 --no-ff 选项
git status //保证现在workspace是干净的
git push //push到远程,如果远程有新的修改,先做一下git pull
####2. 如果两个branch都是远程管理的,想把branchB的内容同步到branchA上
cd <your workspace>
git branch //假定现在所在的branch是branchA,并最好保证没有未提交的修改,并且已经更新到最新
git checkout sourceBranch //确保同一个workspace能在不同的branch直接切换,即保证 .git/config里 [remote "origin"] 的内容是 fetch = +refs/heads/*:refs/remotes/origin/*
git merge targetBranch
解决conflicts如果merge的结果里有显示conflicts
git commit //解决冲突后先commit到sourceBranch
git checkout targetBranch //切换到targetBranch
git merge --no-ff sourceBranch //建议merge的时候总是用 --no-ff 选项
git push origin targetBranch //把sourceBranch的修改merge到targetBranch之后,push到远程的targetBranch
####git 一个分支完全覆盖另一个分支
1,远程
git push origin develop:master -f
就可以把本地的develop分支强制(-f)推送到远程master
2,本地
git checkout master // 切换到旧的分支
git reset --hard develop // 将本地的旧分支 master 重置成 develop
git push origin master --force // 再推送到远程仓库
注释: origin 远程仓库名, master 分支名,force,意为:强行、强制。
这行命令的意思就是强制用本地的代码去覆盖掉远程仓库的代码,敲 git push --help 可查看官方的解释(英文的)。当然不止这一种操作方式了,但是这种操作是最快速的,不会有冲突什么的,当然我也有一个忠告:请谨慎使用!请谨慎使用!请谨慎使用!
####Android.gitignore
备份:https://github.com/github/gitignore/blob/master/Android.gitignore
# Built application files
*.apk
*.ap_
# Files for the ART/Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
out/
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
# Android Studio Navigation editor temp files
.navigation/
# Android Studio captures folder
captures/
# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/dictionaries
.idea/libraries
# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
#*.jks
# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
# Google Services (e.g. APIs or Firebase)
google-services.json
# Freeline
freeline.py
freeline/
freeline_project_description.json
# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md