0%

github官网给出的教程挺通俗易懂的,移步[https://docs.github.com/cn/get-started/using-git]

本地配置ssh和github,参考官方文档[https://docs.github.com/cn/authentication/connecting-to-github-with-ssh/about-ssh]

远程仓库使用

在本地设置推送到远程仓库的用户名:[https://docs.github.com/cn/get-started/getting-started-with-git/setting-your-username-in-git]

1
git config --global user.name "your name"

远程URL是 Git 一种指示“您的代码存储位置”的绝佳方式,您只能推送到两类 URL 地址:

  • HTTPS URL,如 https://github.com/user/repo.git
  • SSH URL,如 git@github.com:user/repo.git

Git 将远程 URL 与名称相关联,您的默认远程通常名为 origin

创建远程仓库,并将其命名为master

1
git remote add master <REMOTE_URL> 

如果后面想更改url,使用

1
git remote set-url origin <new_url>

查看远程仓库设置:

1
git remote -v

重命名远程仓库

1
git remote rename origin destination

删除远程仓库

1
git remote rm destination

推送提交至远程仓库,

1
git push <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME> 

拉取某远程仓库距离上一次抓取之后的工作:

1
2
git fetch <remote_name>
必须注意 git fetch 命令只会将数据下载到你的本地仓库——它并不会自动合并或修改你当前的工作。 当准备好时你必须手动将其合并入你的工作。

显示某远程仓库的信息

1
git remote show <remote_name>

忽略文件

见文档

1
touch .gitignore #该命令在项目根目录会创建一个.gitignore文件,然后往该文件中填东西

如果有些我们不需要跟踪的文件已经提交到了暂存区,那么使用下面的命令来删除暂存区的该文件,再将该文件写入.gitignore文件

1
2
git rm --cached FILENAME 该命令直接将暂存区的那个版本删除了
或者 git restore --staged <file> 该命令会用暂存区的代码覆盖掉工作区的代码

在自己的project中添加别人的project

https://devconnected.com/how-to-add-and-update-git-submodules/

1
2
3
4
5
6
7
git submodule add <remote_url> <destination_folder>

git commit -m "Added the submodule to the project."

git push

git submodule update --init --recursive # 如果想要拉取别人仓库里的submodule到本地

分支

创建分支:当执行git init时,默认创建名字是master的分支

1
git branch <branch_name>

切换到某分支:

1
git checkout <branch_name>

分支切换会改变你工作目录中的文件。在切换分支时,一定要注意你工作目录里的文件会被改变。 如果是切换到一个较旧的分支,你的工作目录会恢复到该分支最后一次提交时的样子。 如果 Git 不能干净利落地完成这个任务,它将禁止切换分支。

上面两条命令可以使用一条命令搞定:

1
git checkout -b <branch_name>

在一条分支上,比如hotfix修改一些文件提交后,需要回到master分支并将hotfix上的分支的修改合并到master

1
git merge hotfix

这是一个Fast-forward。合并完之后master会和hotfix指向同一个位置,这时可以删除hotfix这个分支:git branch -d hotfix

1
2
3
4
5
6
7
8
9
10
11
os.makedirs(path[, mode]) 
递归文件夹创建函数。像mkdir(), 但创建的所有intermediate-level文件夹需要包含子文件夹。

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
输出在文件夹中的文件名通过在树中游走,向上或者向下。返回的是一个三元组(root,dirs,files),是一个生成器类型,需要用for遍历读取

os.chdir(path)
改变当前工作目录

os.listdir(path)
返回path指定的文件夹包含的文件或文件夹的名字的列表。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
import os
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))

path = "/var/www/html/"
dirs = os.listdir( path )
# 输出所有文件和文件夹
for file in dirs:
print (file)

os.walkos.listdir两个函数的区别在于前者会遍历到子文件夹中的子文件,而后者只是返回你传入的path中的文件夹名字和文件名字。

os库中有一个path的模块,专门用于处理文件path相关的属性信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
os.path.abspath(path)
返回绝对路径

os.path.basename(path)
返回文件名

os.path.exists(path)
如果路径 path 存在,返回 True;如果路径 path 不存在,返回 False。

os.path.join(path1[, path2[, ...]])
把目录和文件名合成一个路径

os.path.split(path)
把路径分割成 dirname 和 basename,返回一个元组

os.path.splitext(path)
分割路径,返回路径名和文件扩展名的元组

os.path.walk(path, visit, arg)
遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数

例子:

1
2
3
4
5
6
7
import os

print( os.path.basename('/root/runoob.txt') ) # 返回文件名
print( os.path.dirname('/root/runoob.txt') ) # 返回目录路径
print( os.path.split('/root/runoob.txt') ) # 分割文件名与路径
print( os.path.join('root','test','runoob.txt') ) # 将目录和文件名合成一个路径
print( os.path.splitext('/root/runoob.txt') )
1
2
3
4
5
runoob.txt
/root
('/root', 'runoob.txt')
root/test/runoob.txt
('/root/run/test', '.txt')

上面的split和splitext,前者分割出了文件名和路径,而后者可以分割出路径名和扩展名,如果想要获得文件的扩展名,可以用splitext,传入文件的path就可以了。

1
os.mknod(filename) 创建

复制文件和删除文件,移动文件

如果是删除一个目录,可以使用以下两种方式:

1
2
3
4
5
6
7
8
9
import shutil
# method 1
if os.path.exists(root_dir):
shutil.rmtree(root_dir) # 这里不可以使用os.removedirs(),removedirs只可以删除非空的文件夹,rmdir也是

# method 2
if len(os.listdir(root_dir)) > 0:
for file in os.scandir(root_dir):
os.remove(file.path)

如果想复制一个文件到另外一个文件夹,参考 https://zhuanlan.zhihu.com/p/35725217

1
copyfile(source_file, destination_file)

记住这里第二个参数一定要是可写入的文件名字,而不是目录。

移动文件

1
2
3
4
5
6
7
8
9
10
import shutil
import os

file_source = 'Path/Of/Directory'
file_destination = 'Path/Of/Directory'

get_files = os.listdir(file_source)

for g in get_files:
shutil.move(file_source + g, file_destination)

读写csv文件

第一个方式是用pandas,具体不介绍。

这里总结一下csv库

1
2
3
4
5
6
7
8
9
10
# 读取csv文件
with open(filename,'r') as csvfile:
csv_reader = csv.reader(csvfile)
for row in csv_reader:
print(row[1]) # 用列表的index取值
# 写csv文件
with open(filename,'w') as csvfile:
csv_writer = csv.writer(csvfile)
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) # writerow接受一个list,所有值都会写在一行里
spamwriter.writerows([[],[],[]]) # writerows写入多行,每一行是一个列表,传进去的是列表的列表

