Skip to content
bootstraponline edited this page Sep 28, 2018 · 50 revisions

Troubleshoot Git LFS

  • o ~/.gitconfig look under [lfs]
  • Review output of git lfs env

Count lines of code

  • git ls-files | grep -i "test_runner/*" | xargs wc -l

Count commits ahead of master

Use visual studio code as default editor

  • git config --global core.editor "code --wait"

Disable paging on git branch

  • git config --global pager.branch cat

LFS uninstall

Uninstall LFS on select CI jobs to reduce the bandwidth overhead associated with a monorepo workflow. Every git clone doesn't need all LFS files.

   - script:
        inputs:
        - content: |
            #!/usr/bin/env bash
            set -e

            echo "" > `which git-lfs`
            echo "Git LFS disabled"
        title: Disable Git LFS

LFS remove individual files

Removing files is a pain. It's better to have a white list of only the important large files, instead of globbing entire directories.

$ git lfs uninstall
$ code ~/.gitattributes and remove tracking rules
$ git lfs ls-files
98420079ff * path/to/some.jar

$ touch path/to/some.jar
$ git commit -am "Removed files from git LFS tracking"
$ git lfs install

Remove sensitive data

No context diff

No context with leading +/- removed.

git diff -U0 --color-words --no-color > diff.txt

Delete all branches except master

git checkout master

git --no-pager branch | grep -v \* | xargs git branch -D

Auto resolve rebase

-X can be theirs or ours

git rebase -s recursive -X ours origin/master

Rewrite origin

# ~/.gitconfig
[alias]
ro =  !/bin/bash -c ~/ro.sh
#!/usr/bin/env ruby

# ~/ro.sh

repo_name = %x[git remote get-url origin].match(/\/[^\/]+.git/).to_s
`git remote remove origin`
`git remote add origin git@github.com:bootstraponline#{repo_name}`

puts `git remote -v`

--

Track repo's master branch as a submodule.

git submodule add -b master git@github.com:USER/REPO.git
git submodule update --init --recursive --remote

--

Auto fixup (add to ~/.gitconfig). git f will automatically commit any changes and squash them into the previous commit retaining the commit message of the previous commit.

[alias]
    f = "!sh -c '(git add . ; git commit --amend -C HEAD)' -"

--

Clean merging (from http://stackoverflow.com/a/2763118)

git checkout better_branch
git merge --strategy=ours --no-commit master
git commit          # add information to the template merge message
git checkout master
git merge better_branch             # fast-forward master up to the merge

--

Update all submodules according to remote tracking branch.

git submodule update --remote

--

Change / rename file name only.

git mv -f README.md readme.md

--

Auto squash.

git reset --soft head~2; git commit -am "auto squashed"

--

Remove commit id from history without interaction.

git rebase --onto commit_sha1^ commit_sha1

--

Replace 'pick' with 'f' in vim

:%s/pick /f /g

--

Unstage and delete files.

git revert /path/to/file*
git checkout -- .

--

Ignore changes in a submodule. Add ignore = all

# open .gitmodules
[submodule "my_module"]
  path = my_module
  url = git@github.com:example/my_module.git
  ignore = all

--

Checkout individual file one commit behind.

git checkout HEAD^1 android/adb.js

Two commits behind.

git checkout HEAD~2 android/adb.js

--

Rebase upstream master onto local master.

git pull --rebase up master

--

Update submodules.

git submodule foreach git pull origin master

--

Reset author to yourself.

git commit --amend --reset-author

Reset author to someone else.

git commit --amend --author "username <example@example.com>"

--

Rename tag v0.0.9 to v0.0.09.

git tag v0.0.09 v0.0.9
git tag -d v0.0.9
git push origin v0.0.09
git push origin :v0.0.9

--

Fix deleted file not staged.

git add -u

--

Reset local branch with remote.

git remote add b git@github.com:bootstraponline/appium.git
git fetch b
git reset --hard b/reconnect

--

$ git --version
git version 1.8.1.4

--

Disable blinking cursor in MacVIM.

:set gcr=a:blinkon0

--

Push local master to remote clean branch.

git push origin master:clean

--

Rename patch-1 branch for pull request and delete the old branch.

git fetch origin patch-1
git checkout patch-1
git branch -m patch-1 reset_android
git push origin reset_android
git push origin :patch-1

--

Delete a bunch of remote branches.

$ git branch --remote

b = %(origin/branch1
origin/branch2
)
b.each_line { |l| puts 'git push origin :' + l.strip.chomp + ";\\ \n" }

--

Update date from StackOverflow

GIT_COMMITTER_DATE="`date`" git commit --amend --date "`date`"

--

Push current branch by default (avoids need for git push -u origin branch_name).

git config --global push.default current


git branch --set-upstream master origin/master


Text width in vim (wraps on space) ~/.vimrc

:set tw=80
:set formatoptions=tcq

Stop ruby.

killall -v ruby -s SIGKILL


Cherry pick one commit from a pull request.

git remote add pr git://github.com/npisenti/gollum.git
git fetch pr
git cherry-pick ba8dd5e4bca08efa7b73987a266f74a12ca3fe5c
# we could also checkout pr
git checkout pr/master

git < 1.7.9 Git pull and auto rebase

git config --global branch.autosetuprebase always

git >= 1.7.9 pull.rebase

git config --global pull.rebase true


Lightweight tagging

List all tags, create a lightweight tag, show a tag, then push to origin.

git tag
git tag v1.0
git show v1.0
git push origin v1.0

Fix confused git pull.

git config branch.master.remote origin; \
git config branch.master.merge refs/heads/master;

Gedit + Ubuntu + Git

git config --global core.editor "gedit -w -s"


Redo a commit before it has been pushed.

git reset --soft HEAD^


Revert a failed merge.

git revert -m 1 20dd0816a6


Stash current changes, delete last 3 commits, and then commit current changes.

git stash
git reset --hard HEAD~3
git stash pop

Add only empty files to a commit:

git ls-files --deleted | xargs git rm


Create empty branch java:

git symbolic-ref HEAD refs/heads/java


Squash commits in a pull request.

# Squash the 3 most recent commits.
# Change 'pick, pick, pick' to 'pick, s, s'
# or use f to discard the commit message
# Ctrl + \ in nano will open search and replace (pick -> s)
git rebase -i HEAD~3
git push --force

http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html


Save current changes, pull from upstream, restore changes, and clear the stash.

git stash save
git pull
git stash pop
git stash clear

How to revert a failing pull request:

git revert --no-edit -m 1 HEAD


Updating submodules.

git submodule update --init


List unique authors.

git log --format='%aN <%aE>' | sort -u


Amending.

git commit --amend -m "New msg."

Override branches for clean history in pull requests. Example overrides try1 with try2. Push using git push -f origin try1.

# git branch => on try2
git branch -f try1

Rebase to update an upstream branch on a local fork.

git remote add upstream git://github.com/github/gollum.git
git fetch upstream
git rebase upstream/master

Commands and descriptions based on Quicksilver's workflow.

  1. Fork https://github.com/github/gollum by clicking the Fork button.

  2. Clone your fork.
    git clone git@github.com:bootstraponline/gollum.git
    cd gollum

  3. Add upstream.
    git remote add upstream git@github.com:github/gollum.git

  4. Rebase master branch to upstream as upstream changes.
    git pull --rebase upstream master

  5. Create new branches from master for each work item.
    git checkout -b workBranch master

  6. Commit work to the branch.
    git add .
    git commit -am "Add feature."

  7. Rebase branches to master.
    git checkout workBranch
    git rebase master

  8. Push branch to github.
    git push origin workBranch
    Or git push if the branch is already checked out. Note that git push will push all branches, including master.

  9. Submit pull requests from branches and delete when no longer needed.
    Note that GitHub will not allow deleting master branch if it's set as default.
    git push origin :workBranch
    git branch -D workBranch

Useful commands:
git reset --hard
git push --force
git branch

From git's documentation:

In other words, "git push --force" is a method reserved for a case where you do mean to lose history.

Create a new branch based on upstream.

git remote add upstream https://github.com/github/gollum.git
git fetch upstream
git checkout -b newBranch upstream/master

Force clean:

git clean -dfx

Force clean dry run:

git clean -dfxn

Alt log view

git config --global color.ui "auto"

git config --global alias.lg "log --graph --decorate --oneline"

Command from sant0sk1

git lg

Set name and email.

git config --global user.email "spam@email"
git config --global user.name "my name"

--

Use Sublime Text as the default git editor.

  • git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -w"

--

global ignore

Add to ~/.gitconfig

[core]
  excludesfile = ~/.gitignore

--

Delete all remote branches in one command.

# ruby
branches = `git ls-remote --heads`.split("\n").map { |b| b.split('/').last.strip}.join(' ')
cmd = "git push origin --delete #{branches}"
puts cmd
`#{cmd}`

Clone this wiki locally