# Useful Commands

# General

git pull
    Pull the code from the repo you have cloned the code from but will not create or delete, the new other branches from the repo
git delete repo
    rm -rf .git
git delete last commit
    git reset --hard HEAD~1
git show subtrees
    git log | grep git-subtree-dir | awk '{ print $2 }'
git push subtree
    git subtree push --prefix=mod/ojt totara-mod-ojt OJT_13
git log <branch-name> --author = <author-name>; git log wr362986 --author="Sumaiya"
    get a list of commits done by me on a branch
git show <commit-id> | grep Author; git show c67423a83f71e6d69c782f6f9ef59b0434b3755d | grep Author
    get the name of the author who did a commit
git symbolic-ref HEAD
    get the name of the current branch only
git cherry-pick <commit-number>
    git cherry-pick 3b0a9e4349a68daf25d0ee191ab986d7df4343e0
    you can add a commit with an '-edit' flag
Git merge conflicts
    search for the below to see if by mistake you have saved files without resolving conflicts
        <<<<<<< HEAD

# diff

git diff
    find difference between local holder and remote folder
        git diff <local-branch> <remote-name>/<remote-branch> (--name-only or --dirstat)
        git diff master postgresfulltext/master
    git diff local vs remote file
        git diff remotename/branchname:remote/path/file1.txt local/path/file1.txt
        git diff origin/mdl39-ucace-testing:analytics/classes/manager.php analytics/classes/manager.php
        git diff origin/MOODLE_39_STABLE:lang/en/theme_catawesome.php moodleorigin/mdl39-growsafe-testing:theme/catawesome/lang/en/theme_catawesome.php

# undo

git undo last pull
    git reset --hard
git undo last revert
    git reset --hard HEAD^

# branch

Delete branch
    local
        git branch -d <local-branch>
    remote
        git push origin --delete <remote-branch-name>
Push to remote branch
    git push -u <remote> <branch-name>
    git push -u origin t13-local-wr365280
    git push code to upstream
create remote branch and push to it
    git branch <new-branch> <existing-branch> (create a new branch from an existing branch) (if needed)
    git checkout <new-branch>
    git push -u origin <new-branch>
git track a branch with another branch on remote
    - git branch <local branch name> -u <remote>/<remote branch name>. For eg, git branch mdl310-local -u origin-local/mdl310-local
        make sure the branch exists on remote and has been fetched (git fetch --all)
    - git checkout --track <remote>/<remote branch name>
        git checkout --track origin-local/t13-local-hrimportlogsdelete
    - git branch --set-upstream-to origin/TOTARA_13_STABLE
git working with different branches
    git checkout using the -t flag
    git checkout -t origin/master

# rebase

Git rebase from one branch to another
    https://docs.moodle.org/dev/User:Sam_Hemelryk/My_Moodle_Git_workflow#Rebasing_after_the_weekly_release
Pull back changes from history
    pull back files in a directory or a specific file which has been deleted or edit and committed. For eg, I had deleted all the files for ethnicity_base, ethnicity_chinese etc and wanted to later retrieve them.
    git checkout <commit-hash> -- <.folder-name> (make sure there is a dot before the folder and then slash)
    git checkout b6e64e39c156f627b4169bf1d9afa5efef1c8c22 -- ./local/uceas/classes/analytics/indicator/
    squashing last x no of commits
    git rebase -i <commit-hash-onebefore-the-change> (then replace pick with squash except the firstmost )
    or
    git rebase -i HEAD~3
    retrieve changes from a specific commit
    git checkout <commit-hash>
    See filenames since a commit
    git diff --name-only fdd70eb6646be1ae957bed7c363261a77f117f4b

# history

See changes on another branch specific folder
    git log t12-nzpolicecep-prod -- mod/checklist
See changes done to a file in the entire history
    git log -- <file-path>
Remove a file from last commit
    git reset HEAD^ -- path/to/file
    git commit --amend --no-edit
See history of all branches in a repo on a particular folder or file
    git log --all -- path/to/file
See history of changes on a specific folder by a specific author
    git log --author=catalyst mod/workshop/
Find history of a specific line in a file
    git log -L704,+1:'local/ace/locallib.php'

# merge

