-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-functions
More file actions
179 lines (169 loc) · 6.52 KB
/
Copy pathgit-functions
File metadata and controls
179 lines (169 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# git-functions: A collection of bash functions to streamline Git operations.
# Source this file in your shell (e.g., source git-functions) to make these commands available.
# ------------------------------------------------------------------------------
# git-resync-clean
# ------------------------------------------------------------------------------
# Resets the local repository's master branch to match the remote origin/master
# and removes all untracked files and directories.
# WARNING: This will discard all local changes and untracked files on master.
#
# Usage: git-resync-clean
#
function git-resync-clean() {
git fetch origin && git reset --hard origin/master && git clean -f -d
}
# ------------------------------------------------------------------------------
# git-commit-push-branch
# ------------------------------------------------------------------------------
# Commits all tracked modifications (-a) with the provided commit message and
# pushes the commit to the specified remote branch.
#
# Arguments:
# $1 - Commit message (must be enclosed in double quotes)
# $2 - Remote branch name to push to
#
# Usage: git-commit-push-branch "My commit message" main
#
function git-commit-push-branch() {
if [ $# -ne 2 ]
then
echo "Correct Syntax: git-commit-push-branch <commit message in double quotes> <branch name>."
else
git commit -a -m "$1"
git push -u origin "$2"
fi
}
# ------------------------------------------------------------------------------
# git-add-commit-push-branch
# ------------------------------------------------------------------------------
# Stages all files (including untracked ones), commits them with the provided
# message, and pushes to the specified branch.
#
# Arguments:
# $1 - Commit message (must be enclosed in double quotes)
# $2 - Remote branch name to push to
#
# Usage: git-add-commit-push-branch "Add new feature" feature-branch
#
function git-add-commit-push-branch() {
git add --all
git-commit-push-branch "$1" "$2"
}
# ------------------------------------------------------------------------------
# git-scommit-push-branch
# ------------------------------------------------------------------------------
# Commits all tracked modifications (-a) with a GPG signature (-S), using the
# provided commit message, and pushes to the specified remote branch.
#
# Arguments:
# $1 - Commit message (must be enclosed in double quotes)
# $2 - Remote branch name to push to
#
# Usage: git-scommit-push-branch "Signed commit message" main
#
function git-scommit-push-branch() {
if [ $# -ne 2 ]
then
echo "Correct Syntax: git-scommit-push-branch <commit message in double quotes> <branch name>."
else
git commit -S -a -m "$1"
git push -u origin "$2"
fi
}
# ------------------------------------------------------------------------------
# git-add-scommit-push-branch
# ------------------------------------------------------------------------------
# Stages all files, creates a GPG-signed commit (-S) with the provided message,
# and pushes to the specified branch.
#
# Arguments:
# $1 - Commit message (must be enclosed in double quotes)
# $2 - Remote branch name to push to
#
# Usage: git-add-scommit-push-branch "Signed add & commit" feature-branch
#
function git-add-scommit-push-branch() {
git add --all
git-scommit-push-branch "$1" "$2"
}
# ------------------------------------------------------------------------------
# git-rename-branch
# ------------------------------------------------------------------------------
# Renames an existing local branch and updates the remote branch on origin.
# It deletes the old branch name from origin and pushes the new branch name,
# setting up upstream tracking.
#
# Arguments:
# $1 - Current branch name
# $2 - New branch name
#
# Usage: git-rename-branch old-name new-name
#
function git-rename-branch() {
git branch -m "$1" "$2" # Rename branch locally
git push origin :"$1" # Delete the old branch
git push --set-upstream origin "$2" # Push the new branch and set local branch to track the new remote
}
# ------------------------------------------------------------------------------
# git-revert-and-sync-master
# ------------------------------------------------------------------------------
# Fetches the latest updates from origin, hard-resets the current branch to match
# origin/master, and cleans any untracked files/directories.
# Note: This has the same behavior as git-resync-clean.
# WARNING: This will discard all local changes.
#
# Usage: git-revert-and-sync-master
#
function git-revert-and-sync-master() {
git fetch origin && git reset --hard origin/master && git clean -f -d
}
# ------------------------------------------------------------------------------
# git-revert-and-sync-branch
# ------------------------------------------------------------------------------
# Fetches the latest updates from origin, hard-resets the current branch to match
# origin/<branch_name>, and cleans any untracked files/directories.
# WARNING: This will discard all local changes.
#
# Arguments:
# $1 - Remote branch name to sync and reset to
#
# Usage: git-revert-and-sync-branch feature-branch
#
function git-revert-and-sync-branch() {
git fetch origin && git reset --hard origin/"$1" && git clean -f -d
}
# ------------------------------------------------------------------------------
# git-merge-to-master
# ------------------------------------------------------------------------------
# Merges the specified branch into the local master branch, pushes the updated
# master to the remote origin, and deletes the local merged branch.
#
# WARNING: This function sleeps between steps (15s before push, 10s before delete)
# to allow the user to review or cancel the action (via Ctrl+C).
#
# Arguments:
# $1 - The local branch name to merge into master
#
# Usage: git-merge-to-master feature-branch
#
function git-merge-to-master() {
git checkout master
git merge "$1"
echo "Waiting 15s to confirm the git push"
sleep 15
git push -u origin master
echo "Waiting 10s to confirm the git branch delete"
sleep 10
git branch -d "$1"
}
# ------------------------------------------------------------------------------
# git-config
# ------------------------------------------------------------------------------
# Configures the global Git identity (name and email) for the user Sriram S.
#
# Usage: git-config
#
function git-config() {
git config --global user.name "Sriram S"
git config --global user.email sriram@cloudbuilder.in
}