gitbetter.git
1import os 2 3 4def execute(command: str) -> int: 5 """Execute git command. 6 7 Equivalent to `os.system(f"git {command}")` 8 9 Returns the output of the `os.system` call.""" 10 return os.system(f"git {command}") 11 12 13def new_repo(): 14 """>>> git init -b main""" 15 execute("init -b main") 16 17 18def loggy(): 19 """Equivalent to `git log --oneline --name-only --abbrev-commit --graph`.""" 20 execute("log --oneline --name-only --abbrev-commit --graph") 21 22 23def status(): 24 """Execute `git status`.""" 25 execute("status") 26 27 28# ======================================Staging/Committing====================================== 29def commit(args: str): 30 """>>> git commit {args}""" 31 execute(f"commit {args}") 32 33 34def add(files: list[str] | None = None): 35 """Stage a list of files. 36 37 If no files are given (`files=None`), all files will be staged.""" 38 if not files: 39 execute("add .") 40 else: 41 execute(f'add {" ".join(files)}') 42 43 44def commit_files(files: list[str], message: str): 45 """Stage and commit a list of files with commit message `message`.""" 46 add(files) 47 commit(f'-m "{message}"') 48 49 50def initcommit(): 51 """Equivalent to 52 >>> git add . 53 >>> git commit -m "Initial commit" """ 54 add() 55 commit('-m "Initial commit"') 56 57 58def amend(files: list[str] | None = None): 59 """Stage and commit changes to the previous commit. 60 61 If `files` is `None`, all files will be staged. 62 63 Equivalent to: 64 >>> git add {files} 65 >>> git commit --amend --no-edit 66 """ 67 add(files) 68 commit("--amend --no-edit") 69 70 71def tag(id_: str): 72 """Tag the current commit with `id_`. 73 74 Equivalent to `git tag {id_}`.""" 75 execute(f"tag {id_}") 76 77 78# ==========================================Push/Pull========================================== 79def add_remote_url(url: str, name: str = "origin"): 80 """Add remote url to repo.""" 81 execute(f"remote add {name} {url}") 82 83 84def push(args: str = ""): 85 """Equivalent to `git push {args}`.""" 86 execute(f"push {args}") 87 88 89def pull(args: str = ""): 90 """Equivalent to `git pull {args}`.""" 91 execute(f"pull {args}") 92 93 94def push_new_branch(branch: str): 95 """Push a new branch to origin with tracking. 96 97 Equivalent to `git push -u origin {branch}`.""" 98 push(f"-u origin {branch}") 99 100 101def pull_branch(branch: str): 102 """Pull `branch` from origin.""" 103 pull(f"origin {branch}") 104 105 106# ============================================Checkout/Branches============================================ 107def branch(args: str): 108 """Equivalent to `git branch {args}`.""" 109 execute(f"branch {args}") 110 111 112def list_branches(): 113 """Print a list of branches.""" 114 branch("-vva") 115 116 117def checkout(args: str): 118 """Equivalent to `git checkout {args}`.""" 119 execute(f"checkout {args}") 120 121 122def switch_branch(branch_name: str): 123 """Switch to the branch specified by `branch_name`. 124 125 Equivalent to `git checkout {branch_name}`.""" 126 checkout(branch_name) 127 128 129def create_new_branch(branch_name: str): 130 """Create and switch to a new branch named with `branch_name`. 131 132 Equivalent to `git checkout -b {branch_name} --track`.""" 133 checkout(f"-b {branch_name} --track") 134 135 136def delete_branch(branch_name: str, local_only: bool = True): 137 """Delete `branch_name` from repo. 138 139 #### :params: 140 141 `local_only`: Only delete the local copy of `branch`, otherwise also delete the remote branch on origin and remote-tracking branch.""" 142 branch(f"--delete {branch_name}") 143 if not local_only: 144 push(f"origin --delete {branch_name}") 145 146 147def undo(): 148 """Undo uncommitted changes. 149 150 Equivalent to `git checkout .`.""" 151 checkout(".") 152 153 154def merge(branch_name: str): 155 """Merge branch `branch_name` with currently active branch.""" 156 execute(f"merge {branch_name}") 157 158 159# ===============================Requires GitHub CLI to be installed and configured=============================== 160 161 162def create_remote(name: str, public: bool = False): 163 """Uses GitHub CLI (must be installed and configured) to create a remote GitHub repo. 164 165 #### :params: 166 167 `name`: The name for the repo. 168 169 `public`: Set to `True` to create the repo as public, otherwise it'll be created as private.""" 170 visibility = "--public" if public else "--private" 171 os.system(f"gh repo create {name} {visibility}") 172 173 174def make_private(owner: str, name: str): 175 """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to private. 176 177 #### :params: 178 179 `owner`: The repo owner. 180 181 `name`: The name of the repo to edit.""" 182 os.system(f"gh repo edit {owner}/{name} --visibility private") 183 184 185def make_public(owner: str, name: str): 186 """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to public. 187 188 #### :params: 189 190 `owner`: The repo owner. 191 192 `name`: The name of the repo to edit.""" 193 os.system(f"gh repo edit {owner}/{name} --visibility public") 194 195 196def delete_remote(owner: str, name: str): 197 """Uses GitHub CLI (must be isntalled and configured) to delete the remote for this repo. 198 199 #### :params: 200 201 `owner`: The repo owner. 202 203 `name`: The name of the remote repo to delete.""" 204 os.system(f"gh repo delete {owner}/{name} --yes")
5def execute(command: str) -> int: 6 """Execute git command. 7 8 Equivalent to `os.system(f"git {command}")` 9 10 Returns the output of the `os.system` call.""" 11 return os.system(f"git {command}")
Execute git command.
Equivalent to os.system(f"git {command}")
Returns the output of the os.system
call.
>>> git init -b main
19def loggy(): 20 """Equivalent to `git log --oneline --name-only --abbrev-commit --graph`.""" 21 execute("log --oneline --name-only --abbrev-commit --graph")
Equivalent to git log --oneline --name-only --abbrev-commit --graph
.
Execute git status
.
>>> git commit {args}
35def add(files: list[str] | None = None): 36 """Stage a list of files. 37 38 If no files are given (`files=None`), all files will be staged.""" 39 if not files: 40 execute("add .") 41 else: 42 execute(f'add {" ".join(files)}')
Stage a list of files.
If no files are given (files=None
), all files will be staged.
45def commit_files(files: list[str], message: str): 46 """Stage and commit a list of files with commit message `message`.""" 47 add(files) 48 commit(f'-m "{message}"')
Stage and commit a list of files with commit message message
.
51def initcommit(): 52 """Equivalent to 53 >>> git add . 54 >>> git commit -m "Initial commit" """ 55 add() 56 commit('-m "Initial commit"')
Equivalent to
>>> git add .
>>> git commit -m "Initial commit"
59def amend(files: list[str] | None = None): 60 """Stage and commit changes to the previous commit. 61 62 If `files` is `None`, all files will be staged. 63 64 Equivalent to: 65 >>> git add {files} 66 >>> git commit --amend --no-edit 67 """ 68 add(files) 69 commit("--amend --no-edit")
Stage and commit changes to the previous commit.
If files
is None
, all files will be staged.
Equivalent to:
>>> git add {files}
>>> git commit --amend --no-edit
72def tag(id_: str): 73 """Tag the current commit with `id_`. 74 75 Equivalent to `git tag {id_}`.""" 76 execute(f"tag {id_}")
Tag the current commit with id_
.
Equivalent to git tag {id_}
.
80def add_remote_url(url: str, name: str = "origin"): 81 """Add remote url to repo.""" 82 execute(f"remote add {name} {url}")
Add remote url to repo.
Equivalent to git push {args}
.
Equivalent to git pull {args}
.
95def push_new_branch(branch: str): 96 """Push a new branch to origin with tracking. 97 98 Equivalent to `git push -u origin {branch}`.""" 99 push(f"-u origin {branch}")
Push a new branch to origin with tracking.
Equivalent to git push -u origin {branch}
.
Pull branch
from origin.
108def branch(args: str): 109 """Equivalent to `git branch {args}`.""" 110 execute(f"branch {args}")
Equivalent to git branch {args}
.
Print a list of branches.
118def checkout(args: str): 119 """Equivalent to `git checkout {args}`.""" 120 execute(f"checkout {args}")
Equivalent to git checkout {args}
.
123def switch_branch(branch_name: str): 124 """Switch to the branch specified by `branch_name`. 125 126 Equivalent to `git checkout {branch_name}`.""" 127 checkout(branch_name)
Switch to the branch specified by branch_name
.
Equivalent to git checkout {branch_name}
.
130def create_new_branch(branch_name: str): 131 """Create and switch to a new branch named with `branch_name`. 132 133 Equivalent to `git checkout -b {branch_name} --track`.""" 134 checkout(f"-b {branch_name} --track")
Create and switch to a new branch named with branch_name
.
Equivalent to git checkout -b {branch_name} --track
.
137def delete_branch(branch_name: str, local_only: bool = True): 138 """Delete `branch_name` from repo. 139 140 #### :params: 141 142 `local_only`: Only delete the local copy of `branch`, otherwise also delete the remote branch on origin and remote-tracking branch.""" 143 branch(f"--delete {branch_name}") 144 if not local_only: 145 push(f"origin --delete {branch_name}")
Delete branch_name
from repo.
:params:
local_only
: Only delete the local copy of branch
, otherwise also delete the remote branch on origin and remote-tracking branch.
148def undo(): 149 """Undo uncommitted changes. 150 151 Equivalent to `git checkout .`.""" 152 checkout(".")
Undo uncommitted changes.
Equivalent to git checkout .
.
155def merge(branch_name: str): 156 """Merge branch `branch_name` with currently active branch.""" 157 execute(f"merge {branch_name}")
Merge branch branch_name
with currently active branch.
163def create_remote(name: str, public: bool = False): 164 """Uses GitHub CLI (must be installed and configured) to create a remote GitHub repo. 165 166 #### :params: 167 168 `name`: The name for the repo. 169 170 `public`: Set to `True` to create the repo as public, otherwise it'll be created as private.""" 171 visibility = "--public" if public else "--private" 172 os.system(f"gh repo create {name} {visibility}")
Uses GitHub CLI (must be installed and configured) to create a remote GitHub repo.
:params:
name
: The name for the repo.
public
: Set to True
to create the repo as public, otherwise it'll be created as private.
175def make_private(owner: str, name: str): 176 """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to private. 177 178 #### :params: 179 180 `owner`: The repo owner. 181 182 `name`: The name of the repo to edit.""" 183 os.system(f"gh repo edit {owner}/{name} --visibility private")
Uses GitHub CLI (must be installed and configured) to set the repo's visibility to private.
:params:
owner
: The repo owner.
name
: The name of the repo to edit.
186def make_public(owner: str, name: str): 187 """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to public. 188 189 #### :params: 190 191 `owner`: The repo owner. 192 193 `name`: The name of the repo to edit.""" 194 os.system(f"gh repo edit {owner}/{name} --visibility public")
Uses GitHub CLI (must be installed and configured) to set the repo's visibility to public.
:params:
owner
: The repo owner.
name
: The name of the repo to edit.
197def delete_remote(owner: str, name: str): 198 """Uses GitHub CLI (must be isntalled and configured) to delete the remote for this repo. 199 200 #### :params: 201 202 `owner`: The repo owner. 203 204 `name`: The name of the remote repo to delete.""" 205 os.system(f"gh repo delete {owner}/{name} --yes")
Uses GitHub CLI (must be isntalled and configured) to delete the remote for this repo.
:params:
owner
: The repo owner.
name
: The name of the remote repo to delete.