Last active
August 6, 2021 17:00
-
-
Save veysiertekin/9756670 to your computer and use it in GitHub Desktop.
Git usefull approachs to overcome problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git reset --merge | |
# OR | |
git merge --abort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ssh | |
git clone git@YOURSSHHOST:YOURGISTIDHERE.git | |
# Http | |
git clone https://gist.github.com/veysiertekin/<id>.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git add */.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# merge single commit | |
git cherry-pick <commit_hash> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# export commit | |
git format-patch -1 <sha> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git log --graph --decorate | |
git log --graph --decorate --all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list remote branches with last commit numbers | |
git ls-remote | |
git ls-remote <remote-url> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setting: | |
git config --global alias.workprofile '!f(){ git config user.email "test@gmail.com" && git config user.name "test-user"; };f' | |
# using: | |
git workprofile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git revert <commit_hash> -m 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git reset 8a733336ac8d407c52f9bff4bff549addfd8ca03 | |
git reset --soft HEAD@{1} | |
git commit -m "Revert to 8a733336ac8d407c52f9bff4bff549addfd8ca03" | |
git reset --hard | |
git push |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# show brnaches with reflogs | |
git reflog master | |
# undo to commit with reflog reference | |
git reset --hard master@{1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Revert last commit but keep that changes. | |
git reset --soft HEAD~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git checkout <commit_hash> -- <folder_path> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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