-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-short-cuts.txt
228 lines (157 loc) · 8.23 KB
/
git-short-cuts.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
```
//check status
git status
//ALWAYS DO A PULL FIRST FROM YOUR TRACKED REMOTE BRANCH
git pull
//Add Files for Commiting
git add -A
//Commit local with
git commit -am "My commit message"
//View git remote
git remote -v
//Set remote site
git remote set-url origin [email protected]:projectname/gitfolder.git
//Create Branch
git checkout -b branchname
//Push local branch to remote branch and setup tracking
git push -u origin branchname
//Get a remote branch you do not have then run the second command
git fetch origin
git checkout branch
//Delete Local Branch must make sure not on branch that needs to be deleted
git branch -D branchname
//Delete Remote Branch
git push origin :branchname
or
git push origin --delete branchname
//Look at git global settings
git config --global --list
//This setting will set it so nothing is changed with the line endings this can be overwriiten in the .gitattributes file
git config --global core.auto.crlf false
//Create an ssh key
ssh-keygen -t rsa -b 4096
//Cherry pick individual files from one git branch to another
git checkout --patch branchPickingFrom pathToFile/filename
//Remove orphaned local and remote branches
git remote update --prune
//Show tracked and untracked branches
git remote show origin
//show commit hash numbers
git log --oneline
//revert to an earlier commit
git checkout b46176a
//pull a specific file from an earlier commit
git checkout b46176a hello.py
//remove a commit get the hash
git log --oneline
git revert b46176a
//Revert the commit we just created
git revert HEAD
//Like git checkout, git reset is a versatile command with many configurations. It can be used to remove committed snapshots, although it’s more often used to undo changes in the staging area and the working directory. In either case, it should only be used to undo local changes—you should never reset snapshots that have been shared with other developers.
//Remove the specified file from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
git reset <file>
//Reset the staging area to match the most recent commit, but leave the working directory unchanged. This unstages all files without overwriting any changes, giving you the opportunity to re-build the staged snapshot from scratch.
git reset
//Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too. Put another way: this obliterates all uncommitted changes, so make sure you really want to throw away your local developments before using it.
git reset --hard
//Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone. All changes made since <commit> will reside in the working directory, which lets you re-commit the project history using cleaner, more atomic snapshots.
git reset <commit>
//Move the current branch tip backward to <commit> and reset both the staging area and the working directory to match. This obliterates not only the uncommitted changes, but all commits after <commit>, as well
git reset --hard <commit>
//git clean allows you to remove untracked files and directories it is permanent so be careful. Do a dry run first
git clean -n (this does a dry run)
//Remove ALL untracked files in local repo only
git clean -f
//Remove untracked files, but limit the operation to the specified path.
git clean -f <path>
//Remove untracked files and untracked directories from the current directory.
git clean -df
//Remove untracked files from the current directory as well as any files that Git usually ignores.
git clean -xf
//Difference between local branch and remote branch
git diff <local branch> <remote>/<remote branch>
//Difference between remote file and local file
git diff origin/master -- README.md
//Stop git from showing the VIM editor on git pull
git config --global core.mergeoptions --no-edit
//Show all files that were modified in commit status will show modified or added
git show --pretty="" --name-only hashofcommit
git show --pretty="" --name-status hashofcommit
//Allow long file paths in windows
git config --system core.longpaths true
//Use Verbose with items Make sure it is after the command or it will not work
git 'command' -v
git 'command' --dry-run
//Git clone a specific branch
git clone -b my-branch [email protected]:projectname/gitfolder.git
// Delete a git tag locally and on the remote repo
git tag -d tagname
git push origin :refs/tags/tagname
// Push all tags and push single tag
git push origin --tags
git push origin tagname
// Committing certain folders only
git commit modules_app build -m "updated sql script"
// Revert to last commit discarding changes
git reset --hard
// This shows me what is inside the stash
git stash show -p
// This removes what was in the stash and applies it
git stash pop
// This applies what is in the stash but also keeps it in the stash also
git stash apply
// This shows a list of what is in the stash.
git stash list
// Cherry pick from the list if you are on Git 2.11 will not need the quotes
git stash pop stash@"{1}" or git stash apply stash@"{1}"
// Shows just the names of the files (not the contents) in your first stash
git stash show -p stash@{0}
// One very useful feature is to list contents of all local stashes
git stash list | awk -F: '{ git stash pop stash@"{1}"print "\n\n\n\n"; print $0; print "\n\n"; system("git stash show -p " $1); }'
// List all commited files
git diff-tree --no-commit-id --name-only -r hashofcommit
// Clean history of things that should have been added to .gitignore file but were not
git filter-branch --tree-filter 'rm -rf logs' -f --prune-empty HEAD
// Dealing with long path and file names in Windows
git config --system core.longpaths true
// Add a Git Submodule to your existing project. By default, submodules will add the subproject into a directory named the same as the repository, in this case “DbConnector”. You can add a different path at the end of the command if you want it to go elsewhere.
git submodule add https://github.com/chaconinc/DbConnector
// Cloning a Project with Submodules. When you clone such a project, by default you get the directories that contain submodules, but none of the files within them yet.
git clone https://github.com/chaconinc/MainProject
// The DbConnector directory is there, but empty. You must run two commands: git submodule init to initialize your local configuration file, and git submodule update to fetch all the data from that project and check out the appropriate commit listed in your superproject: git submodule init
git submodule update
git clone https://github.com/chaconinc/MainProject
// Remove directory from git but NOT local
git rm -r --cached myFolder
// Stop VIM from coming up when you do merges in Git Bash
git config --global mergetool.prompt false
about git merge stuff: it depends on your git installation too:
https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-merge.html, perhaps you need to define `GIT_MERGE_AUTOEDIT` env variable to `no`.
// Get all branches back when you do git fetch --all
git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
// See a visual commit history of directories
alias gdf='gitk --follow --all -p'
gdf directory
//How to remove a merge branch
git checkout master
git revert -m 1 <<SHA1 value>>
//If you ever get this error message filename to long
git config --system core.longpaths true
//How to show what files will be added or overwritten with a git pull
git fetch origin ; git diff --name-only master origin/master
//If you want to count the commits on a branch that are made since you created the branch
Examples
git checkout master
git checkout -b test
<We do 3 commits>
git rev-list --count HEAD ^master
//Great way to resolve conflicts
https://lostechies.com/joshuaflanagan/2010/01/29/how-to-resolve-a-binary-file-conflict-with-git/
//See the commits for a partidular day
git log --after="2022-07-07 00:00" --before="2022-07-08 13:59"
//Checkout a particular git commit based on hash
git checkout hash
git checkout -b newbranch hash
//Great Post How to revert a Git repository to a previous commit?
https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit
```