Snippets

Martin MacPherson Git commands

Created by mwmac last modified
# List files in a commit
git diff-tree --no-commit-id --name-only -r commit_sha

#Add all changes in current dir
git add .

#Push to upstream for first time
git push -u origin BRANCHNAME

# Delete remote branch
git push origin :BRANCHNAME

#Pull someone else's branch
git checkout -t origin/BRANCHNAME

#Revert changes to working copy
git checkout .

#Revert changes to single file
git checkout -- *file*

#Edit last commit message
git commit --amend

# Remove a change from previous commit
git reset HEAD^ /path/to/file -- Reset file to previous version
git commit --amend -a  -- Amend last commit

Note if you have other changes that you don't want to commit then you will have to do
git commit add /path/to/file
git commit --amend

#Checkout specific commit
git reset --hard commit_sha

#Unstages a file
git reset HEAD *file* push

#Remove last commit and blow it away
git reset --hard HEAD~1

#Remove last commit and leave changes
git reset --soft HEAD~1

#If you destroy a commit you can still find it again (for up to 90 days) and then resurrect it
git reflog (to find it among partial commits)
git checkout -b someBranchName shaYouDestroyed

# Create patch and apply it
git diff branch origin/master > patch
git apply patch

#Clean untracked files/dirs
git clean -fd

#Get list of remotes
git remote -v

#Change remote url
git remote set-url origin *url*

git remote prune [-n | --dry-run] <name>
    - Deletes all stale remote-tracking branches under <name>. These stale branches have already been removed from the remote repo referenced by <name>, but are still locally available in "remotes/<name>".
    
#List Tags
git tag -l *optional filter*

# Add lightweight tag
git tag *tag name*

# Push tags
git push --tags

#Fix problem with extra file after pulling
git rm --cached -r .
git reset --hard

git log --before={2015-06-03} --after={2017-07-08} --author="'git config user.name'" --reverse --pretty=format:"%cd %h %s" --date=short

git gc [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
Runs a number of housekeeping tasks within the current repository, such as compressing file revisions (to reduce disk space and increase performance) and removing unreachable objects which may have been created from prior invocations of git add.

Users are encouraged to run this task on a regular basis within each repository to maintain good disk space utilization and good operating performance.

git config branch.master.autosetuprebase always
git config --global branch.autosetuprebase always
git config --global core.editor "'pathToEditor' -w"

#Set proxy
git config --global http.proxy <proxyUrl>
git config --global https.proxy <proxyUrl>
git config --global --unset http.proxy
git config --global --unset https.proxy

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.