除了写入list,还可以写字典类型的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# my data rows as dictionary objects 
mydict =[{'branch': 'COE', 'cgpa': '9.0', 'name': 'Nikhil', 'year': '2'},
{'branch': 'COE', 'cgpa': '9.1', 'name': 'Sanchit', 'year': '2'},
{'branch': 'IT', 'cgpa': '9.3', 'name': 'Aditya', 'year': '2'},
{'branch': 'SE', 'cgpa': '9.5', 'name': 'Sagar', 'year': '1'},
{'branch': 'MCE', 'cgpa': '7.8', 'name': 'Prateek', 'year': '3'},
{'branch': 'EP', 'cgpa': '9.1', 'name': 'Sahil', 'year': '2'}]

# field names
fields = ['name', 'branch', 'year', 'cgpa']

# name of csv file
filename = "university_records.csv"

# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv dict writer object
writer = csv.DictWriter(csvfile, fieldnames = fields)

# writing headers (field names)
writer.writeheader()

# writing data rows
writer.writerows(mydict)

在ubuntu上安装了其他版本的python之后遇到如下报错:

参考[https://stackoverflow.com/questions/58649177/python3-7-error-while-loading-shared-libraries-libpython3-7m-so-1-0]

对于我的这种情况,先查看libpython3.7m.so.1.0这个文件在哪里?

1
locate libpython3.7m.so.1.0

上面的命令如果返回无,要先更新下系统内文件系统的index字典

1
updatedb

找出libpython3.7m.so.1.0在哪一个lib文件夹内,然后将该lib路径加入搜索路径,可以通过:

1
export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib

以上方式只对该session起作用,如果reboot了系统就会失效,想要永久的方式:

1
sudo ldconfig /usr/local/lib