when you want to merge between two branches and accept incoming change
    git merge --strategy-option theirs <remote>/<branch>
        git merge --strategy-option theirs origin/mdl39-catalystdemo1-testing

# rebase vs merge

- merge stitches them together (roughly) rebase takes one branch and places it on top of the other branch as if all the commits had been made after the latest commit on the branch being rebased to
- a merge or rebase usually takes place once you have finished all development on your local feature branch and want to add it back to a specific client branch

# cherry-pick

How to cherry-pick changes to a different folder structure
    - Not exactly what you are looking for here but this is a useful alternative https://www.codefull.net/2018/11/specify-target-file-in-git-cherry-pick/

# Debugging

Below example git commands to investigate the history of code in a specific repo. For e.g. while upgrading between versions of totara, you would like to check if the code customization of a specific plugin or core.

git log --author=catalyst course/modlib.php

git show bce18c291372378ee62ff15d11338ebb78f83857

git blame origin/t12-nzpolicecep-testing:course/modlib.php | grep bce18c291

Find log with a keyword
    git log --pickaxe-regex -p --color-words -S "TL-32705"

git bisect

git log --grep=
This might be useful to others who like me have been using git log | grep ... to search the git history of a repo: git log --grep= is a thing. It's more tidy than piping into grep (it keeps the syntax highlighting and you don't have to specify how many of the preceding or following rows you want) and allows to also use --invert-grep, which I've been using to remove MDL- commits when investigating a file with a lot of customisations (that will also remove backported commits, so to use with care).

# Undo "git commit --amend"

git reset --soft HEAD@{1}

# Undo git add or unstaged files or folders

git clean -fdx

# Branch has diverged

git pull --rebase
https://coderwall.com/p/7aymfa/please-oh-please-use-git-pull-rebase

# Create Patch file from commit

git format-patch -<n> <SHA-1> --stdout > <name_of_patch_file>.patch
git format-patch cc1dde0dd^..6de6d4b06 --stdout > foo.patch
git format-patch -1 <sha>
git format-patch -1 HEAD

# Apply patch file

git apply --ignore-space-change --ignore-whitespace mychanges.patch

# Apply patch in a different directory structure

git format-patch 611a6a8bac73bdcf4e1bd2641dca5eb880c7cae0^..4d9926dc311a1a1d9281aed8cd142498ca0862db --stdout > ~/working/WRMS/patch1
(manually change the directory)
git apply --3way --ignore-space-change --ignore-whitespace ~/working/WRMS/patch1
https://www.codefull.net/2018/11/specify-target-file-in-git-cherry-pick/

# Save credentails

Why is Git always asking for my password?

ssh-add -K ~/.ssh/id_rsa
git config --global credential.helper cache

https://stackoverflow.com/questions/35942754/how-can-i-save-username-and-password-in-git

# Plugin maintenance inside another repo

git checkout -b <client-branch>
cd path/to/plugin
git init
git branch -m <newbranch-with-fixes>
git add .
git commit -m "init"
git remote add origin <git-repo>
git pull
git checkout <exitingbranch-oldupdates-foreg-master>
git diff <exitingbranch-oldupdates-foreg-master> <newbranch-with-fixes> > mydiff.diff
git apply mydiff.diff
git push origin <newbranch-with-fixes>
rm -rf .git

# Composer git authentication issue

Error Could not authenticate against github.com can be resolved by adding token to composer at project level or globally
To add globally do the below
nano  /home/sumaiyajaved/.config/composer/auth.json
{
    "http-basic": {
        "github.com": {
        "username": "<username>",
        "password": "<add token here>"
        }
    }
}

# Cannot use ssh in git repo

Making possible solutions but for me removing CanonicalDomains and CanonicalizeHostname resolved it.

# Copy files or folder from one branch to another

git restore --source master dirname
git restore --source mdl401-otagopoly-prod course/format/multitopic/

# Git picking changes not made

It could be because of line ending issue.
You can check by using "git diff -w". It will ignore whitespace differences and if the line disappear it's a line ending issue.
Use a tool called "dos2unix" on the files to ensure they all have linux line endings

# Git find if a subplugin has a specific version

Okay, based on doing git grep "'minorVersion' => 27" $(git branch -a | grep '/mdl401-.*-prod$') -- h5p, it appears we have not done this upgrade to any of our existing mdl401 sites