gitref.txt 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. Git: distributed version-control system (https://git-scm.com/)
  2. Git User Manual (git means "unpleasant person" in British English slang)
  3. file:/usr/share/doc/git/html/user-manual.html
  4. You can download default github .gitignore files for many programming languages here:
  5. https://github.com/github/gitignore
  6. Practicing and Understanding Git - ohmygit.org
  7. License file - https://choosealicense.com/
  8. .gitignore file - https://gitignore.io
  9. Visualizing Git concepts with D3 -
  10. https://onlywei.github.io/explain-git-with-d3/#commit
  11. Ref: man gittutorial, gittutorial-2, giteveryday, gitcore-tutorial
  12. Git is simple to use. Just type git. Without any arguments, Git lists its
  13. options and the most common subcommands.
  14. $ git
  15. For a complete (and somewhat daunting) list of git subcommands, type git help --all.
  16. $ git help --all
  17. Git stores configuration options in three separate file, which lets you scope options
  18. to individual repositories (local), user (global), or the entire system (system):
  19. * local /.gitconfig - repository-specific settings
  20. * global /.gitconfig - user-specific settings
  21. * system /etc/gitconfig - system-wide settings
  22. View config settings:
  23. git config --list
  24. view config settings and the file path:
  25. git config --list --show-origin
  26. Git local setup:
  27. git config --local user.name "Saravanan Dayalan"
  28. git config --local user.email "dayalsaravanan@gmail.com"
  29. git config --local core.editor vim
  30. git config --local color.ui false
  31. git config --local init.defaultBranch main
  32. git config --local --edit
  33. git config --local --list
  34. git config --local --list --show-origin
  35. Git global setup:
  36. git config --global user.name "Saravanan Dayalan"
  37. git config --global user.email "dayalsaravanan@gmail.com"
  38. git config --global core.editor vim
  39. git config --global color.ui fasle
  40. git config --global init.defaultBranch main
  41. git config --global --edit
  42. git config --global --list
  43. git config --global --list --show-origin
  44. Git system setup:
  45. git config --system user.name "Saravanan Dayalan"
  46. git config --system user.email "dayalsaravanan@gmail.com"
  47. git config --system core.editor vim
  48. git config --system color.ui false
  49. git config --system init.defaultBranch main
  50. git config --system --edit
  51. git config --system --list
  52. git config --system --list --show-origin
  53. Reference: https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
  54. # aliases examples
  55. git config --global alias.st status
  56. git config --global alias.co checkout
  57. git config --global alias.br branch
  58. git config --global alias.up rebase
  59. git config --global alias.ci commit
  60. git help config
  61. git config --help
  62. man git-config
  63. Create a new repository:
  64. git clone https://gitlab.com/dsaravanan/Books.git
  65. cd Books
  66. touch README.md
  67. git add README.md
  68. git commit -m "add README"
  69. git push -u origin main
  70. Existing folder:
  71. cd existing folder
  72. git init
  73. git remote add origin https://gitlab.com/dsaravanan/Books.git
  74. git add .
  75. git commit -m "Initial commit"
  76. git push -u origin main
  77. Existing Git repository:
  78. cd existing repo
  79. git remote rename origin old-origin
  80. git remote add origin https://gitlab.com/dsaravanan/Books.git
  81. git push -u origin --all
  82. git push -u origin --tags
  83. # git pull example (git pull does a git fetch followed by a git merge)
  84. git init .
  85. git remote add origin https://gitlab.com/dsaravanan/dotfiles.git
  86. git pull origin main # download all files & commits & overrides in existing repository
  87. git add <filename>
  88. git commit -m <commit message>
  89. git push -u origin main
  90. git status
  91. git log | less / git log --stat | less
  92. git log --pretty=oneline # print the git log messages in oneline
  93. git branch
  94. git branch <localbranchname>
  95. git remote -v # list the web address of the repository
  96. git rev-list @ -- file_name # list commit message of that particular file
  97. and No. of git commits that touched the file
  98. for file in *.pdf; do echo -n "$file "; git rev-list @ -- $file | wc -l; done
  99. git pull origin main # to update the repository
  100. git reflog # manage reflog information
  101. git diff hash1 hash2 # display difference between hash1 and hash2
  102. git status # to check the status of the remote repository
  103. git rm <filename> # delete/remove filename
  104. git pull origin main --allow-unrelated-histories
  105. # Delete the repository in Github
  106. https://help.github.com/en/github/administering-a-repository/deleting-a-repository
  107. # change git remote url
  108. git remote set-url <remote_name> <remote_url>
  109. git remote set-url origin https://git-repo/new-repository.git
  110. git remote set-url origin git@github.com:user/repository.git
  111. # remove files from a git repository
  112. To remove a file both from the Git repository and the filesystem, you can use
  113. git rm without any parameters (except for the file's name, of course):
  114. $ git rm <filename>
  115. If you only want to remove the file from the repository, but keep it on the
  116. filesystem, you can add the --cached flag:
  117. $ git rm <filename> --cached
  118. When trying to delete multiple files in a directory or via a glob pattern, you
  119. might want to perform a "dry-run" first and see which files would be removed:
  120. $ git rm <directoryname>/* --dry-run
  121. rm '<directoryname>/about.txt'
  122. rm '<directoryname>/general.txt'
  123. # remove directory from a git repository
  124. $ git rm -r directory/
  125. $ git commit -m 'remove directory'
  126. $ git push origin main
  127. # Commonly used git commands:
  128. add Add file contents to the index
  129. bisect Find the change that introduced a bug by binary search
  130. branch List, create, or delete branches
  131. checkout Checkout and switch to a branch
  132. clone Clone a repository into a new directory
  133. commit Record changes to the repository
  134. diff Show changes between commits, the commit and working trees, etc.
  135. fetch Download objects and refs from another repository
  136. grep Print lines matching a pattern
  137. init Create an empty git repository or reinitialize an existing one
  138. log Show commit logs
  139. merge Join two or more development histories
  140. mv Move or rename a file, a directory, or a symlink
  141. pull Fetch from and merge with another repository or a local branch
  142. push Update remote refs along with associated objects
  143. rebase Forward-port local commits to the updated upstream head
  144. reset Reset current HEAD to the specified state
  145. rm Remove files from the working tree and from the index
  146. show Show various types of objects
  147. status Show the working tree status
  148. tag Create, list, delete, or verify a tag object signed with GPG
  149. # Practical git commands:
  150. 1. Configure User Profile
  151. $ git config user.name "USERNAME"
  152. $ git config user.email "user@example.com"
  153. Add the --global option to set these policies globally.
  154. $ git config --global user.name "USERNAME"
  155. $ git config --global user.email "user@example.com"
  156. 2. Initialize Git Repositories
  157. $ git init
  158. 3. Add Project Files
  159. $ git add <file>
  160. $ git add *.php
  161. 4. Verify Added Files
  162. $ git status
  163. 5. Commit Changes to Repository
  164. $ git commit # invoke default Linux editor
  165. $ git commit -m "commit message"
  166. 6. Display the Logs
  167. $ git log # generalized information
  168. $ git log <filename> # specific file information
  169. 7. Verify Project Branches
  170. $ git branch # the output will mark the current branch with an asterisk
  171. 8. Reset Project Branches
  172. $ git reset
  173. $ git reset --soft
  174. $ git reset --hard
  175. 9. Add a New Branch
  176. $ git branch experimental # create a branch named experimental
  177. 10. Switch between Branches
  178. $ git checkout experimental
  179. 11. Delete a Project Branch
  180. $ git checkout main # switch to main branch
  181. $ git branch -D experimental # delete experimental branch
  182. 12. Check Differences among Commits, Trees, and Files
  183. $ git diff
  184. $ git diff experimental main
  185. 13. Merge Two Branches
  186. $ git merge experimental2 experimental1
  187. $ git merge -s ours experimental # merges experimental branch to current development branch
  188. $ git merge --no-commit experimental # merges branch experimental to current branch
  189. 14. Revert Existing Commits
  190. $ git revert <commit id> # revert changes introduced by <commit id>
  191. $ git revert HEAD~3 # relapses the fourth last commit in HEAD and performs a new commit
  192. 15. Stash Working Directory
  193. $ git stash
  194. $ git stash list
  195. 16. Clone a Repository
  196. $ git clone git://example.com/git.git
  197. $ git clone git://example.com/git.git/ test-dir/ # will download said project into test-dir/
  198. 17. Pull New Updates
  199. $ git pull
  200. 18. Push Your Updates
  201. $ git push
  202. 19. Display Remote Repositories
  203. $ git remote
  204. $ git remote --verbose
  205. 20. Connect to Remote Repositories
  206. $ git remote add origin <server> # add 'origin' as the remote name to the server
  207. 21. Add Tags to Your Project
  208. $ git tag 1.0.0 <commit id>
  209. $ git log
  210. $ git push origin --tags
  211. 22. Fetch Remote Data
  212. $ git fetch origin
  213. 23. Restore Non-Committed Changes
  214. $ git restore --staged test.php
  215. $ git restore --source=HEAD --staged --worktree test.php
  216. 24. Remove Files
  217. $ git rm *.php
  218. $ git rm -r dir/
  219. $ git rm --cached *.php # remove file from repository, not on the filesystem
  220. 25. Move or Rename Files
  221. $ git mv test.py newtest.py
  222. $ mv test.py newtest.py
  223. $ git add newtest.py
  224. $ rm test.py
  225. 26. Clean Untracked Files
  226. $ git clean
  227. $ git clean -n # options -f, -i, -n
  228. 27. Optimize Local Repositories
  229. $ git gc
  230. 28. Archive Local Repositories
  231. $ git archive --output=test
  232. 29. Search for Patterns
  233. $ git grep -iw 'import' main
  234. $ git grep 'import' $(git rev-list --all)
  235. 30. Manage Working Trees
  236. $ git worktree list
  237. $ git worktree add new-branch
  238. $ git worktree remove new-branch
  239. $ git worktree prune
  240. 31. Prune Untracked Objects
  241. $ git prune --dry-run
  242. $ git prune --verbose --progress
  243. 32. Pack Unpacked Objects
  244. $ git repack
  245. 33. List Unpacked Objects
  246. $ git count-objects
  247. 34. Validate the Object Database
  248. $ git fsck
  249. 35. Display Changes for Each Commit
  250. $ git whatchanged
  251. 36. Summarize Log Information
  252. $ git shortlog
  253. $ git shortlog --email --summary
  254. 37. Manage Cnfiguration Options
  255. $ git config --list
  256. $ git config --help
  257. 38. Consult Git Help
  258. $ git help
  259. $ git --help
  260. $ git <command> --help
  261. $ git commit --help
  262. 39. Consult Manual Page
  263. $ man git
  264. $ man git commit
  265. 40. Display Version Information
  266. $ git --version
  267. # To remove remote:
  268. git remote remove origin
  269. # Instead of removing and re-adding:
  270. git remote set-url origin git://new.url
  271. # To remove files from staging area:(to reverse git add <filename>)
  272. git reset <filename>
  273. git reset # reset all the files from the staging area
  274. # To list all deleted files
  275. git log --diff-filter=D --summary
  276. git log --diff-filter=D --summary | grep delete
  277. # Git commands that can help resolve merge conflicts
  278. ## General tools:
  279. git status % The status command is in frequent use when a working with Git and
  280. during a merge it will help identify conflicted files.
  281. git log --merge % Passing the --merge argument to the git log command will produce a
  282. log with a list of commits that conflict between the merging branches.
  283. git diff % diff helps find differences between states of a repository/files.
  284. This is useful in predicting and preventing merge conflicts.
  285. ## Tools for when git fails to start a merge
  286. git checkout % checkout can be used for undoing changes to files,
  287. or for changing branches
  288. git reset --mixed % reset can be used to undo changes to the working directory
  289. and staging
  290. ## Tools for when git conflicts arise during a merge
  291. git merge --abort % Executing git merge with the --abort option will exit from the
  292. merge process and return the branch to the state before the merge
  293. began.
  294. git reset % Git reset can be used during a merge conflict to reset conflicted
  295. files to a know good state
  296. # gitlab reference
  297. https://gitlab.com/pages/plain-html
  298. https://about.gitlab.com/stages-devops-lifecycle/pages/
  299. https://about.gitlab.com/blog/2016/04/07/gitlab-pages-setup/
  300. https://gitlab.com/html-themes/
  301. https://www.atlassian.com/git/tutorials/
  302. # downloading raw file
  303. https://github.com/<username>/<repo-name>/<some-directory>/<filename> # github page link
  304. wget https://raw.github.com/<username>/<repo-name>/<branch-name>/<some-directory>/<filename>
  305. curl -OL https://raw.githubusercontent.com/<username>/<repo-name>/<branch-name>/path/to/file
  306. O means that curl downloads the content
  307. L means that curl follows the redirection
  308. # fatal: refusing to merge unrelated histories
  309. git pull origin main --allow-unrelated-histories
  310. # 7 Git tricks
  311. 1. Autocorrection in Git (enable git autocorrection in git configuration)
  312. $ git config --global help.autocorrect 1
  313. If you want this to apply only to your current repository, omit the --global option.
  314. 2. Count your commits
  315. $ git rev-list --count <branch_name>
  316. 3. Optimize your repo
  317. $ git gc --prune=now --aggressive
  318. This command is an internal utility that cleans up unreachable or orphaned git
  319. objects in your repository.
  320. 4. Take a backup of untracked files
  321. Git, along with some Bash command piping, makes it easy to create a zip archive
  322. for your untracked files.
  323. $ git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup_untracked.zip
  324. The above command makes an archive (and exclude files listed in .gitignore).
  325. 5. Know your .git folder
  326. Current state of HEAD:
  327. $ cat .git/HEAD
  328. Description of your repository:
  329. $ cat .git/description
  330. 6. View a file of another branch
  331. Suppose you have a file called README.md, and it's in the "main" branch. You're
  332. working on a branch called "dev". With the following git command, you can do it
  333. from the terminal.
  334. $ git show main:README.md
  335. 7. Search in Git
  336. $ git rev-list --all | xargs git grep -F ' '
  337. $ git rev-list --all | xargs git grep -F 'font-size: 52 px;'
  338. # ssh-key setup (~/.ssh/)
  339. ssh-keygen -t ed25519 -C "gitlab"
  340. xclip -i -selection clipboard < id_gitlab.pub
  341. ssh -T git@gitlab.com
  342. eval $(ssh-agent -s)
  343. ssh-add /home/saran/.ssh/id_gitlab
  344. ssh -T git@gitlab.com
  345. Ref: https://docs.gitlab.com/ee/ssh/#working-with-non-default-ssh-key-pair-paths
  346. ssh-keygen -t ed25519 -C "github"
  347. xclip -i -selection clipboard < id_github.pub
  348. ssh -T git@github.com
  349. eval $(ssh-agent -s)
  350. ssh-add /home/saran/.ssh/id_github
  351. ssh -T git@github.com
  352. Ref: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/
  353. generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  354. ssh-keygen -t ed25519 -C "bitbucket"
  355. xclip -i -selection clipboard < id_bitbucket.pub
  356. ssh -T git@bitbucket.org
  357. eval $(ssh-agent -s)
  358. ssh-add /home/saran/.ssh/id_bitbucket
  359. ssh -T git@bitbucket.org
  360. ssh-keygen -t ed25519 -C "notabug"
  361. xclip -i -selection clipboard < id_notabug.pub
  362. ssh -T git@notabug.org
  363. eval $(ssh-agent -s)
  364. ssh-add /home/saran/.ssh/id_notabug
  365. ssh -T git@notabug.org
  366. ssh-keygen -t ed25519 -C "codeberg"
  367. xclip -i -selection clipboard < id_codeberg.pub
  368. ssh -T git@codeberg.org
  369. eval $(ssh-agent -s)
  370. ssh-add /home/saran/.ssh/id_codeberg
  371. ssh -T git@codeberg.org
  372. # conflicting changes in git
  373. 1. Cleaning up the working copy:
  374. Make sure the working copy doesn't contain conflicting changes. There are
  375. two ways to achieve this.
  376. a) saving local changes on a stash
  377. To preserve the local changes, can safely store them on a stash and
  378. available in case want them back at a later point.
  379. $ git stash --include-untracked
  380. b) discarding local changes
  381. If sure that don't need them anymore, can discard local changes completely.
  382. $ git reset --hard
  383. If untracked/new files, will have to use the "git clean" command to get rid
  384. of these.
  385. $ git clean -fd
  386. 2. Pull again
  387. After cleaned up any local changes/untracked files that would have been
  388. overwritten, the pull will finally work.
  389. $ git pull
  390. # rename a local git branch
  391. git branch -m old-name new-name # rename a local branch from another branch
  392. git branch -a # list all -- both local and remote -- branches to verify that it has been renamed
  393. # rename a remote git branch
  394. 1. rename a local branch by following the previous step
  395. 2. then delete the old branch and push the new one
  396. git push origin --delete old-name
  397. git push origin :old-name new-name
  398. 3. reset the upstream branch for new local branch
  399. git push origin -u new-name
  400. Ref: https://www.hostinger.com/tutorials/how-to-rename-a-git-branch/
  401. # modify commit message
  402. git commit --amend -m 'new commit message'
  403. # safe way to rollback to a previous state
  404. To revert everything from the HEAD back to the commit hash, meaning it will recreate
  405. that commit state in the working tree as if every commit after 0766c053 had been
  406. walked back.
  407. git revert --no-commit 0766c053..HEAD
  408. git commit -m '<commit message>'
  409. The --no-commit flag lets git revert all the commits at once, otherwise you'll be
  410. prompted for a message for each commit in the range, littering your history with
  411. unnecessary new commits.
  412. If want to have individual commits (instead of reverting everything with one big
  413. commit), then you can pass --no-edit instead of --no-commit, so that you don't have
  414. to edit a commit message for each reversion.
  415. git revert --no-edit 0766c053..HEAD
  416. # to view what haven't git added yet
  417. git diff file.txt
  418. # to view already added changes
  419. git diff --cached file.txt
  420. git diff --staged file.txt
  421. # force git pull to overwrite local files
  422. No need to fetch all remotes and branches if you're going to reset to the origin/main
  423. branch. So, instead of doing:
  424. git fetch --all
  425. git reset --hard origin/main
  426. I'd advise doing the following:
  427. git fetch origin main
  428. git reset --hard origin/main
  429. # add & commit and file
  430. (works only for already tracked files but not for untracked files)
  431. git commit -am 'commit message'
  432. # add subject and description to a commit message
  433. git commit -m 'subject' -m 'description ...'
  434. # fine grained control files to skip with .gitignore
  435. ignore all .a files: *.a
  436. but do track lib.a, even though you're ignoring .a files above: !lib.a
  437. only ignore the TODO file in the current directory, not subdir/TODO: /TODO
  438. ignore all files in any directory named "build": build/
  439. ignore doc/notes.txt, but not doc/server/arch.txt: doc/*.txt
  440. ignore all .pdf files in the doc/ directory and any of its subdirectories: doc/**/*.pdf
  441. # git rename a file/directory and retain the history
  442. Execute the following commands where the project .git/ directory present and also make
  443. sure all the changes are commited.
  444. Example:
  445. nmsc/Scripts/Python/Euler/
  446. then cd nmsc/
  447. ls
  448. .git/ Scripts/
  449. 1. rename innermost directory path
  450. from Scripts/Python/Euler/ to Scripts/Python/eulerscheme/
  451. git filter-repo --path-rename Scripts/Python/Euler/:Scripts/Python/eulerscheme/ --force
  452. 2. then rename previous innermost directory path
  453. from Scripts/Python/ to Scripts/python/
  454. git filter-repo --path-rename Scripts/Python/:Scripts/python/ --force
  455. 3. finally rename the outermost directory path from Scripts/ to scripts/
  456. git filter-repo --path-rename Scripts/:scripts/ --force
  457. then, 'git pull --rebase origin <branch>' before 'git push origin <branch>'
  458. # git pull merge/rebase/fast-forward
  459. hint: You have divergent branches and need to specify how to reconcile them.
  460. hint: You can do so by running one of the following commands sometime before
  461. hint: your next pull:
  462. hint:
  463. hint: git config pull.rebase false # merge
  464. hint: git config pull.rebase true # rebase
  465. hint: git config pull.ff only # fast-forward only
  466. hint:
  467. hint: You can replace "git config" with "git config --global" to set a default
  468. hint: preference for all repositories. You can also pass --rebase, --no-rebase,
  469. hint: or --ff-only on the command line to override the configured default per
  470. hint: invocation.
  471. # add all modified files
  472. git add -u
  473. # git bare directory (to maintain dotfiles)
  474. git init --bare # initialize bare git repository
  475. git init --bare $HOME/.dotfiles
  476. echo "alias config='/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME'" >> $HOME/.bashrc
  477. config config --local status.showUntrackedFiles no
  478. $ cd $HOME
  479. $ config add .bashrc
  480. $ config commit -m 'add bash configuration file'
  481. $ config push
  482. # using git restore to undo git add
  483. Undoing a git add can done simply by the git restore --staged command on the
  484. attected file:
  485. $ git restore --staged file.txt
  486. This will remove the file from Git's staging area, making sure it is not part of
  487. the next commit.
  488. If, at the same time, you also want to discard any local changes in this file,
  489. you can simply omit the --staged flag:
  490. $ git restore file.txt
  491. This will undo any modifications in this file since you last committed it.
  492. Please be careful with this command: undoing uncommitted local changes cannot be
  493. undone!
  494. Ref: https://www.git-tower.com/learn/git/faq/undo-git-add
  495. # combine git repositories with unrelated histories
  496. To merge repositoryA into repositoryB:
  497. $ cd path/to/repositoryB
  498. $ git remote add repositoryA /path/to/repositoryA
  499. $ git fetch repositoryA --tags
  500. $ git merge --allow-unrelated-histories repositoryA/main
  501. $ git remote remove repositoryA
  502. In case you want to put repositoryA into a subdirectory, you can use
  503. git-filter-repo (filter-branch is discouraged). Run the following commands
  504. before the commands above:
  505. $ cd path/to/repositoryA
  506. $ git filter-repo --force --to-subdirectory-filter repositoryA
  507. Note: In above command only commited files are taken, the uncommited files are
  508. not taken even if it present in the repositoryA. A seperate directory with
  509. repositoryA is created within the existing repositoryA directory.
  510. Example:
  511. To merge julia/ into a subdirectory in codelearn/:
  512. $ cd ~/julia/
  513. $ git filter-repo --force --to-subdirectory-filter julia
  514. $ cd ~/codelearn/
  515. $ git remote add julia ~/julia
  516. $ git fetch julia --tags
  517. $ git merge --allow-unrelated-histories julia/main
  518. $ git remote remove julia
  519. Reference:
  520. stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories
  521. https://jeffkreeftmeijer.com/git-combine/
  522. # create commit with different date
  523. $ GIT_AUTHOR_DATE='Mon May 18 19:32:10 2020 +0530' \
  524. GIT_COMMITTER_DATE='Mon May 18 19:32:10 2020 +0530' \
  525. git commit -m 'Commit message'
  526. # add multi-line git commit messages
  527. $ git commit program.c -m "Note the Changes" -m "- Fixed a bug"
  528. # rename the master branch to main
  529. renaming the local master branch to main:
  530. The first step is to rename the "master" branch in your local git repositories
  531. $ git branch -m master main
  532. Let's quickly check if this has worked as expected
  533. $ git status
  534. On branch main
  535. Your branch is up to date with 'origin/master'.
  536. nothing to commit, working tree clean
  537. renaming the remote master branch as well:
  538. Make sure your current local HEAD branch is still "main" when executing the
  539. following command
  540. $ git push -u origin main
  541. We now have a new branch on the remote named "main". Let's go on and remove the
  542. old "master" branch on the remote
  543. $ git push origin --delete master
  544. [Reference: https://www.git-tower.com/learn/git/faq/git-rename-master-to-main/]
  545. # create a pull request
  546. When you want to work on a GitHub project (https://github.com/rrthomas/pdfjam.git),
  547. the first step is to fork a repo. This creates a new copy of pdfjam repo under your
  548. GitHub user account with a URL like:
  549. https://github.com/dsarvan/pdfjam
  550. The copy includes all the code, branches, and commits from the original repo. Next
  551. clone the repo by opening the terminal and running the command:
  552. git clone https://github.com/dsarvan/pdfjam
  553. Once the repo is cloned, you need to do two things:
  554. 1. Create a new branch by issuing the command:
  555. git checkout -b refactor/pdfjam
  556. 2. Create a new remote for the upstream repo with the command:
  557. git remote add upstream https://github.com/rrthomas/pdfjam
  558. In this case, upstream repo refers to the original repo you created your fork from.
  559. Now you can make changes to the code. The following code push it to refactor/pdfjam
  560. branch.
  561. git push -u origin refactor/pdfjam
  562. Once you push the changes to your repo, the 'Compare & pull request' button will
  563. appear in rrthomas/pdfjam Github page. Click it and you'll be taken to 'Open a
  564. pull request'. Open a pull request by clicking the 'Create pull request' button.
  565. This allows the repo's maintainers to review your contribution. From here, they
  566. can merge it if it is good, or they may ask you to make some changes.
  567. Reference: https://opensource.com/article/19/7/create-pull-request-github
  568. # git branch naming practices
  569. Prefixing your branch names with a type helps categorize branches and provides
  570. additional context to everyone working on the project. Some commonly used types
  571. include bugfix/, develop/, feature/, hotfix/, main/, refactor/, release/.
  572. * develop/ is the branch you'll be doing most of your work off of; it's also
  573. the branch that represents the code to be deployed in the next release.
  574. * feature/ branches represent non-trivial features and fixes that have not yet
  575. been deployed (a completed feature/ branch is merged back into develop/).
  576. * updating main/ is done through the creation of a release/.
  577. * main/ is always 'production ready' code. Commits are never made directly to
  578. main/. Rather, code on main/ only gets there after a production release/
  579. branch is created and finished. Thus the code on main/ is always able to be
  580. released to production. Also, main/ is always in a predictable state, so you
  581. never need to worry if main/ (and thus production) has changes one of your
  582. other branches doesn't.
  583. * develop/ branch is where most of your work is done. This branch contains all
  584. of the completed features and bug fixes yet to be released; nightly builds or
  585. continuous integration servers should target develop, as it represents the
  586. code that will be included in the next release. For one-off commits, feel free
  587. to commit to develop/ directly.
  588. * for larger features, a feature/ branch should be created. feature/ branches
  589. are created off of develop/. They can be small enhancements for the next
  590. release or further out changes that, nonetheless, need to be worked on now. To
  591. start work on a new feature, use:
  592. $ git flow feature start <feature name>
  593. This creates a new branch, feature/<feature name>. Commits are then made to
  594. this branch as normal. When the feature is complete and ready to be released
  595. to production, it should be merged back into develop/ using the following
  596. command:
  597. $ git flow feature finish <feature name>
  598. This merges the code into develop/ and deletes the feature/<feature name>
  599. branch.
  600. * release/ branch is created from develop/ when you're ready to begin a
  601. production release. Create one using the following command:
  602. $ git flow release start <release number>
  603. Note that this is the first time a version number for the release is created.
  604. All completed and ready to be released features must already be on develop/
  605. (and thus feature finished). After your release branch is created, release
  606. your code. Any small bug fixes needed after the release are made directly to
  607. the release/<release number> branch. Once it has settled down and no more bug
  608. fixes seem necessary, run the following command:
  609. $ git flow release finish <release number>
  610. This merges your release/<release number> changes back into both main/ and
  611. develop/, meaning you never need to worry about either of those branches
  612. lacking changes that are in production (perhaps as the result of a quick bug
  613. fix).
  614. * while potentially useful, hotfix/ branches are, I would guess, little used in
  615. the real world. hotfix/ is like a feature/ branch off of main/: if you've
  616. already closed a release/ branch but realize there are vital changes that need
  617. to be released, create a hotfix/ branch off of main/ (at the tag created
  618. during $ git flow release finish <release number>) like so:
  619. $ git flow hotfix start <release number>
  620. After you make your changes and bump your version number, finalize the hotfix/
  621. via
  622. $ git flow hotfix finish <release number>
  623. This, like a release/ branch (since it essentially is a type of release
  624. branch), commits the changes to both main/ and develop/.
  625. The reason I assume they're rarely used is because there is already a
  626. mechanism for making changes to released code: committing to an un-finished
  627. release branch. Sure, in the beginning, teams may git flow release finish ...
  628. too early, only to find they need to make some quick changes the next day.
  629. Over time, though, they'll settle on a reasonable amount of time for a
  630. release/ branch to remain open and, thus, won't have a need for hotfix/
  631. branches. The only other time you would need a hotfix/ branch is if you needed
  632. a new 'feature' in production immediately, without picking up the changes
  633. already in develop/. That strikes me as something that happens (hopefully)
  634. very rarely.
  635. # git commit template
  636. A properly formed Git commit subject line should always be able to complete
  637. the following sentence:
  638. * If applied, this commit <will your subject line here>
  639. ** Example:
  640. [type](optional scope): [subject]
  641. [optional body]
  642. [optional footer]
  643. ** Type
  644. Must be one of the following:
  645. * build - Build related changes
  646. * chore - Build process or auxiliary tool changes
  647. * docs - Documentation only changes
  648. * feat - A new feature
  649. * fix - A bug fix
  650. * perf - A code change that improves performance
  651. * refactor - A code change that neither fixes a bug or adds a feature
  652. * revert - Reverting things
  653. * style - Markup, white-space, formatting, missing semi-colons...
  654. * test - Adding missing tests
  655. ** Subject
  656. The subject contains a succint description of the change:
  657. * Use the imperative, present tense: "change" not "changed" nor "changes"
  658. * No dot (.) at the end.
  659. ** Scope
  660. A scope may be provided to a commit’s type, to provide additional contextual
  661. information and is contained within parenthesis.
  662. e.g., feat(parser): add ability to parse arrays
  663. ** Body
  664. Just as in the subject, use the imperative, present tense: "change" not "changed"
  665. nor "changes".
  666. The body should include the motivation for the change and contrast this with previous
  667. behavior.
  668. ** Rules
  669. The 7 rules of a great commit message
  670. 1. Separate subject from body with a blank line
  671. 2. Limit the subject line to 50 characters
  672. 3. Summary in present tense. Not capitalized
  673. 4. Do not end the subject line with a period
  674. 5. Use the imperative mood in the subject line
  675. 6. Wrap the body at 72 characters
  676. 7. Use the body to explain what and why vs. how
  677. # merge develop branch into main
  678. To merge develop branch into the main branch - you checkout main and merge develop:
  679. $ git checkout develop
  680. # ...develop some code...
  681. $ git add .
  682. $ git commit -m 'some commit message'
  683. $ git checkout main
  684. # ...switched to branch main...
  685. $ git merge develop
  686. Reference: https://stackabuse.com/git-merge-branch-into-master/
  687. # undo latest local commit
  688. Let's create a file we'd like to add to our repository, add it and finally commit:
  689. $ echo "Hello World!" >> file.txt
  690. $ git add file.txt
  691. $ git commit -m 'added file.txt'
  692. $ echo "It's a great day today :(" >> file.txt
  693. $ git add file.txt
  694. $ git commit -m 'modified file.txt'
  695. Yikes, we've accidentally left in a horrible typo in the content. We've put in
  696. the parentheses in the wrong way! And we've just committed that mistake:
  697. $ git log --pretty=oneline
  698. df9fc1b773ecf3ba14c990615831f1087817611f (HEAD -> main) Modified file.txt
  699. 55db4f399d1ad64e0a40e1858d23fef0ffe31fb0 Added file.txt
  700. To remove a local commit, assuming it hasn't been pushed to the remote repository yet,
  701. we can use the `git reset` command, which is effectively the opposite of `git add`:
  702. $ git reset HEAD~
  703. Unstaged changes after reset:
  704. M file.txt
  705. We've reset the HEAD (pointer to the last commit), pointing it back (~) to the
  706. previous commit. By including a number after the tilde, we could've gone back
  707. multiple commits instead of just one.
  708. Once reset, the change we've made to the file.txt, i.e. the erroneous modification is
  709. unstaged. Let's take a look at the log again:
  710. $ git log --pretty=oneline
  711. 55db4f399d1ad64e0a40e1858d23fef0ffe31fb0 (HEAD -> main) Added file.txt
  712. And the status is:
  713. $ git status
  714. On branch main
  715. Changes not staged for commit:
  716. (use "git add <file>..." to update what will be committed)
  717. (use "git restore <file>..." to discard changes in working directory)
  718. modified: file.txt
  719. no changes added to commit (use "git add" and/or "git commit -a")
  720. However, what are the contents of the file now?
  721. $ nano file.txt
  722. Hello World
  723. It's a great day today :(
  724. By default, the reset command is --soft. The --soft flag doesn't reset the changes
  725. done to the file, just removes the commit that was made. Let's go ahead and re-commit
  726. this mistake again, so we can take a look at what happens when we run the --hard
  727. option.
  728. Hard Reset:
  729. Instead of the --soft reset, which we can use to simply undo the commit, while leaving
  730. the files intact (and the changes still present, which is why the git status command
  731. prompted us with staging the changes) - we can also do a --hard reset:
  732. $ git reset --hard HEAD~
  733. HEAD is now at 55db4f3 Added file.txt
  734. This time around, the changes aren't unstaged, like before. They're removed. If we
  735. check the log, it'll look much like last time:
  736. $ git log --pretty=oneline
  737. 55db4f399d1ad64e0a40e1858d23fef0ffe31fb0 (HEAD -> main) Added file.txt
  738. Though, if we check the status:
  739. $ git status
  740. On branch main
  741. nothing to commit, working tree clean
  742. There's nothing to commit, because the change that was made to the file in the
  743. previous commit was also removed, setting the HEAD back to the first commit:
  744. $ nano file.txt
  745. Hello World!
  746. Reference: https://stackabuse.com/git-undo-latest-local-commit/
  747. # adding multi-line commit messages
  748. $ git commit index.js -m "My Changes" -m "- Fixed a critical bug" -m "- Probably
  749. added more bugs"
  750. These multiple messages will then look like this in the resulting commit:
  751. My Changes
  752. - Fixed a critical bug
  753. - Probably added more bugs
  754. Another option, which depends on the shell you're using, is to just enter a single or
  755. double quote and press Enter, without closing the quote. This works well in Bash,
  756. which doesn't enter the command until you've closed the quote:
  757. $ git commit index.js -m "My Changes
  758. - Fixed a critical bug
  759. - Probably added more bugs
  760. "
  761. And finally, you don't actually need to use the -m flag at all. If you omit this flag
  762. then Git will automatically open a text editor for you to enter the commit message.