Skip to content

Latest commit

 

History

History
492 lines (411 loc) · 14 KB

utils-git.md

File metadata and controls

492 lines (411 loc) · 14 KB

Table of Contents generated with DocToc

GIT

TUTORIAL

TOOLS

OSS

GITLAB

ACTIONS

ACTIONS: EXT

TEMPLATES

MOST USED

ssh-keygen -t rsa (create a ssh certificate then import it:account>setting)
git init (new blank repository)
git remote add origin url://to/source/repository (add a remote)
git status (get infos)
git stash (save local diff)
git stash branch myfeature  (restore the stashed diff on a new branch)
git stash pop (restore the stashed diff on current branch)
git fetch origin <sha1-of-commit-> (fetch a commit or branch or tag)
git show d42c389f:path-to-file.ts > histo.txt (load previous file from hash)
git rev-parse HEAD (get last commit hash)
git checkout d42c389f (check out a particular commit)
git cherry-pick d42c389f (apply any commit on current branch)
git cherry-pick -e d42c389f (same as above + change commit message)
git cherry-pick -m 1 d42c389f (apply any merge commit on current branch)
git revert d42c389f (revert any commit on current branch)
git revert -m 1 d42c389f (revert any merge commit on current branch)
git remote prune origin --dry-run (check local branches that have been deleted from remote)
git remote prune origin (clean local branches that have been deleted from remote)
npx git-removed-branches --prune --force (clean local branches that have been deleted from remote)
git branch -d branchname (delete local branch)
git branch -D branchname (delete local branch force)
git config --get remote.origin.url (check the remote url)
git commit --amend -m "New commit msg" (edit wrong unpushed commit message)

CLONE

  • Clone existing project into project folder
git clone -b my-branch git@github.com:user/myproject.git
  • Clone existing project into the current folder (.)
git clone -b my-branch git@github.com:whatever .
  • Clone existing project into the specific folder (my-name)
git clone -b my-branch git@github.com:whatever my-name

INIT

  1. Setting up a remote repository using web github/bitbucket
  2. Setting up your local repository and commit
git init
git add *
git commit -m "init project"
  1. Add origin bitbucket or git
git remote add origin https/or/git/url
git push origin master
git push -u origin --all

RESET

  • Clean deleted branch:
git fetch --prune
  • Different reset possible
git fetch origin
git reset --soft  (Uncommit changes, changes are left staged (index))
git reset --mixed (default) (Uncommit + unstage changes, changes are left in working tree)
git reset --hard: (Uncommit + unstage + delete changes, nothing left)
  • Reset to match the remote branch
git reset --soft HEAD~1 (Undo last local commit)
git reset --hard origin/master (Undo all the local commits)
git reset --hard 3c74a11530697214cbcc4b7b98bf7a65 (Reset to commit)
git reset --hard (Reset any local changes)
  • Remove local files and dir
git clean -n -f (to see)
git clean -f (to execute)
git clean -fd (and dir)
  • Reset local
rm -Force -Recurse .git

STASH

Find advanced command to use stash feature as much as possible:

git stash save "your message" 
git stash save -u "your message"
git stash list
git stash apply stash@{1} # or 'stash@{1}' on windows
git stash pop
git stash pop stash@{1} # or 'stash@{1}' on windows
git stash show
git stash show -p
git stash show stash@{1} # or 'stash@{1}' on windows
git stash branch <name> stash@{1} # or 'stash@{1}' on windows
git stash clear
git stash drop stash@{1} # or 'stash@{1}' on windows

More detail: https://www.freecodecamp.org/news/useful-tricks-you-might-not-know-about-git-stash-e8a9490f0a1a/

HASH

To get the full SHA:

$ git rev-parse HEAD
cbf1b9a1be984a9f61b79a05f23b19f66d533537

To get the shortened version:

$ git rev-parse --short HEAD
cbf1b9a

CREATE BRANCH

  1. develop on base branch..
  2. checkout
git checkout -b branchName
  1. commit and push
