-
Notifications
You must be signed in to change notification settings - Fork 0
/
git
473 lines (340 loc) · 13.1 KB
/
git
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# do not show untracked files in git status
git status -uno
# git revert a file to a specific version
git checkout c5f567 -- file1/to/restore file2/to/restore
# update url for a remote
git remote set-url origin <remote-url>
# remove remote
git remote rm origin
# cache credentials
git config credential.helper "cache --timeout=3600"
# create a new tag
# git pull remote branch
git tag <tagname>
git tag <tagname> commit-hash
# push tags
git push --tags
# delete tag
git tag -d tag-name
# compare local changes with remote after fetch
git diff master origin/master
# avoid merge conflicts while pulling
git pull --rebase
git push
# to checkout the remote branch
git checkout <remote branch name>
# Delete local branch
git branch -d <local-branch-name>
# List all local merged branches.
git branch --merged
# List all local unmerged branches.
git branch --no-merged
# List all remote merged branches.
git branch -r --merged
# List all remote unmerged branches.
git branch -r --no-merged
# Deleting non-existent tracking branches
git remote prune <remote> --dry-run
# setting default name for git branch
git config --global init.defaultBranch master
# add all files and commit in single line
git commit -am "Your commit message"
# get one file from another branch
git checkout master # first get back to master
git checkout experiment -- app.js # then copy the version of app.js from branch "experiment"
# merge feature branch into master
git checkout master
git pull origin master
git merge test
git push origin master
# abort a merge if it fails
git merge --abort
# To create a new branch and switch to it at the same time
# you can run the git checkout command with the -b switch
git checkout -b iss53
# This is shorthand for:
git branch iss53
git checkout iss53
# in order to retrieve a deleted file from git history
# this restores the file
git checkout -- filepath
# to create a branch from a specific branch other than master
git checkout -b newbranch dev
# to abort a rebase operation
git rebase --abort
# see all branches in remote
config ls-remote --heads origin
# fix for some remote branches not being pulled; edit in the repo config file
#https://stackoverflow.com/questions/12762922/git-cannot-see-new-remote-branch
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
# sometimes the branch may not be listed but you can just use
git checkout <branch-on-remote>
# unstage all changes or undo all staged files at once
# or abanded unwanted merge
git reset
# view difference between a file in two different branches
git diff branch1 branch2 -- /path/to/file
# In order to compare two branches using commit abbreviations, use the “git log” command with the following options.
git log --oneline --graph --decorate --abbrev-commit branch1..branch2
# try this also
git log --oneline --graph --all --decorate --abbrev-commit
# see git log for specific branches
git log --oneline --graph branch1 branch2
# Filter git diff by type of change
# for filtering modified files
--diff-filter=M
# undo latest git commit
git reset HEAD~ # uses --soft by default
# by using the --soft option you can keep the modifications in your Working Directory
# To discard the changes in the working directory as well
git reset --hard HEAD~
# see difference between two branches
git checkout master
git diff feature
# clone git repo using app password
git clone https://oauth-key-goes-here@github.com/username/repo.git
# cloning git repo with PAT or personal access token
git clone https://ghp_ThisIsThePersonalAccessTokenxxxxxxxx@github.com/username/reponame
# get number of commits made sorted by author
git shortlog -s
# clone using ssh
eval `ssh-agenet -s` # restart agent
ssh-add ~/.ssh/id_rsa # add key
ssh-add -L # list keys
# see only word diff
git diff --word-diff
# To undo git add before a commit, run
git reset <file>
git reset # to unstage all changes
# view the url associated with git repo
git config --get remote.origin.url
# see details associated with git repo
git config --list
# create a branch without any previous commit history
git checkout --orphan <branchname>
# push an orphan branch to remote
git push origin branchname
# clone specific branch
git clone -b <branch>
# revert to commit before merge without creating new commit
git reset --hard <commit_before_merge>
# git checkout if file or folder or same name exists
git checkout branchname --
# dry run for merge operation
git merge --no-commit --no-ff $BRANCH
git diff --cached # To examine the staged changes:
git merge --abort # undo the merge, even if it is a fast-forward merge:
# delete branches from remote
git push -d <remote> <branchname>
# see which files changed along with commit history
git log --raw
# undo a revert commit
git reset --hard HEAD^
# stage only parts of a modified file interactively
git add -p
# checkout a specific version of file from git history and save it with different name
# without file path the show command might produce a diff
git show <commit_id>:path/to/file > new_file_name
# delete a branch
git branch -d branchname # to show a warning if a branch is not merged
git branch -D branchname # to directly delete it
# Merge all changes in a linear fashion upto a specific commit
git merge commit-hash # if the histories can be conneted linearly this is called a fast-forward merge
# and proceeds without issues
# Use cherry-pick to merge specific changes introduced by a specific commit
git checkout -b temp # no dry run option
git cherry-pick commit-hash
# you might have to manually resolve merge conflicts if they happen by removing conflict
# markers in the affected file and then adding the file using
git add file-path
git cherry-pick --continue
# To see the changes introduced by a specific commit to a particular file
git log -p -1 merge_commit_hash -- path/to/your/file
# the p option stands for patch and 1 limits the output to a single commit
# To squash several commits into one.
git rebase -i HEAD~# # where # is the number of commits to squash.
Change pick to squash on each commit that should become part of the one above it. Then edit your commit messages as you see fit.
# To show word diff instead of line diff in git
git diff --word-diff filename
# Noticed something about git
# Contrary to my assumption, git rebase doesnt order commits using the timestamp of commits
# checkout file from another branch and but rename it
git show [source_branch]:[file_path] > [full_file_path]
# undo a git merge
git reset --hard commit-before-the-merge
or
git reset --hard HEAD~1
# A better way to undo a git merge
#Since the methods discussed above will revert uncommitted changes, Git provides a safer flag which is --merge.
#To undo a merge with the --merge flag, run git reflog to see the hashes of commits, then run
git reset --merge previous-commit
# To get changes from master back into a feature branch
git checkout feature-branch
git rebase master || git rebase --abort
# restore staged changes in file based on hunk or patch
git checkout -p file.txt
# restore a previous version of a file using commit hash but with different name
git show <commit_hash>:path/to/old_filename.ext > path/to/new_filename.ext
# see branches in remote
git ls-remote --heads origin
# delete branch from remote without deleting locally
git checkout <branch-name>
git push origin --delete <branch-name>
# see last three commit info
git log -n 3
# select individual commits to be added from an old branch to a new branch
git checkout new-branch
git cherry-pick <commit-hash-1>
# or if multiple commits
git cherry-pick <oldest-commit>^..<newest-commit>
# if duplicate commits are a concern
-x # this will add a cherry-picked from message in the commit msg
# see condensed diff
git diff --stat master develop
# see the diff between latest commit and the one prior for a particular file
# HEAD^ refers to parent commit of current HEAD
git diff HEAD^ HEAD -- examples/make_dataset_single.py
# using a particular commit hash
git diff abcdef1 -- path/to/your/file.txt
# to see the history of changes or commits that affect a single file
git log -- <file>
# when using git stash pop a file may appear under both modified
# to discard changes in current repo and keep remote
git checkout --theirs <file> # after restoring add and commit it
# to decide whether to git pull or git pull --rebase
git fetch origin # Fetch changes from the remote
git log origin/<branch-name>..HEAD # Check the commit history
# or the other way around
git log HEAD..origin/<branch-name>
# force merge an orphan branch into the master
--allow-unrelated-histories
# see condensed git diff
git diff branch1 branch2 --stat
# similar to git show, get an entire directory from a previous commit
# note that this can overwrite your local folder of same name if you have one
git checkout <commit-hash> -- path/to/folder
# see the global git configuration
git config --list
# create branch from latest commit which has not been merged
git checkout -b temp-review origin/main
# add using the patch option
git add -p
# split into individual hunks manually using e in patch mode
if you want to keep only lines starting with -
delete lines starting with +
if you want to selectively keep some lines starting with -
change the - to a space
# see last 5 commits in log
-n5
# do actions in bulk like git restore
git restore --staged .
# git log in oneline with date information
git log --pretty=format:"%h %ad %s" --date=short
# with coloring etc.
git log --pretty=format:"%C(auto)%h%C(reset) %ad %C(auto)%d%C(reset) %s" --date=format:"%b %d, %Y"
# undo a git reset
git reset ORIG_HEAD
# stash
git stash
# save individual file to stash
git stash -m "message" -- path/to/file
# git stash reapply
git stash pop
# see all stash
git stash list
# see contents in a stash
git stash show stash@{0}
# show diff between the stashed state and its original parent
git stash show -p
# see individual file changes from the stash
git diff stash@{0}^! -- /path/to/file
# save the version of file in the stash with different name
git show stash@{0}:/path/to/file
# drop the stash
git stash drop stash@{0}
# save the version in shown in git status
git show :/path/to/file > new/path/to/file
# you forgot to add something in your last commit
git commit --amend --no-edit
# restore with patch
git restore -p
# use reflog to find commit or HEAD number
git reflog
git reset --hard HEAD@{1}
# local git configuration
git config user.email "email-id"
git config user.name "username"
# Do a fast-forward merge but create a merge commit if not possible
git pull --ff
# Abort if a fast-forward merge is not possible
git pull --ff-only
# prevent git config holding sensitive information
[include]
path = ~/.gitconfig.local
# reverse the usual order of the diff
diff -R
########################
ADVANCED
########################
# git branch operations
-m
--move
Move/rename a branch and the corresponding reflog.
-M
Shortcut for --move --force
# combine last three git commits into one
git reset --soft HEAD~3
git commit -m "msg"
git push -f
# rename local and remote branch
# Step 1: Rename the local branch to the new name
git branch -m <old_name> <new_name>
# Step 2: Delete the old branch on remote - where <remote> is, for example, origin
git push <remote> --delete <old_name>
# Or shorter way to delete remote branch [:]
git push <remote> :<old_name>
# Step 3: Prevent git from using the old name when pushing in the next step.
# Otherwise, git will use the old upstream name instead of <new_name>.
git branch --unset-upstream <new_name>
# Step 4: Push the new branch to remote
git push <remote> <new_name>
# Step 5: Reset the upstream branch for the new_name local branch
git push <remote> -u <new_name>
# To fix if you accidently commit big file to git
# if it was the most recent commit
git rm --cached big/file/path
git commit --amend -C HEAD
# else find the commit hash of the last good commit - before the commit where added this file
git rebase -i <commit-hash>
# change the `pick` keyword to `edit` for the bad commit
# write and exit and proceed using the first method - as if adding the file was the most recent commit
# create a backup for safety reasons
--allow-empty # use if needed
# zip files indexed by git
git archive --format zip --output /full/path/to/zipfile.zip master # from the root of the git directory
# ignore chmod changes mode
git config core.fileMode false
# fix for git push failed error
# https://stackoverflow.com/questions/38378914/how-to-fix-git-error-rpc-failed-curl-56-gnutls
git config --global http.postBuffer 1048576000
# To stash your working directory including untracked files
# (especially those that are in the .gitignore)
git stash -u
# Abort a stash pop
# Note: This will discard any staged changes
git reset --merge
# specify location of git dir in case of bare git repos
--git-dir=/path/to/git
# use vimdiff as your git diff tool
difftool -y -t vimdiff filename
# set up a submodule
git submodule add "/path/to/repo"
git submodule update --init --recursive
# change the remote url of a submodule
git submodule set-url -- voidrice git@github.com:linnabraham/voidrice.git
Add and commit the .gitmodule file
## on cloned version of repo do the following after pulling
git submodule sync
# make git push verbose
git push --verbose