Git - sgml/signature GitHub Wiki

Cheatsheet

Events

Security

Terminology

Local Repository Guide

To see all branches, use fetch with the all and verbose options:

git fetch --all --verbose

To see all branches by name, use branch with the -r option, pipe to grep, and use sed to strip the word origin:

git branch -r | grep 'foo' | sed 's/origin\///'

Cloning and Remote Upstreaming

To clone a specific branch, use `--single-branch --branch <branchname> <remoterepo>:

git clone --single-branch --branch foo [email protected]:foo-bar/baz.git

To sync a new repo, make a new folder and go to it:

<pre> mkdir foobarbaz cd foobarbaz Create the directories, initialize the local git repo, and copy the code locally, create a branch:

mkdir framework
cd framework
git init
git clone ssh:foobarbaz.git
git remote remove origin
git remote add origin [email protected]:myname/foobar.git
git branch --set-upstream-to=origin/master master
git push --set-upstream origin master

To view remotes:

git remote -v show

To create a new branch, use git checkout -b:

git checkout -b myBranch

To link a new branch to a remote repo, use git branch --set-upstream-to:

git branch --set-upstream-to=origin/project/sprint-25-3-8to3-21 feature/INVPROD-2419

To list all remote branches, use git branch -r:

git branch -r
git branch -r | grep localhost

To switch branches, use git checkout with no options:

git checkout foo

To reset existing commits and create a new branch, stash your local env, create a branch from it, push the changes, switch to the current top-level branch, then pull in the latest changes:

git push
git stash
git stash branch local-changes
git add *
git push
git checkout master
git pull
git checkout -b newbranch
git checkout -p origin/local-changes

Checkin

To check in, use the following process:

Add new files:

git add foo

Stash changed files:

git stash

Stash specific files:

git stash push [--] [<pathspec>...]

Pull new changes from the repo:

git pull

Show stashed file list:

git stash show

List stashed changes:

git stash list

Merge stashed changes:

git stash apply

Commit pushed changes:

git commit -m "foo bar baz"

Amend an unacceptable commit message:

git commit --amend

Undo an amended or other staged commit:

git reset 'HEAD@{1}' --soft

Revert the last pushed commit

git revert HEAD

Push new changes to the repo:

git push

Diff current remote to an older branch:

git diff FETCH_HEAD...feature/MAS-2782 --diff-filter=A

Diff last two revisions:

git diff 7194f404d5dccbb177bf4ea5aefa4d60081def31..452db42589004b3b2838933d6fcb6e046f980f28

One file copy from one commit to the local branch:

git show [commit hash] -- /foo/bar/baz.js > /foo/bar/baz.js

Remove a file

git rm -f ngtemplates.js

Reset the local branch HEAD to the remote branch HEAD

git reset
git checkout -b feature/INVPROD-4781
git branch --set-upstream-to=origin/feature/INVPROD-4781 feature/INVPROD-4781
git reset --hard FETCH_HEAD

Cherry pick

  1. Checkout master

  2. Create a branch from it called misc/foo

  3. Cherry pick from develop For example:

    git push # sync the branch you're working on to its remote
    git pull # pull in the merged changes
    git fetch
    git checkout master # change branches to the one you want to modify
    git cherry-pick 533c2093200 #merge; Run git status, resolve conflicts, Run git add, then commit and push

If you pull all cherry pick files this way, you can create a yaml file and parse them using tac, awk, and xargs:

tac commits.yaml | awk '// {print $2}' | xargs git cherry-pick

Blame

  1. Open a file

  2. Find the line

  3. Run git blame on it. For example:

    git blame -L 374,376 schema.py

Search by Path

Find all Vue.js files in the pages path with the word 'organization':

repo:example.org/web.foo organization language:Vue path:pages

Search by Date and Author

git log --since 2017-08-06 --remotes='*' --author=Foo* | grep commit

Search and format non-merge commits

git log --pretty=format:"%h%x09%an%x09%ad%x09%s" --no-merges --since 2024-03-01 --author Foo | awk '!/Merge/'

Interactive Patch of Selected Files or Commits

git pull origin develop
git checkout -b myDevIntgrMerge #new branch
git checkout -p origin/foo -- ./test #pick branch and subdirectories

Manual Conflict Resolution

If you are several weeks or months behind on a branch, use the following process: * For each conflict in each file, copy the HEAD changes and paste them to the end of the file * For each conflict at the end of the file, compare the fragments to the source file in the branch * Piece together the conflict hunks at the bottom of the file, then resolve the conflicts one by one * Run a linter to make sure there are no syntax errors * Merge the newly resolved files

Auto Conflict Resolution

When you get into a failed merge state, the index/stage splits into three. The first index is the abyss. The second index is your version of things. The third is the remote version of things. From here, you can either git checkout file --ours or git checkout file --theirs. This will accept either your file or their file into the stage, overriding the merge conflict in that file. git commit and that pesky merge conflict is dead.

git checkout origin/dev-intgr --ours -- foo.bar
git checkout origin/feature/baseline --ours -- ApplicationMockData.js

Auto reset and merge to rollback to an unmerged commit

git pull origin baseline
git reset HEAD 12345
git checkout -b rebaseline
git merge -s ours origin/feature/baseline

Rollback a repo to a merged commit

git checkout master
git checkout -b rollback
git checkout -p rev_to_rollback_to

Rollback a file to a merged commit

git checkout master
git checkout -b rollback
git checkout -p rev_to_rollback_to ./index.html

Select the files and changes you want during each prompt

Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]?

Using the following key:

y - apply this hunk to index and worktree n - do not apply this hunk to index and worktree q - quit; do not apply this hunk or any of the remaining ones a - apply this hunk and all later hunks in the file d - do not apply this hunk or any of the later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help

Then push to the remote using:

git push --set-upstream origin myDevIntgrMerge #push merge

Merge to stable from unstable without accepting any commits

git merge origin/feature/INVPROD-2898-release -s ours

Merge to unstable from stable without auto resolving conflicts

git merge origin/integration -s recursive -X ours

Test for Bitbucket Conflicts Locally

You need to update your local master branch. So do the following steps :

git checkout master
git pull origin master

Resolve the conflicts here, then run:

git add *
git stash
git checkout << your branch >>

To pull in your local changes, run git stash apply

git merge master

Resolve the conflicts again

git add *
git commit
git push

Ours Merge Strategies

For starters, git merge -s ours xyz is not the same as git merge -X ours xyz. The first uses merge strategy called “ours”, and the second uses default ‘recursive’ merge strategy, but with “ours” option. Creating two entities named “ours” that do similar, but subtly different things is the epitome of bad interface design.

The “-s” variant creates a commit that merges current branch and “xyz”, but ignores all changes from xyz, so the resulting content would be identical to the current branch. This is useful for getting rid of unwanted branches when force-pushes and deletes are disabled. The “-X” variant only ignores changes from xyz that cause a conflict. Changes from xyz that do not cause a conflict will be included in the end result.

Merging Two Repositories

Revert Local Git Changes

To fix an invalid commit message(no iTrack, no whitespace, etc), do the following:

git commit --amend

To clean mistaken deletes or checkins, do the following:

git reset --hard HEAD

Then clean the untracked files and directories with:

git clean -df

Search the commit history of a branch

git reflog master@{one.week.ago}

Git easy-to-parse status

git status --porcelain=2

Rename a Branch

  1. Rename your local branch

    git branch -m new-name

Remove a Branch

  1. git branch -d example

  2. Delete the old-name remote branch and push the new-name local branch.

    git push origin :old-name new-name
  3. Reset the upstream branch for the new-name local branch.

    git push origin -u new-name

Store Password

Create a netrc file:

Linux: vi ~/.netrc Windows: New-Item -Name _netrc -ItemType File -Path $env:userprofile Set the permissions for your eyes only:

chmod 0600 ~/.netrc Then add the bitbucket URL and your username and password:

machine bitbucket.etrade.com login myusername password mypassword

Patch Mode

Naming Conventions

  • bugfix/: bug fixes

  • hotfix/: hot fixes for production

  • release/: code for new feature releases

  • chore/: documentation, copy, and image

  • experiment/: demos

  • wip/: drafts

  • improvement/: refactoring

History

Migration

Architecture

Large Files

Search Engine

URL Paths

References

Config

Worst Parts

SSH

Git Workflows (GitFlow, GithubFlow, GitlabFlow)

Github API

⚠️ **GitHub.com Fallback** ⚠️