git commit on ide
git push --set-upstream origin 1109-issue-name
git push -u origin 1109-issue-name

OR

  1. develop on base branch..
  2. stash and create branch from stash
git stash
git stash branch 1109-issue-name
  1. commit and push
git commit on ide
git push --set-upstream origin 1109-issue-name
git push -u origin 1109-issue-name

MERGE/REBASE

MERGE BRANCH

Example for merging master into custom_branch

git checkout master
git pull
git checkout custom_branch
git merge master
git merge --abort (cancel)

You can also use Gitlab/Bitbucket explorer or VSCode plugin "Git merger":

  • from custom_branch > CTRL+SHIFT + P > Git: Merge from > Master

REBASE BRANCH

Rebasing works well when there is not so many commit in your branch else consider squashing all your commits into one before rebasing: Example for rebasing master into custom_branch

# example by pulling the master locally first
git checkout master
git pull
git checkout custom_branch
git rebase master

# example without pulling develop locally first
git checkout custom_branch
git fetch -a
git rebase origin/develop

# finalize
git rebase --continue (continue the rebase)
git rebase --abort (cancel the rebase)
git push --force (send to remote)

RENAME BRANCH

# rename the master branch to another old, then rename seotweaks to master and push force
git branch -m master old-master
git branch -m seotweaks master
git push -f origin master

CONTRIBUTE

  1. Fork the github remote repository
  2. Checkout your fork in your machine
  3. Set the source repository as upstream
  4. Fetch the full upstream repository.
  5. Create branch, do your changes, push
  6. Create a PR from your repo to the upstream
on the GitHub website fork the repository in question (called ‘upstream’) # step 1
git clone https://github.com/{user}/{your-repo}.git # step 2
git remote add upstream https://github.com/{user}/{source-repo}.git # step 3
git fetch upstream # step 4
git merge upstream/main # step 5
git checkout -b my-branch & git commit & git push # step 6
on the GitHub website open a PR for your branch: my-branch # step 7

CONFIG

Switch credentiel method (token)

git config user.email

git config --global credential.helper wincred
git config --global credential.helper manager

git remote rm origin
git remote add origin https://user:password@github.com/pegaltier/utils-dev.git

ALIAS

https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

git config --global alias.pul "pull origin master"
git config --global alias.pus "push origin master"
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

VSCODE SSH

start-ssh-agent
code

SUBMODULES

  • switch branch:
git pull origin name
git submodule update
  • reset config:
# go to the branch with gitmodules conf
git checkout integration
# get the latest changes
git pull
# copy the new URL to your local config
git submodule sync --recursive
# update the submodule from the new URL
git submodule update --init --recursive
  • pin a submodule
cd submodule
git checkout 1eeda2931087d0d9ab693484e0b0d41e13cd9cb1
cd ..
git commit -m "pins latest @submodule"

PAGES

MULTI SSH

https://gist.github.com/jexchan/2351996 https://www.freecodecamp.org/news/how-to-manage-multiple-ssh-keys/ https://superuser.com/questions/272465/using-multiple-ssh-public-keys/272613 https://stackoverflow.com/questions/32212593/multiple-ssh-keys-are-not-working

git encrypts/decrypt network traffic using this private/public key mechanism if you have a pr in ~/.ssh/id_rsa then ssh will automaticly use it you can also use ssh-agent to manage more keys manually but you have to repeat each time you reboot since ssh-agent loose config on restart of your computer so best way is to use the ~/.ssh/config file in addition to that you can want to configure per repo your user: git config user.name "Your Pseudo Here" git config user.email your@email.com

# multi key config:

# ssh-agent commands:
# ssh-add -l # list all ssh keys
# ssh-add -D # delete all ssh keys
# ssh-add ~/.ssh/id_rsa # add a specific key
# ssh-add ~/.ssh/id_ed25519
#
# ssh-config example:
# man ssh_config
#
Host *github.com
  Hostname github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentitiesOnly yes
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519_github

Host *company.com
  Hostname company.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentitiesOnly yes
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_company