Skip to content

Instantly share code, notes, and snippets.

@briancodes
Last active July 14, 2023 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briancodes/adb200244688e24882c46b8faed76fa5 to your computer and use it in GitHub Desktop.
Save briancodes/adb200244688e24882c46b8faed76fa5 to your computer and use it in GitHub Desktop.

When Things Go Wrong...

Multiple Git Accounts

KDiff3 Setup

Alternatively check out Meld Merge which is highest rated on Slant

https://sourceforge.net/projects/kdiff3/files/latest/download

KDiff3 Setup

git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path "C:/dev/kdiff3/kdiff3.exe"
git config --global mergetool.kdiff3.trustExitCode false
git config --global mergetool.keepBackup false
// For .origni files, also go to: Settings > Configure > Directory > Backup (.orig) - uncheck

// This updates the .gitconfig
[merge]
	tool = kdiff3
[mergetool "kdiff3"]
	path = C:/dev/kdiff3/kdiff3.exe
	trustExitCode = false
[mergetool]
	keepBackup = false

While on feature branch:

Reset commit that has not been pushed

git reset HEAD~

This is how VSCode does it

git reset --soft HEAD~

Resset/Squash last 3 commits

git reset --soft HEAD~3
git commit -m 'commit message
git push --force

Note: undo if you reset too far back

git reset HEAD@{1} - StackOverflow How to Undo Reset

vscode: Stash (include untracked)

git stash save -u -- blog home stash

Git Config Location

git config --list --show-origin

Remove files from git, without deleting

actually does seem to delete when syncing - need to check this

git rm -r --cached ./keys

Skip Worktree - ignore future changes

need to be the same on each branch, otherwise won't allow branch checkout

git update-index --skip-worktree keys/CONTENTFUL_CDA.ts

with --no-skip-worktree to revert

List the files being skipped

git ls-files -v . | grep ^S

  • -v verbose, and grep by:
  • H cached,
  • S skip-worktree,
  • M unmerged,
  • R removed/deleted,
  • C modified/changed,
  • K to be killed

Git Workflow - fork, clone, branch, merge, pull request

origin (your repo), remote (local), upstream (original)

Change the origin of a cloned project

...that's not forked, alternatively just fork it like a normal person

git remote set-url origin git:new.url.here

remove a remote

git remote rm upstream-remote

Get update from remote,

  • and prune any merged local branches that have been deleted on the remote

git remote update --prune origin-remote

After you fork a repo

git clone https:sdfadf/asdf.git

git remote -v

  • will have implicit origin of your remote repo (the original that you forked is not in your git locally)

git branch - to show branches

git pull origin master - fetch and merge from origin to master

git checkout -b comment-branch - creates a new branch and selects it. Make a change

git add . - staged

git status - see changes

git commit -m "comment to add to commit"

NOTE -u (upstream) sets tracking for the branch - e.g. sync in VSCode and pull/push will refer to origin as opposed to upstream

Using -u (tracking changes) CREATES A CONNECTION between the remote and local branches, allowing shorthand "git pull" and "git push", and also giving you information about "# commits ahead" etc

git push -u origin --all - push all branches

Use your private email

  • by setting it in config brian@users.noreply.github.com

git config --global user.email "brian@users.noreply.github.com"

  • verify with git config --global user.email

git config --global user.name "Brian"

Note: when did the first commit, after adding --global configs, it asked for GitHub Password. 'git log' showed that using private email

Add an 'upstream' remote to Sync local with Original

git remote add upstream https:github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git - after a fork and clone

git fetch upstream - Fetch the branches and their respective commits from the upstream repository

git checkout master - Check out your fork's local master branch

git merge upstream/master - Merge the changes from 'upstream/master' into your local 'master' branch (preserves local changes)

git push origin master - Previous step is local only. Now update your (forked) GitHub repo with the sync updates

To avoid non-fast-forward-error (your origin repo is ahead of your local), you do a pull (usually first step)

git pull origin YOUR_BRANCH_NAME_EG_MASTER - equivalent to git fetch origin > git merge origin YOUR_BRANCH_NAME

Create branch, commit, and merge

checkout master branch first, then...

git checkout -b hotfix combines 'git branch hotfix' and 'git checkout hotfix'

Make your changes and commmit

git checkout master

git merge hotfix - You can delet the branch now

Syncing of repos after the merge

git pull - git fetch, git merge

git push

To delete the branch

git branch --list git branch hotfix -d - or -D to force delete if warnings

If you've deleted the branch on the remote after the Pull Request -> Merge, then you can do a prune... but only makes sense if the branch(es) was actually deleted on the remote

git remote prune --dry-run origin then without the dry run part

git branch -d jasmine-testing-framework - delete a branch

Create a pull request after pushing to your forked repo on GitHub

This is done via GitHub online UI - not via the git bash terminal

Squash feature branch commit history - then commit to master

Clone (not fork) a repo, add to your GitHub

git clone --depth=1 <url.git> - clones with just 1 commit history

git clone https:ORIGINAL_REPO

git remote rename origin upstream

git remote add origin http:github.com/YOU/YOUR_REPO

Note, you could alternatively clear their remote with remote set-url

git remote set-url origin https:github.com/YOU/YOUR_REPO - point to origin from theirs to yours

NOTE -u (upstream) sets tracking for the branch - e.g. sync in VSCode and pull/push will refer to origin as opposed to upstream

git push -u origin master - master set up to track remote branch master from origin.

Push all branches with tracking

git push -u origin --all

Git Tagging

git tag --list git tag -a v0.3 -m "Tag Message"

not pushed to remote with regualr push, need to do:

git push origin <tag-name>

git push origin --tags

Rename a branch

git branch -m when branch is checkout branch

git branch -m <oldname> <newname> when on a diff branch

Rename a Branch (local and remote)

// On branch feature/A rename to feature/B
git branch -m feature/B
// Delete old branch on remote
git push origin --delete feature/A
git fetch --prune
git push origin -u feature/B

2019 Updates

Pull without checking out branch

  • git fetch <remote> <sourceBranch>:<destinationBranch> Works with fast-forwards only

git fetch origin master:master

Undo local commit and keep changes (unstaged).

This is what you probably want

git reset HEAD~

Revert last pushed commit

git log --oneline (copy the hash) git revert [hash]

  • VSCode: Unstage All Changes, and push

Undo a messed up merge

If the merge is still in progress

git merge --abort

git reset --hard

Delete local branches (multiple)

  1. List all branches beginning with bugfix/

git for-each-ref --format='%(refname:short)' refs/heads/feature

  1. Delete all those local branches (use -d to do it with warnings, -D to force)

git for-each-ref --format='%(refname:short)' refs/heads/feature | xargs git branch -D

Patches (move features from one branch to another)

  1. Checkout the feature branch with the changes

  2. Choose a branch you want to compare it to e.g. the branch it was created from master

  3. If you are in client folder, create a folder to save the patches to e.g. patches-dir

  4. Run command

git format-patch master -o patches-dir

  1. Checkout your new branch - the one you're applying patches to

  2. Apply the patches as commits

git am patches-dir/*.patch --ignore-space-change --ignore-whitespace

Remove a file from last push

https://stackoverflow.com/questions/18357511/git-remove-committed-file-after-push/18357621

  • check out the previous (unchanged) state of your file; notice the double dash
  • git checkout HEAD^ -- /path/to/file
  • commit it:
  • git commit -am "revert changes on this file, not finished with it yet"
  • push it, no force needed:
  • git push
  • get back to your unfinished work, again do (3 times arrow up):
  • git checkout HEAD^ -- /path/to/file

Revert a range of commits

  • Revert a range of commits inclusive. Creates a new commit (staged changes) with the range reverted
    • git revert --no-commit a85434e^..556e07d

Pro Squash and Rebase

To simplify the rebase process it makes sense to reset all commits into a single one.

Don't use git pull origin master on your feature branch at any stage if using this approach

  • git log -n 3
    • git rev-list master.. --count
  • git reset --soft HEAD~2 - reset last two commits. --soft means they become Staged Changes
  • git commit -m "D-xxxx: Bugfix" - merge all your commits into one commit
  • git push --force to overwrite remote branch

Rebasing

  • git fetch --prune
  • git fetch origin master:master or git checkout master, git pull
  • git rebase master -i - invoke rebase process against master branch. -i means interactive mode to resolve merge conflicts.
  • esc :wq - should be just one pick xxx D-xxx: Bugfix command in the git-rebase-todo

May give message Successfully rebased and updated..., in which case use

  • git push --force

If conflicts, fix these, then git add to Staged Changes - you don't change the commit message

  • git rebase --continue
  • git push --force - push to remote, --force if remote branch already exists
    • git push origin bugfix/DE-xxxxx - if doesn’t already exist
  • git rebase --abort if all is going haywire!

NPM

npm root -g - C:\Users\b.HQ\AppData\Roaming\npm\node_modules

npm init -y - creates the package.json

npm config ls -l - config - full list

npm config list - shorter

npm list -g --depth=0 - List all files

npm list -g --depth=0 --link=true - view all npm link libraries

Updating and installing

npm outdated --global // Outdated packages

Version of Packages

npm show react-native@* version - released versions

npm show react-native versions - pre-release included

View NPM distributed bundles

npm view <package-name> dist.tarball

npm view @angular/cli@8.3.4 dist.tarball

run-p arguments

sudo npm run start:tasks -- -- abc

// passes string 'abc' to the first task
// Can be accessed in Express.js with process.argv[2]
"start:tasks": "run-p \"start:server -- {1}\" start:ui",

nvm

nvm alias default 0.12.7 // use this if nvm use isn't working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment