記在小本本上的 git 操做

記在小本本上的 git 操做

標籤: gitgit

  • 查看 git 配置信息github

$ git config --list
or 
$ git config -l
  • 設置 git 配置信息shell

    • 全局設置bash

    $ git config --global user.name "{{userName}}"
    $ git config --global user.email "{{userEmail}}"
    • 修改當前項目的 git 配置信息 <!--more-->url

    $ ls -a
    $ cd .git/
    $ vi config 
    add  
    [user]
       name = {{userName}}
       email = {{userEmail}}
    
    or
    $ git config user.name "{{userName}}"
    $ git config user.email "{{userEmail}}"
  • 查看分支版本控制

$ git branch 查看本地分支
$ git branch -r 查看遠程分支
$ git branch -a 查看全部分支
  • 建立一個新分支code

$ git checkout -b {{branch}}
  • 切換遠程分支orm

$ git checkout -b {{branch}} origin/{{branch}}
  • 刪除分支ip

$ git branch -D {{loaclBranch}}
$ git push --delete origin {{remoteBranch}}
  • 合併分支rem

$ git merge {{branch}} 將 branch 與當前分支合併
  • 查看 commit head message

$ git reflog
  • 打 tag

$ git tag {{tagName}}
$ git push origin {{tagName}}
or 
$ git push --tags
  • 刪除 tag

$ git tag -d {{localTag}}

$ git tag -d {{remoteTag}}
$ git push origin :refs/tags/{{remoteTag}}
  • 給一個歷史提交添加 tag

// Set the HEAD to the old commit that we want to tag
$ git checkout {{leading 7 chars of commit}}

// temporarily set the date to the date of the HEAD commit, and add the tag
$ GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a {{tag}} -m "{{commit message}}"

// set HEAD back to whatever you want it to be
$ git checkout master

$ git push --tags
  • push 後發現沒加 .gitignore,刪除本地及遠程的冗餘提交

$ git rm -r --cached .
$ git add .
$ git commit -m "{{commit message}}"
  • 修改已經 commit 的郵箱和用戶名

// get the commit we want to modify
$ git log 

// go to the commit
$ git reset --soft {{commitId}}

$ git commit --amend --author='{{userName}}<{{userEmail}}>'
$ git push
  • 修改已經 push 的 commit 的郵箱和用戶名

// clone a new repo 
git clone --bare https://github.com/user/repo.git
cd repo.git
// copy the script below and modify the variables: OLD_EMAIL, CORRECT_NAME, CORRECT_EMAIL

#! /bin/sh

git filter-branch --env-filter '

OLD_EMAIL=""
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
// then press enter to run the script

// checkout if there any error in new git and push 
$ git push --force --tags origin 'refs/heads/*'

// delete the temporary clone
$ cd ..
$ rm -rf repo.git
  • 修改已經 push 的 commit message

方法一:
$ git commit --amend 
$ git push --force

方法二:
$ git rebase -i HEAD~n

change the `pick` to `reword`, which means edit the commit message

save and exit and then update the commit message as you like and :wq

$ git push --f
  • 修改已經 push 的提交內容

git reset --hard <commit_id>

git add .

git commit --amend

git push origin HEAD --force
  • 解決提交前的 conflict,協同工做必備

// 先不提交修改的內容直接建立新分支 temp ,若已 commit 則建立新分支後在本分支 reset 到上一個 commit ,再執行 git pull

$ git checkout -b temp 

$ git add .

$ git commit -m 'commit message'

// 此時的master 分支是乾淨的,無本身的提交
$ git checkout master

// 拉取遠程修改
$ git pull

// 複製剛剛提交的 commit-id  commit-a, 將本身的提交 cherry-pick 進去
$ git cherry-pick commit-a

// 若是有衝突 fix it
$ git add .

$ git cherry-pick --continue

// then push
$ git push
  • gerrit merge 前執行 git commit --amend 改寫上次提交,並將 gerrit 上的 changeid 加入到上次提交的 commit message 下面(上下各空一行),再 git push(仍是原來的 commit,不產生新的提交)

  • bash ctrl+R 快速查看輸入過的命令

  • git 建立倉庫並關聯到 github

    • cd 到項目目錄,git init 初始化,使 git 對此項目進行版本控制

    • git add .

    • git commit -m ''

    • github create a new repository

    • 將本地項目關聯到 github 上 git remote add origin {{url}}

    • git push -u origin master

  • Github Pages 發佈靜態頁面

    • repository-Settings-Github Pages-select a source and save

相關文章
相關標籤/搜索