跳转至

Git笔记

常用命令

git clone 仓库地址

git pull 仓库地址

git rm -r myFolder 从git删除文件夹

git rm --cached -r myFolder 从本地git中删除某个文件夹

git rm --cached myfile 从本地git中删除某个文件

git checkout . 放弃当前未提交的修改文件

https://{username}:{passwd}@git.coding.net 在Uri地址中存入git的账户密码之后执行 git config --global credential.helper store

git remote set-url origin https://git.coding.net/ykh/Blog.git重新设置仓库地址

git log --all --decorate --oneline --graph # 显示git log 移除--decorate可不显示分支信息

.gitignore文件作用

创建.gitignore文件,文件用于告诉git忽略某些特定或者特定文件类型的文件:

*.[oa]                  #忽略所有.o和.a类型的文件
*.log                   #忽略所有.log类型的文件
!info.log               #不忽略info.log文件
*~                      #忽略所有文件名称以~结尾的文件
media/                  #忽略所有的media/文件夹
migrations/*.py         #忽略所有migrations/文件夹下的.py文件
!migrations/__init__.py #不忽略所有migrations/文件夹下的__init__.py Django项目多端同步的时候极为有用
/environment            #忽略根目录下的environment文件夹

常见错误

冲突

本地版本低于远程版本,则先从远程服务器拉取代码,在本地合并完成后再次提交

error: path 'signals.py' is unmerged

git reset signals.py
git checkout signals.py

命令可使用git Bash、SourceTree、PyCharm等工具执行

python3获取git log信息脚本

Terminal中输入git log | git_parser.py即可执行脚本,git_parser.py文件:

import re
import io
import sys
import datetime

# array to store dict of commit data
commits = []


def parse_commit(commit_lines):
    # dict to store commit data
    commit = {}
    # iterate lines and save
    for commit_line in commit_lines:
        if commit_line == '' or commit_line == '\n':
            # ignore empty lines
            pass
        elif bool(re.match('commit', commit_line, re.IGNORECASE)):
            # commit xxxx
            if len(commit) != 0:  # new commit, so re-initialize
                commits.append(commit)
            commit = {'hash': re.match('commit (.*)', commit_line, re.IGNORECASE).group(1)}
        elif bool(re.match('merge:', commit_line, re.IGNORECASE)):
            # Merge: xxxx xxxx
            pass
        elif bool(re.match('author:', commit_line, re.IGNORECASE)):
            # Author: xxxx <xxxx@xxxx.com>
            m = re.compile('Author: (.*) <(.*)>').match(commit_line)
            commit['author'] = m.group(1)
            commit['email'] = m.group(2)
        elif bool(re.match('date:', commit_line, re.IGNORECASE)):
            # Date: xxx
            m = re.compile('Date:   (.*)').match(commit_line)
            date = m.group(1)
            commit['date'] = datetime.datetime.strptime(date, '%a %b %d %H:%M:%S %Y %z').strftime('%Y-%m-%d %H:%M:%S')
        elif bool(re.match('    ', commit_line, re.IGNORECASE)):
            # (4 empty spaces)
            if commit.get('message') is None:
                commit['message'] = commit_line.strip()
        else:
            print('ERROR: Unexpected Line: ' + commit_line)


if __name__ == '__main__':
    input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
    parse_commit(input_stream.readlines())
    # print commits
    print('Author'.ljust(8) + '  ' + 'Email'.ljust(20) + '  ' + 'Hash'.ljust(8) + '  ' + 'Date'.ljust(
        20) + '  ' + 'Message'.ljust(20))
    print("=====================================================================================================")
    for commit in commits:
        print(commit['author'].ljust(8) + '  ' + commit['email'][:20].ljust(20) + '  ' + commit['hash'][:7].ljust(
            8) + '  ' + commit['date'] + '  ' + commit['message'])