Skip to content

Instantly share code, notes, and snippets.

@veysiertekin
Last active August 6, 2021 17:00
Show Gist options
  • Save veysiertekin/9756670 to your computer and use it in GitHub Desktop.
Save veysiertekin/9756670 to your computer and use it in GitHub Desktop.
Git usefull approachs to overcome problems
git reset --merge
# OR
git merge --abort
# make core editor vim
git config --global core.editor /usr/bin/vim
# make core editor sublime-text
git config --global core.editor "subl -n -w"
# Ssh
git clone git@YOURSSHHOST:YOURGISTIDHERE.git
# Http
git clone https://gist.github.com/veysiertekin/<id>.git
# List all branches (locale/remote)
git branch -a
# List only local branches
git branch
# List only remote branches
git branch -r
# delete remote braanch
git push origin --delete <branchName>
# delete local braanch
git branch -D <branchName>
# Create and select branch
git branch test # create
git checkout test # switch to `test`
# or
git checkout -b test # create&switch to `test`
# Create new branch
git push -u origin <feature_branch_name>
git push origin HEAD
# Checkout a remote branch
git fetch
git checkout test
# `git checkout test` will NOT work in modern git if you have multiple remotes. In this case use `git checkout -b test <remote-name>/test`
# merge single commit
git cherry-pick <commit_hash>
# Commit
git commit -m "your message"
# Edit last commit message
git commit --amend -m "Edited commit message"
# add message to a commit
git commit --squash -m <message> <commit_number>
# compare with remote branch
git fetch # update local cache with remote changes
git diff <local branch> <remote>/<remote branch>
# for example:
git diff master origin/master
# Show diff by file names with addtion and deletion details
git diff --stat <destination>
# export diff as a patch
git diff --patch > ../0-changes.patch
# export diff to a patch
git diff 935f7116b5c864c37181e0115714efad09aa32dd HEAD > ../changes.diff
# export commit
git format-patch -1 <sha>
git log --graph --decorate
git log --graph --decorate --all
# list remote branches with last commit numbers
git ls-remote
git ls-remote <remote-url>
# http://stackoverflow.com/a/9683337/1888799
rm -rf .git
git init
git add -A .
git commit -m "init"
git remote add origin <url>
git push -u --force origin master
# Merge unrelated (when creating newly created projects)
git merge master --allow-unrelated-histories
# Merge as a patch (without commit)
git merge <branch> --no-commit --no-ff
# setting:
git config --global alias.workprofile '!f(){ git config user.email "test@gmail.com" && git config user.name "test-user"; };f'
# using:
git workprofile
# interactive rebase!!!
# enable interactive rebase
git config --global rebase.autosquash true
# to do interactive rebase
# 1) make new changes at new branch (also you can use worktree)
git checkout -b <new_changes_branch>
... // do what ever you want and commit:
git commit -a -m <message>
# 2) go back one commit
git reset --hard HEAD~
# 3) and fix branch
git commit -a --fixup :/<branch-to-fix>
# 4) and interactive rebase to upstream
git rebase -i upstream/master
# Start interactive rebase with specific commit
git rebase -i <commit_id>
# Rebase to initial commit
git rebase -i --root
# interactive rebase commands
#
# reword : allow to change message of the commit. Do not forget! After saving this screen, a prompt screen will be open sequentially to change messages.
# fixup : merges this commit into above one. discards the message.
# squash : merges this commit into above one. does not discard the message.
# exec : lets you run shell commands against a commit.
# edit : you'll be given the chance to amend the commit.
#
pick 6772ee2 AVG-23 removes unneccessary files.
fixup 39ff6dc AVG-23 introduces sale service.
reword 9c4d694 AVG-23 adds test scheme.
# Revert all changes and unstaged files in a git repo (works well on windows and unix)
git clean -fdx
git fetch origin
git reset --hard origin/master
git revert <commit_hash> -m 1
git reset 8a733336ac8d407c52f9bff4bff549addfd8ca03
git reset --soft HEAD@{1}
git commit -m "Revert to 8a733336ac8d407c52f9bff4bff549addfd8ca03"
git reset --hard
git push
# save stash
git stash save
# save stash with specific name
git stash save "<stash_name>"
# list stashes
git stash list
# list stashes with local time
git stash list --date=local
# apply stash
git stash apply stash@{n} # n -> 0, 1, ...
# delete top stash
git stash drop
# delete specific stash
git stash drop stash@{n} # n -> 0, 1, ...
# export as patch file
git stash show stash@{n} -p > ~/Desktop/n.patch.diff
# apply patch file
git apply ~/Desktop/n.patch.diff
# add subtree to project
git subtree add --prefix <target-folder> <remote-url> <branch name or tag or version number> --squash
# example:
git subtree add --prefix <target-folder> <remote-url> master --squash
git subtree add --prefix <target-folder> <remote-url> v1.1 --squash
# update subtree
git subtree pull --prefix <target-folder> <remote-url> <branch name or tag or version number> --squash
git subtree pull --prefix <target-folder> <remote-url> master --squash
git subtree pull --prefix <target-folder> <remote-url> v1.5 --squash
# List of tags
git tag
# Creating tags
# -a -> (short of annotate)
# -m -> Message
git tag -a v0.0.1 -m "i have a new tag named `v0.0.1`"
# Pushing tags to master
git push --tags
# show brnaches with reflogs
git reflog master
# undo to commit with reflog reference
git reset --hard master@{1}
# list worktree
git worktree list
# create new worktree with new branch
git worktree add -b <new-branch-name> <folder-name-to-branch>
# create new worktree with already created branch
git worktree add <folder-name-to-branch> <branch-name>
# remove worktree branch
rm -rf <folder-name-to-branch>
git worktree prune
git branch -D <branch-name>
# Revert last commit but keep that changes.
git reset --soft HEAD~
git checkout <commit_hash> -- <folder_path>
# run git commands without actually being in directory
git -C <git_repo_directory> <commands>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment