Skip to content

Instantly share code, notes, and snippets.

@chris-ramon
Last active May 17, 2024 01:25
Show Gist options
  • Save chris-ramon/edef5add98b67caf0acb to your computer and use it in GitHub Desktop.
Save chris-ramon/edef5add98b67caf0acb to your computer and use it in GitHub Desktop.
git notes
# pull github remote branch
git fetch origin pull/86/head:MASTER
# undo last commit, keep changes
git reset --mixed HEAD~1
# git log by directory
git log --oneline -n 10 -- dir_name/
# ammend git commit message
git rebase -i parent-commit-hash
# replace pick with reword, then update the commit message.
# cherry-pick all commits from branch: branch-name
git cherry-pick ..branch-name
# undo pull, find good commit then reset
git reflog
git reset HEAD@{1} --hard
# revert line by line
git checkout -p -- filename
y - enter for discard the changes
n - enter for keep the changes.
# show file at specific git commit
git show <commit-sha>:file_path
# prune local changes
git fetch -p
# git log for single file
git log --diff-filter=A -- file_name
# skip git hooks
HUSKY_SKIP_HOOKS=1 git ...
# no vefify
git commit -n -m ...
git commit --no-verify ...
# git diff single file
git diff -- file_path
# pull with strategy merge theirs/ours
git pull origin --strategy theirs remote_branch_name
git pull origin --strategy-option theirs remote_branch_name
git pull -s recursive -X theirs
git pull --rebase -s recursive -X ours
# soft git push --force, will only succeed if local branch is up to date
git push origin branch_name --force-with-lease
# git log for single directory
git log --pretty=format:"%an%x09%ad%x09%s" -n 8 -- subdir-name/
# filter by file extension and git add
git ls-files --modified | grep 'package\.json$' | xargs git add
# count modified files
git status | grep "modified:" | wc -l
# restore deleted branches commits files
# find commit sha, create a new brach, cherry-pick commits
gitk --reflog
# apply git diff from clipboard
pbpaste | git apply -
pbpaste | git apply
# merge upstream into current branch, ignoring conflicts and picking upstream changes.
git merge -X theirs upstream
# undo first commit
git update-ref -d HEAD
# orphan issue
docker rmi $(docker images -f dangling=true -q)
# git conflict, accept remote changes
git checkout --ours file.js
git add file.js
# hub, create pull-request against different branch than default
hub pull-request -b [<owner>/]<branch>
# cherry-pick, pick theirs
git cherry-pick 8efb3b335223c6f754cc16596a9f3b26130eb347 --strategy-option theirs
# set remote, from http to git
git remote set-url origin [email protected]:graphql-go/graphql.git
# pull tags
git pull --tags
# checkout to tag
git checkout tags/0.1.1 -b v0.1.1
# remove local tag
git tag -d tag-name
# remove remote tag
git push --delete origin tag-name
# list remote tags
git ls-remote --tags
# example to patch a not yet merge pr from github
(cd node_modules/source-map; curl https://patch-diff.githubusercontent.com/raw/mozilla/source-map/pull/257.patch | patch lib/source-node.js)
# turn off EOL, vim adds a line at the end of the file
:set binary
:set noeol
# ignore white spaces created by editors or IDEs
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
# checkout to remote branch
git fetch origin
git checkout --track origin/remote-branch-name
# remove remote branch
git push origin --delete remote-branch-name
# show x files of diff context
git diff --no-prefix -Ux file.sh
# unlink git submodule
git submodule deinit <asubmodule>
git rm <asubmodule>
git rm --cached <asubmodule>
rm -rf .git/modules/<asubmodule>
# clone submodule
git submodule update --init --recursive
# update git submodules
git submodule foreach git pull origin master
# add git submodule
git submodule add https://github.com/org/repo.git path/to/sub-directory/.
# rm a submodule in case of issue: a-git-directory-is-found-locally-issue
rm -rf .git/modules/path_to_submodule
# search in previos git commit messages
git log --grep=<pattern>
# git log limit to n commits
git log -n 2
# rebase xyz branch from remote eg. upstream
git pull --rebase upstream master
# pull remote branches
git remote add xyz
git fetch xyz
git checkout desired-branch
# git diff for staged changes
git diff --cached
# show stash content
git stash show -p stash@{2}
# show stash content for single file
git diff stash@{1} -- file_path
# squash commits, 1. finds the parent commit maybe using `gitk`
git rebase -i <parent-commit>
# 2. use the edit tool.
# 3. write a new commit msg or preserve all the squashed commit msgs.
# revert git commit
git revert hash
# git revert -m 1 (first sha) -m 2 (second sha)
Eg:
git show sha-to-revert
Merge: sha-a sha-b
git revert sha-to-revert -m 1 # tree goes as it was on sha-a
git revert sha-to-revert -m 2 # tree goes as it was on sha-b
# copy one commit from diff branches
git cherry-pick commit-hash
# current branch detailed log
git log --graph --decorate --oneline
# rename git branch
git branch -m new-branch-name
# rename locally and remote
git push origin origin/dep:refs/heads/no-dependencies :dep
# clone specific release
git clone --branch v0.4.3 [email protected]:graphql/graphql-js.git
# pull remote branch
git remote add coworker git://path/to/coworkers/repo.git
git fetch coworker
git checkout --track -b coworker/master coworker/master
# to update branch just
git pull
# show modified file names only
git show --name-only commitsha
# existing remote branch
gem install git_remote_branch
grb track some_branch
# remove commits from github
# first stash the changes you want to preserve
# use git reflog show to know which head you go
# then git reset HEAD{X} then stash
# then
git push origin master --force
# resolve conflicts
git mergetool -y
# git pull --rebase origin develop
# Normal merge conflict for 'main.go':
# {local}: modified file
# {remote}: modified file
# Hit return to start merge resolution tool (opendiff): local
# LOCAL: Local changes.
# REMOTE: Remote changes.
# list global config
git config --list
# tell git use other editor instead of vi
git config --global core.editor "vim"
# prevent extra commit when git pull
git config --global pull.rebase true
# change from ssh to https
git remote -v
git remote set-url origin https://github.com/abc/efg.git
# move head to specific branch/commit
git checkout -b new_branch 6e559cb
# config name email
# locally
git config user.email '[email protected]'
git config user.name 'ur name'
# global
git config --global user.email '[email protected]'
git config --global user.name 'urname'
# delete remote branch
git push origin --delete remote_branch_123
# push with flags, prevent integration tests running
git push origin some_branch --skip-ci
# list which branchs contain a commit by hash
git branch --contains 74b638d1f1abc9f3303a35fa3f8522f2baa64332
# delete branch
git branch -d branch_name
# create and checkout branch
git checkout -b newbranch
# update git submodule
# first see which submodule you want to update
git submodule
# them go to the repo
cd public/javascripts/shared
# pull to latests commit
git pull origin master
# finally update submodule
git submodule update
# igoring files temp
# http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html
# different github accounts from same machine
http://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config
# adding ssh
ssh-add ~/.ssh/id_rsa_abc
# github authenticated as?
ssh -T [email protected]
# clone and include submodules
git clone --recursive git://github.com/foo/bar.git
# for cloned repos
git submodule update --init --recursive
# git stash show only file names
git stash show --name-only stash@{0}
# removing specific stash
git stash drop stash@{0}
# applying specific stash
git stash apply stash@{2}
# listing stashs
git stash list
# list stash by date
git stash list --date=local
# fatal: remote origin already exists.
git remote rm origin
# merging someone else pull request
git pull https://github.com/chrisirhc/angular-ui-bootstrap.git feature/add-typeahead-controller-pr
# push subdirectory git repo
git subtree push --prefix xims heroku master
# remove file and file history
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch config/secret.js' \
--prune-empty --tag-name-filter cat -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
# ovrride server changes, like when you did a commit then you reset it
# and wanna push without the need to do pull
git push -f
# display committers for specific file
git blame .gitignore
#start untracking, tracked files
git rm -r --cached app/robots.txt
## git ui
gitk
## stash changes, and include untracked files
git stash -u
## recover stashed changes
git stash apply
## if you see: Could not restore untracked files from stash
git stash pop stash@{1}
## undo last commit
# shows the history of the HEAD
git reflog show
# reset the head to specific head version
git reset HEAD@{1}
## clone submodules (green icon github)
git submodule update --init
## go to specific commit, remove changes
git reset --hard sha1_commit
### gitk
# when going back to a commit
mixed: will keep changes, but commits done before will be gone.
## quit rebase
git rebase --abort
## remove specific commits
git rebase -i HEAD~2
## push changes after rebase to remote
git push origin +devel
### unstage files, specially when did not mean to do (git add .)
git reset
### preserve git commits when merging branches
git merge --no-ff
### discard untrucked files and directories
git clean -f -d
### undo last commit and conserve changes **
git reset --soft HEAD^
### checkout changes of staged files **
git checkout -f
### amend last commit message
git commit --amend -m "New commit message"
### ignore file mode
git config core.filemode false
### to find who modified specific file
git log --all file_name.py
### detalle del commit
git show bae0d0e37fc881e50dd0e50b5a1161fa6694699c
### git mergetool wont save files *.orig
git mergetool
git config --global mergetool.keepBackup false
### to reset head, so you can go back to some commit
gitk --all
### then choose the commit, right-click and reset branch to here
### remove all untracked files
git clean -fd
### restore deleted files
git rev-list -n 1 HEAD -- config/deploy.rb
git checkout theHASHcommit^ -- config/deploy.rb
### abort git merge some_other_branch
git merge --abort
### clonning repo from remote
git clone [email protected]:/home/ubuntu/apps/cool-project
## new repo pushing from local repo
## add remote
git remote add origin [email protected]:chris-ramon/karma-basic-examples.git
## push
git push origin master -f
#ignoring files
.DS_Store
.idea/
django_angularjs_demo_cbv.iml
*.pyc
# push existing repo
git remote add origin [email protected]:chris-ramon/wtm-demo.git
# if origin exists
git remote rm origin
git push -u origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment