Skip to content

Instantly share code, notes, and snippets.

@masterkawaster
Last active November 12, 2019 09:08
Show Gist options
  • Save masterkawaster/ac926692fb7cf9c2f759ccfe860fb446 to your computer and use it in GitHub Desktop.
Save masterkawaster/ac926692fb7cf9c2f759ccfe860fb446 to your computer and use it in GitHub Desktop.
Git simple usage
#Powershell module
Install-Module posh-git
#Ubuntu shell, oh my zsh
git config --global core.autocrlf true
git config --global core.filemode false
#Create Branch
git checkout -b [name_of_your_new_branch]
git add [file]
git commit
#get remote branches
git fetch
#Publish branch
git push origin [name_of_your_new_branch]
#Status
git status
#history
git log
git log --oneline
git log --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --date=short
#files from specific commit
git diff-tree --no-commit-id --name-only -r <hash_of_commit>
#find commit on branch:
git branch --contains <commit>
git branch -r --contains <commit>
git branch -a --contains <commit>
#undo modified files
git reset --hard
git clean -f
git checkout -- <file path>
#stash and unstash changes for example before rebase (save in some clipboard)
git stash
git stash pop
git stash apply stash@{0}
#See what's there
git stash show -p
git stash show -p stash@{1}
in powershell: git stash show -p stash@'{'1'}'
#show only file names:
git stash show -p --name-only
#rebase on master
git rebase origin/master --force
git pull --rebase origin <branch_name>
git pull --rebase -s recursive -X theirs origin master
#adding small corrections to last commit
git commit -amend
#error message: The following untracked working tree files would be overwritten by checkout
# -x means ignored files are also removed as well as files unknown to git.
# -d means remove untracked directories in addition to untracked files.
# -f is required to force it to run.
git clean -d -fx ""
#pushed to wrong repository:
git checkout wrong_branch
git revert commitsha1
git revert commitsha2
git checkout right_branch
git cherry-pick commitsha1
git cherry-pick commitsha2
#committed to wrong branch:
git checkout wrong_branch
git reset commitsha3 #commit just before commitsha2
git checkout right_branch
git cherry-pick commitsha1
git cherry-pick commitsha2
#squash local commits
git rebase --interactive HEAD~2
#then change to squash the latest commits
pick <hash> b
squash <hash> c
#to make rebase inetactive and automatically keep newest version:
git rebase -i -s recursive -X theirs HEAD~3
git commit --allow-empty - to allow empty commits.
#make rebase and keep your changes without resolving conflicts:
git rebase -s recursive -X theirs master
#history
d1186d1 first branch
739dfed second master
e65056c first master
#split commit to 2 commits (or more)
git rebase -i <commit_hash>
#select commit to e (edit)
git reset --soft HEAD~
git reset <file> (if need to unstage)
git add or git commit
git rebase --continue when you are finished
#make rebase and take changes from master branch instead (your commit will be omited):
git rebase -s recursive -X ours master
#history:
739dfed second master
e65056c first master
#remove files from commit:
git reset --soft HEAD~1
git reset HEAD path/to/unwanted_file
git commit -c ORIG_HEAD
#revert one file to a commit version
- Assuming the hash of the commit you want is c5f567:
git checkout c5f567 -- file1/to/restore file2/to/restore
#discard latest commit and get changed files from it
git reset --soft HEAD~1
#reset unstaged files
git stash save --keep-index --include-untracked
git stash drop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment