-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.txt
105 lines (82 loc) · 4.33 KB
/
git.txt
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
# Aliases! Everyone loves aliases. Who has time for typing?!
# super-fancy history
alias gl='git log --graph --pretty=format:'\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit'
alias gsns='git show --name-status'
alias gdc='git diff --cached'
# "git last commit"
alias glc='git show --name-status'
# "git last branches"
alias glb='git reflog | grep checkout | cut -d\ -f6,8 | sed '\''s/ /\\n/'\'' | head -n50 | awk '\''!x[$0]++'\'' | egrep [g-z] | head -n8'
alias glp='git log -p'
# git log also supports \e[38;5;208m--grep=foo | --grep foo\e[0m to filter commit messages. Add \e[38;5;208m-i\e[0m after it to make it case-insensitive.
# show \e[4monly\e[0m paths/names of changed files, for additional handling. cut removes the git flag(s), sed takes care of quotes
# in filenames with spaces.
alias gsf='git status -s -uno --porcelain | cut -c4- | sed '\''s/^"//;s/"$//'\'''
# grab myfile.txt from otherbranch and put it HERE, RIGHT FUCKING NOW
$ git checkout otherbranch myfile.txt
# just show old/other version of file, from another branch or specific commit (shown)
$ git show 412c00baa6\e[32;1m:\e[0mpath/to/filename
# grab a commit from elsewhere to current branch:
$ git cherry-pick otherBranch
# or by commit hash:
$ git cherry-pick <commit-hash>
# what am I about to push?
$ git diff [--stat] origin/master
# diff between branches, commit hash, symbolic ref..
$ git diff [options] <commit>..<commit> [--] [path..]
# add specific parts of changed file to stage:
$ git add -p
# it'll let you open an editor to tweak it line by line if needed
# git add everything that is being tracked already
$ git add -u
# delete (remote) branch
$ git push -d origin <branchname>
$ git branch -d <branchname>
# '-D' or '-d --force' will also delete unmerged branches
# if this fails to work with a
error: unable to delete '<foo>': remote ref does not exist
error: failed to push some refs to '<remote addr>'
# maybe do a \e[38;5;208mgit fetch --prune\e[0m
# rename (remote) branch, assuming you have branchOldName checked out
$ git branch -m \e[32mbranchNewName\e[0m
$ git push origin :branchOldName; $ git push --delete \e[38;5;208mbranchOldName\e[0m
# this doesn't seem to work with github, just use the webinterface in that case..
$ git branch --unset-upstream \e[32mbranchNewName\e[0m
$ git push
# first you rename the current local branch, then you delete the old remote one (the first form is a shortcut),
# then you unset the remote branch (which is still set to the old name) for the current local new branch
# then you simply push and git will yell at you, and give you something to copy/paste to both push and set the remote
# branch to the new name.
# run any git commit in a repo that is not your current directory
$ git -C /other/path/ status
$ git -C /other/path commit file.txt -m message
# etc
### Repos
# git add remote [github] repo for existing project
# Create remote repo, copy the url. if you don't do this on github, use:
$ git init --bare somedir.git
# and the path can be used, maybe after a ssh 'host:'
$ git remote add origin <paste-url-here>
$ git push
# urls can be like https://... or [email protected]:username/foo.bar, the latter works better with ssh keys
# \e[1mUndo last commit\e[0m - this basically creates a new commit that is the inverse of the last commit, which is more useful
# then \e[38;5;208mgit reset HEAD~1\e[0m if you've already pushed the faulty commit.
$ git revert HEAD
# commit again, and if needed push
# to create a new empty repository locally:
$ git init --bare foo.git
# and paste path instead of url
# git change remote url
git remote set-url origin foo://bar/baz.git
# looks like you can also edit .git/config ?
### Misc
# git puts all informative output on STDERR, which is not unixy. to not spam your inbox via crontab,
# you can use the following (in a script):
git -E fragile-command > tmp 2>&1 || mail_tmp_to_yourself
# or for a bunch of them, "cat tmp >> all_errors.txt" and check that at EOF
# see diff with more context
$ git diff -U30 file
# -U takes number of lines before and after the change. you can use \e[32m-U$(wc -l <file>)\e[0m as well which will
# show the whole file. Since wc also prints the filename after the number of lines, no need to specify that for git too.
$ git diff -U$(wc -l file)
https://jvns.ca/blog/2023/11/01/confusing-git-terminology/