# Git Notes [TOC] ## Configure ``` git config --global user.email "you@example.com" git config --global user.name "Your Name" ``` ## Typical worklfow Starting on master, get changes from origin using `git pull <repository> <branch>` ``` git pull origin master ``` Create working branch ``` git checkout -b feature-branch ``` When ready to commit changes add files to commit `git add file/path` then ` git commit`, make sure to add a meaningful commit message. Push your changes to the repositry with `git push` ``` git push --set-upstream origin feature-branch ``` Log in to ado/github and navigate to repository you pushed changes to. You should be prompted to open a pullrequest. Once opened keep an eye on CI failures, fix it as needed. ### Merge Conflicts If there are merge conflicts between your branch and master branch, rebase your branch on master, see [rebase-my-branch-with-master](https://hackmd.io/n1bgqM-gSK-annVrmDw_Pg#rebase-my-branch-with-master). ## Cherry pick changes from another local repo Answer from https://stackoverflow.com/questions/5120038/is-it-possible-to-cherry-pick-a-commit-from-another-git-repository ``` git --git-dir=../<some_other_repo>/.git \ format-patch -k -1 --stdout <commit SHA> | \ git am -3 -k ``` for example, pull changes from hawkowl/ARO-Installer/ to openshift/ARO-Installer ``` git --git-dir=/home/tschneid/go/src/github.com/hawkowl/ARO-Installer/.git \ format-patch -k -1 --stdout 8bf92fbea179ebca5b3697f7ecbd3c96d9f94175 | \ git am -3 -k git --git-dir=/home/tschneid/go/src/github.com/hawkowl/ARO-Installer/.git \ format-patch -k -1 --stdout 7fcd385070f5370240cb76151a2cd32f2e07aa3f | \ git am -3 -k git --git-dir=/home/tschneid/go/src/github.com/hawkowl/ARO-Installer/.git \ format-patch -k -1 --stdout 70a2ef97e6bc9772701ca80e541b50b394c72b29 | \ git am -3 -k ``` ## Tags ### Create/push a tag ``` git tag -a v20220729.01 -m 'Release v20220729 build 01' git push upstream v20220729.01 ``` ### Checkout repo at tag ``` # fetch all tags git fetch --all --tags # checkout tag git checkout tags/<tag> ``` ## Git workflow ### Show remotes ``` $ git remote -v origin https://github.com/tony-schndr/ARO-RP.git (fetch) origin https://github.com/tony-schndr/ARO-RP.git (push) upstream https://github.com/Azure/ARO-RP.git (fetch) upstream https://github.com/Azure/ARO-RP.git (push) ``` ### create new branch ``` git checkout -b my-branch git commit -m "Test commit" git push -u origin my-branch ``` ### update origin with upstream (GitHub/Fork workflow) ``` git checkout <main/master> git pull upstream master git push origin master ``` ``` git checkout master git pull upstream master git push origin master ``` ### rebase my-branch with master ``` git checkout my-branch git rebase master git push --force-with-lease ``` ### Squash ``` git rebase -i <after this commit> git rebase -i HEAD~5 ``` ### Stash ``` git stash save "good description here" ``` ``` git stash pop ``` ### Fetch a pull request Add to .bashrc ``` function gitpr() { if [ -z "${1}" ]; then echo "PR is not valid" else git fetch upstream pull/${1}/head:pr${1} git checkout pr${1} fi } ``` then run `gitpr <pr num>` in the bash shell ## Revert Commit ``` git revert 13c3290ffcde7ef05bc447286b8c8b6731ecbd18 6e1acd4f13446276a09bb5f74125350bc69d58a8 b2273bdc6a42e4733cd21dc2c73ecc789bb323d9 ``` ## Branches Create branch from main, carry changes to new branch ``` git checkout $branch_name ``` To rename the current branch: ``` git branch -m <newname> ``` To rename a branch while pointed to any branch: ``` git branch -m <oldname> <newname> ``` -m is short for --move. To push the local branch and reset the upstream branch: ``` git push origin -u <newname> ``` To delete the remote branch: ``` git push origin --delete <oldname> ``` To create a git rename alias: ``` git config --global alias.rename 'branch -m' ``` On Windows or another case-insensitive filesystem, use -M if there are only capitalization changes in the name. Otherwise, Git will throw a "branch already exists" error. ``` git branch -M <newname> ``` ### Clone Clone contents of repo to a directory ``` git clone https://github.com/Br35Ba56/ansible-datapower.git ansible_collections/community/datapower ``` --- ## Working with Remotes ### Show remotes ```bash= git remote -v ``` ### refresh non-main branch from remote main ```bash= git merge origin/master` ``` ### Point an exsiting repo to a new one. ```bash= git remote rename origin old-origin git remote add origin https://<new repo> ``` ### Remove a remote by name ```bash= git remote remove <old-origin> ``` ### Change the remote origin url ```bash= git remote set-url origin <repo url> ``` ### Setup a local branch to track a remote branch, this will point local "master" branch to the remote named "origin" "master" branch ```bash= git branch --set-upstream-to=origin/master master ```