Git 学习:Git 和Heroku常用命令以及相关资料。
操作流程
日常使用git更新提交代码的一般流程是这样的:
- 在对代码进行了一些修改之后,使用:git add –all将本地所有新增文件添加进版本库。
- 使用:git commit -m 备注将代码提交到本地版本库。(备注内容没有空格的话不需要加引号)
- 使用:git pull origin从服务器拉取代码,更新本地版本库。
- 使用:git push origin将本地版本库推送到服务器。
例子
也可以用如下方法:
1 | git add -A |
如果是在origin master上,可以直接:
1 | git push -u origin master |
Heroku是这样的:
1 | git remote set-url origin https://git.heroku.com/AppName.git |
这里git pull是相当于是从远程获取最新版本并merge到本地
1 | $ git pull origin master |
获得某一个分支:
1 | git pull origin prefill02 |
加一整个文件夹
1 | $ git add folderName/* |
常用指令
Removing a remote
查看当前remote
View current remotes
1 | $ git remote –v |
Remove remote
1 | $ git remote rm destination |
Change a remote URL
1 | git remote set-url origin https://git.heroku.com/appName.git |
*拉取指定分支代码 *
1 | git clone -b develop XXX |
其中develop就是分支的名称
创建
创建并切换 branch
Create a new branch
1 | $ git checkout -b feature_branch_name |
仅仅切换 branch
1 | git checkout 分支名 |
创建 tag
1 | git tag 标签名 |
创建 tag 并备注(备注信息加不加双引号都可以)
1 | git tag -a 标签名 -m 备注信息 |
创建PGP tag 并备注
1 | git tag -s 标签名 -m 备注信息 |
文件
贮藏本地改变
经常会看到这个。If Your local changes to the following files would be overwritten by merge: Please stash them before you merge.
1 | git reset --hard |
*List Differences *
1 | git status |
List changes
1 | git diff |
退出:按q即可
Undo git add
Undo a git add - remove files staged for a git commit
1 | git reset filename.txt |
Will remove a file named filename.txt from the current index, the “about to be committed” area, without changing anything else.
删除
删除本地 branch / tag
1 | git branch -d 分支名或标签名 |
删除所有未推送的本地 branch
1 | git fetch -p |
仅仅删除某个远程 branch / tag
1 | git push origin :分支名或标签名 |
推送
推送某个 branch / tag
1 | git push origin 分支名或标签名 |
推送所有 branch
1 | git push --all origin |
推送所有 tag
1 | git push --tags |
回滚
1 | soft (默认) |
只回滚到某个commit,本地代码不变 (不加–soft或–hard默认为–soft)
1 | git reset --soft 分支名或标签名 |
hard: 彻底回滚(commit和本地代码都回滚)
1 | git reset --hard 分支名或标签名 |
下载、合并分支
合并某本地分支到当前分支
1 | git merge 分支名 |
合并某远程分支到当前分支 直接合并,慎用。
1 | git pull origin 远程分支名 |
下载
下载某个远程标签
1 | git fetch origin tag 远程标签名 |
合并分支
合并某本地分支到当前分支
1 | git merge 分支名 |
合并某远程分支到当前分支 直接合并
冲突解决,merge失败时很可能因对同一文件的同时更改,所以必须手动人工解决 取舍代码,确认冲突解决后需要把冲突文件 git add到staged area,运行git status查看所有状态确保无误后再git commit提交,注释好冲突为什么这么解决.
下载某个远程标签:
1 | git pull origin 远程分支名 |
1 | git fetch origin tag 远程标签名 |
合并分支查看远程分支
使用如下git命令查看所有远程分支:
1 | git branch -r |
拉取远程分支并创建本地分支
方式一
使用如下命令:
1 | git checkout -b 本地分支名x origin/远程分支名x |
使用该方式会在本地新建分支x,并自动切换到该本地分支x。采用此种方法建立的本地分支会和远程分支建立映射关系。
方式二
使用如下命令:
1 | git fetch origin 远程分支名x:本地分支名x |
or
git fetch origin 远程分支名x
使用该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动checkout。采用此种方法建立的本地分支不会和远程分支建立映射关系。
修改推送者名称
Changing Your Committer Name & Email Globally
You can run the “git config” command with the –global flag; this will make sure all of your future commits use the given information:
$ git config –global user.name “John Doe”
$ git config –global user.email “john@doe.org“
Changing Your Committer Name & Email per Repository
If you want to use special settings only when working in a certain repository, you can simply omit the –global flag. This makes the configuration valid only in that repository:
$ git config user.name “John Doe”
$ git config user.email “john@doe.org“
显示username信息
1 | git config --list |
设置git的账户名和邮箱
1 | $ git config --global user.name [username] |
返回主分支
1 | git checkout master |
If you want to rename a branch while pointed to any branch, do:
1 | git branch -m <oldname> <newname> |
处理冲突文件
Reference: https://confluence.atlassian.com/bitbucket/resolve-merge-conflicts-704414003.html
These steps include details for resolving conflicts between two branches in a Git repository. You’ll see references to these placeholders:
The directory to the forked repository as
The destination branch as
The source branch as
The file with resolved conflicts as
Collapse
- Make sure you’re in your repository directory.For example, if your repository name is my-repository, the result might look something like this:
1
$ cd ~/<repo_directory>
computer:$ cd ~/my-repository
computer:my-repository emmap$
- Pull the most recent version of the repository from Bitbucket.Checkout the source branch.
1
$ git pull
$ git checkout
- Pull the destination branch into the source branch. At this point, pulling the destination will try to merge it with the source and reveal all the conflicts.
1 | $ git pull origin <destination_branch> |
For example, if your destination branch is master, the result will look something like this:
1 | computer:my-repository emmap$ git pull origin master |
When you merge two branches with conflicts locally, you’ll get conflict markers in the file when you open your editor.
- Open the file to resolve the conflict. You can do this using the command line or you can navigate to the file.
The file will look something like this:
A. The beginning of the change in the HEAD branch. In this case, HEAD represents the active branch into which you’re merging.
B. The end of the change in the active branch and the beginning of the change in the non-active branch.
C. The end of the change in the non-active branch.
- Resolve the conflict by doing the following:
Remove the change designations added by Git (A, B, and C in the screenshot above).
Correct the content.
Save the file.
The result will look something like this:
- Add and commit the change.
1 | $ git add <filename> |
- Push the change to the remote.
1 | git push origin <feature_branch> |
When you check the pull request, the pull request will still be open and you’ll no longer see any merge conflicts.
其他
还原上一个版本的commit
1 | git reset --hard HEAD~1 |
删除文件夹【rm】
Linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可。 直接rm就可以了,不过要加两个参数-rf 即:
1
rm -rf 目录名字
git还原到之前某个版本,本地和远程都还原
命令行操作:
第一步: git log 查看之前的commit的id,找到想要还原的版本
第二步: git reset –hard 44bd896bb726be3d3815f1f25d738a9cd402a477 还原到之前的某个版本
第三步: git push -f origin master 强制push到远程
【uninstall npm modules in node js 】
1 | npm uninstall <name> removes the module from node_modules, but not package.json |
一键快速部署
1 | #Set the time |
表情符号
Inspired by dannyfritz/commit-message-emoji
See also gitmoji.
Commit type | Emoji |
---|---|
Initial commit | 🎉 :tada: :tada: |
Version tag | 🔖 :bookmark: :bookmark: |
New feature | ✨:sparkles: :sparkles: |
Bugfix | 🐛:bug: :bug: |
Metadata | 🔒 :card_index: :card_index: |
Documentation | 📚 :books: :books: |
Documenting source code | :bulb: :bulb: |
Performance | :racehorse: :racehorse: |
Cosmetic | :lipstick: :lipstick: |
Tests | 🚨 :rotating_light: :rotating_light: |
Adding a test | :white_check_mark: :white_check_mark: |
General update | :zap: :zap: |
Improve format/structure | :art: :art: |
Refactor code | ♻️:hammer: :hammer: |
Removing code/files | 🗑️:fire: :fire: |
Continuous Integration | :green_heart: :green_heart: |
Security | :lock: :lock: |
Upgrading dependencies | :arrow_up: :arrow_up: |
Downgrading dependencies | 💩 :arrow_down: :arrow_down: |
Lint | :shirt: :shirt: |
Translation | :alien: :alien: |
Text | :pencil: :pencil: |
Critical hotfix | :ambulance: :ambulance: |
Deploying stuff | :rocket: :rocket: |
Fixing on MacOS | :apple: :apple: |
Fixing on Linux | :penguin: :penguin: |
Fixing on Windows | :checkered_flag: :checkered_flag: |
Work in progress | 🚧:construction: :construction: |
Adding CI build system | :construction_worker: :construction_worker: |
Analytics or tracking code | :chart_with_upwards_trend: :chart_with_upwards_trend: |
Removing a dependency | :heavy_minus_sign: :heavy_minus_sign: |
Adding a dependency | :heavy_plus_sign: :heavy_plus_sign: |
Docker | :whale: :whale: |
Configuration files | :wrench: :wrench: |
Package.json in JS | :package: :package: |
Merging branches | :twisted_rightwards_arrows: :twisted_rightwards_arrows: |
Bad code / need improv. | :hankey: :hankey: |
Reverting changes | :rewind: :rewind: |
Breaking changes | :boom: :boom: |
Code review changes | :ok_hand: :ok_hand: |
Accessibility | :wheelchair: :wheelchair: |
Move/rename repository | :truck: :truck: |
Other | Be creative |
Github
1 | echo "# SCMP_SFDC_Pro_2" >> README.md |
Getting & Creating Projects
Command | Description |
---|---|
git init |
Initialize a local Git repository |
git clone ssh://git@github.com/[username]/[repository-name].git |
Create a local copy of a remote repository |
Basic Snapshotting
Command | Description |
---|---|
git status |
Check status |
git add [file-name.txt] |
Add a file to the staging area |
git add -A |
Add all new and changed files to the staging area |
git commit -m "[commit message]" |
Commit changes |
git rm -r [file-name.txt] |
Remove a file (or folder) |
Branching & Merging
Command | Description |
---|---|
git branch |
List branches (the asterisk denotes the current branch) |
git branch -a |
List all branches (local and remote) |
git branch [branch name] |
Create a new branch |
git branch -d [branch name] |
Delete a branch |
git push origin --delete [branchName] |
Delete a remote branch |
git checkout -b [branch name] |
Create a new branch and switch to it |
git checkout -b [branch name] origin/[branch name] |
Clone a remote branch and switch to it |
git checkout [branch name] |
Switch to a branch |
git checkout - |
Switch to the branch last checked out |
git checkout -- [file-name.txt] |
Discard changes to a file |
git merge [branch name] |
Merge a branch into the active branch |
git merge [source branch] [target branch] |
Merge a branch into a target branch |
git stash |
Stash changes in a dirty working directory |
git stash clear |
Remove all stashed entries |
Sharing & Updating Projects
Command | Description |
---|---|
git push origin [branch name] |
Push a branch to your remote repository |
git push -u origin [branch name] |
Push changes to remote repository (and remember the branch) |
git push |
Push changes to remote repository (remembered branch) |
git push origin --delete [branch name] |
Delete a remote branch |
git pull |
Update local repository to the newest commit |
git pull origin [branch name] |
Pull changes from remote repository |
git remote add origin ssh://git@github.com/[username]/[repository-name].git |
Add a remote repository |
git remote set-url origin ssh://git@github.com/[username]/[repository-name].git |
Set a repository’s origin branch to SSH |
Inspection & Comparison
Command | Description |
---|---|
git log |
View changes |
git log --summary |
View changes (detailed) |
git diff [source branch] [target branch] |
Preview changes before merging |