Skip to content

Commit

Permalink
Merge pull request cheat#48 from Jonny-exe/master
Browse files Browse the repository at this point in the history
git: added two more options
  • Loading branch information
chrisallenlane authored Nov 10, 2020
2 parents 20d55f2 + 0fe9371 commit b1b2aaa
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions git
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,53 @@ git add --all
# from the stash anytime
git stash

# To stash changes with a message
# To stash changes with a message:
git stash push -m <message>

# To list all the stashed changes
# To list all the stashed changes:
git stash list

# To apply the most recent change and remove the stash from the stash list
# To apply the most recent change and remove the stash from the stash list:
git stash pop

# To apply any stash from the list of stashes. This does not remove the stash
# from the stash list
git stash apply stash@{6}

# To commit staged changes
# To commit staged changes:
git commit -m <message>

# To edit previous commit message
# To edit previous commit message:
git commit --amend

# Git commit in the past
git commit --date="`date --date='2 day ago'`"
git commit --date="Jun 13 18:30:25 IST 2015"
# more recent versions of Git also support --date="2 days ago" directly

# To change the date of an existing commit
# To change the date of an existing commit:
git filter-branch --env-filter \
'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
then
export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
fi'

# To removed staged and working directory changes
# To remove staged and working directory changes:
git reset --hard

# To go 2 commits back
# To go 2 commits back:
git reset --hard HEAD~2

# To revert first/initial commit on a branch
# To revert first/initial commit on a branch:
# Running git reset --hard HEAD~1 will give error:
# fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
git update-ref -d HEAD

# To remove untracked files
# To remove untracked files:
git clean -f -d

# To remove untracked and ignored files
# To remove untracked and ignored files:
git clean -f -d -x

# To push to the tracked master branch:
Expand All @@ -74,13 +74,19 @@ git push origin master
# To push to a specified repository:
git push git@github.com:<username>/<repo>.git

# To delete the branch "branch_name"
# To push a tag to remote:
git push origin <tagname>

# To force a push:
git push -f

# To delete the branch <branch>:
git branch -D <branch>

# To make an exisiting branch track a remote branch
# To make an exisiting branch track a remote branch:
git branch -u upstream/foo

# To see who commited which line in a file
# To see who commited which line in a file:
git blame <file>

# To sync a fork with the master repo:
Expand All @@ -97,63 +103,63 @@ git diff branch_1 branch_2 # Check difference
git log # Show all the commits
git status # Show the changes from last commit

# Commit history of a set of files
# To view the commit history of a set of files:
git log --pretty=email --patch-with-stat --reverse --full-index -- Admin\*.py > Sripts.patch

# Import commits from another repo
# To import commits from another repo:
git --git-dir=../some_other_repo/.git format-patch -k -1 --stdout <commit SHA> | git am -3 -k

# View commits that will be pushed
# To view commits that will be pushed:
git log @{u}..

# View changes that are new on a feature branch
# To view changes that are new on a feature branch:
git log -p feature --not master
git diff master...feature

# Interactive rebase for the last 7 commits
# To perform an interactive rebase for the prior 7 commits:
git rebase -i @~7

# Diff files WITHOUT considering them a part of git
# To diff files WITHOUT considering them a part of git:
# This can be used to diff files that are not in a git repo!
git diff --no-index path/to/file/A path/to/file/B

# To pull changes while overwriting any local commits
# To pull changes while overwriting any local commits:
git fetch --all
git reset --hard origin/master

# Update all your submodules
# To update all submodules:
git submodule update --init --recursive

# Perform a shallow clone to only get latest commits
# To perform a shallow clone to only get latest commits:
# (helps save data when cloning large repos)
git clone --depth 1 <remote-url>

# To unshallow a clone
# To unshallow a clone:
git pull --unshallow

# Create a bare branch (one that has no commits on it)
# To create a bare branch (one that has no commits on it):
git checkout --orphan branch_name

# Checkout a new branch from a different starting point
# To checkout a new branch from a different starting point:
git checkout -b master upstream/master

# Remove all stale branches (ones that have been deleted on remote)
# To remove all stale branches (ones that have been deleted on remote):
# So if you have a lot of useless branches, delete them on Github and then run this
git remote prune origin

# The following can be used to prune all remotes at once
# To prune all remotes at once:
git remote prune $(git remote | tr '\n' ' ')

# Revisions can also be identified with :/text
# So, this will show the first commit that has "cool" in their message body
git show :/cool

# Undo parts of last commit in a specific file
# To undo parts of last commit in a specific file
git checkout -p HEAD^ -- /path/to/file

# Revert a commit and keep the history of the reverted change as a separate revert commit
# To revert a commit and keep the history of the reverted change as a separate revert commit:
git revert <commit SHA>

# Pich a commit from a branch to current branch. This is different than merge as
# this just applies a single commit from a branch to current branch
# To pick a commit from a branch to current branch. This is different than merge as
# this just applies a single commit from a branch to current branch:
git cherry-pick <commit SHA1>

0 comments on commit b1b2aaa

Please sign in to comment.