Created
May 13, 2019 10:49
-
-
Save ipedrazas/8823b5d116cfcca6b36c8b8b7b5eb7ca to your computer and use it in GitHub Desktop.
git commands
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
# Initialise a git project | |
git init . | |
# Initialise a git repo so that we can pull/push from/to it | |
git init --base master.git | |
# Run a Git Daemon | |
# CentOS | |
git daemon --verbose --base-path=/path/to/workspace/ --base-path-relaxed --reuseaddr --informative-errors --export-all | |
# Cygwin | |
git daemon --verbose --base-path=C:/path/to/workspace/ --base-path-relaxed --reuseaddr --informative-errors --export-all | |
# Then we can clone our local repo (the default port is 9418) | |
git clone git://localhost:9418/master.git | |
# Getting the status of the current repo | |
git status | |
# If we want a short status | |
git status -s | |
# Or, if we only want a specific directory | |
git status src/ | |
# Get the commit difference between two branches | |
# What's in HEAD that isn't in origin/master? | |
git cherry -v origin/master HEAD | |
# To remove a file from git | |
git rm /path/to/file.txt | |
# To remove a file from git but keep it in the working tree | |
git rm --cached ~/path/to/file.txt | |
# To move/rename a file in git | |
git mv /path/to/old_file.txt /path/to/new_file.txt | |
# Get detailed information on the branches | |
git branch -vv | |
# Reset branch to previous commit | |
git reset --hard HEAD~1 | |
# If we want to un-stage a file | |
git reset /path/to/file.txt | |
# Change default git push behaviour | |
git config --global push.default current | |
# List all global configuration settings | |
git config --list | |
# Set the default editor to vim | |
git config --global core.editor "vim" | |
Or on Windows | |
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -nosession" | |
# Commit changes without launching an editor | |
git commit -m "This is our commit message" | |
# Show the diffs in the editor when we run 'git commit' | |
git commit -v | |
# We can amend the previous commit. | |
# If we want to add a file to the previous commit, run git add first | |
git commit --amend | |
# Diff-ing between 2 tags with information and colour | |
git diff --stat --color <tag> <tag> | |
git diff --stat --color Release_4.1.1.9 Release_4.1.1.10 | |
# List the commits in current branch that aren't in another branch | |
git rev-list <my_branch>..<other_branch> | |
# The above command can also take format options | |
git rev-list <my_branch>..<other_branch> --format="%B" | |
# Create a new branch | |
git checkout -b <new_branch_name> | |
# Rename local git branch | |
git branch -m <new_branch_name> | |
# Push local branch to remote branch with a different name | |
git push origin <local_branch_name>:<remote_branch_name> | |
# Push local branch to remote repository | |
git push origin <local_branch_name> | |
# Show differences in files that have been staged | |
git diff --cached | |
# Delete remote branch | |
git push origin --delete <branch_name> | |
OR | |
git push origin :<branch_name> | |
# Delete local branch | |
git branch -D <branch_name> | |
# Rebase onto a branch | |
git rebase <upstream>/<branch_name> | |
# Example | |
git rebase origin/master | |
# Show what changed with each commit | |
git log -p | |
# If we want to limit the number of entries shown to 2 | |
git log -p -2 | |
# Show some statistics about each of the commits | |
git log --stat | |
# If we want a one-liner per commit | |
git log --pretty=oneline | |
# We can also customise the commits messages by specifying a format | |
# "<hash> - <author name>, <author date, relative> : <Subject>" | |
# We can append the --graph option to view the example with a graph | |
git log --pretty=format:"%h - %an, %ar : %s" | |
%H Commit hash | |
%h Abbreviated commit hash | |
%T Tree hash | |
%t Abbreviated tree hash | |
%P Parent hashes | |
%p Abbreviated parent hashes | |
%an Author name | |
%ae Author email | |
%ad Author date (format respects the --date=option) | |
%ar Author date, relative | |
%cn Committer name | |
%ce Committer email | |
%cd Committer date | |
%cr Committer date, relative | |
%s Subject | |
# Other options that can be used with git log | |
-p Show the patch introduced with each commit. | |
--stat Show statistics for files modified in each commit. | |
--shortstat Display only the changed/insertions/deletions line from the --stat command. | |
-name-only Show the list of files modified after the commit information. | |
--name-status Show the list of files affected with added/modified/deleted information as well. | |
--abbrev-commit Show only the first few characters of the SHA-1 checksum instead of all 40. | |
--relative-date Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format. | |
--graph Display an ASCII graph of the branch and merge history beside the log output. | |
--pretty Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format). | |
# We can limit the log output from 2 weeks ago until present | |
git log --since=2.weeks | |
# Or | |
git log --since="2008-01-15" # Format is YYYY-MM-DD | |
# Or | |
git log --since="2 years 1 day 3 minutes ago" | |
# Other options that can be used with git log | |
-(n) Show only the last n commits | |
--since, --after Limit the commits to those made after the specified date | |
--until, --before Limit the commits to those made before the specified date | |
--author Only show commits in which the author entry matches the specified string. | |
--committer Only show commits in which the committer entry matches the specified string. | |
--grep Only show commits with a commit message containing the string | |
-S Only show commits adding or removing code matching the string | |
# Show the commits from one user | |
git log --author="<author_name>" | |
# cherry-pick commits | |
git cherry-pick <commit hash> | |
# Or if we want to edit the commit message before committing the cherry-pick | |
git cherry-pick --edit <commit_message> | |
# Stash staged changes | |
git add . # This will stage the changes | |
git stash # Then we'll stash them | |
# Or with a custom comment. | |
git stash save "<insert_comment>" | |
# Show stash contents | |
git stash show -p stash@{0} | |
# Get the list of current stashes | |
git stash list | |
# Apply most recent stash on current work | |
git stash apply | |
# Or apply a certain stash | |
git stash apply stash@{0} | |
# To drop the first stash on the stack (we can use stash@{0} to specify a stash) | |
git stash drop | |
# Check what branches have been merged into HEAD | |
git branch --merged | |
# Check what branches haven't been merged into HEAD | |
git branch --no-merged | |
# Discard a hunk in a file - this will go through each hunk and ask if you want to discard it | |
git checkout -p <file_name> | |
# If you've removed a file and committed it we can get the file back from the following: | |
git checkout 'master@{7 days ago}' -- path/to/file.txt | |
# Discard changes in a given file | |
git checkout -- path/to/file.txt | |
# Show a list of remotes | |
git remote -v # The -v isn't necessary, but produces a verbose list | |
# Add a remote | |
git remote add <name> <address> | |
git remote add paul git://127.0.0.1/repo-name | |
# Remove a remote | |
git remote rm <remote> | |
git remote rm paul | |
# Fetch all the information from a remote | |
git fetch <remote> | |
git fetch paul | |
# Show information about a remote | |
git remote show <remote> | |
git remote show origin | |
# Set the default pull behaviour to pull --rebase | |
git config --global pull.rebase true | |
# List the available tags | |
git tag | |
# List the available tags with particular string | |
git tag -l Release_* | |
# Create an annotated tag | |
git tag -a <tag name> -m "Some message goes here" | |
# Create a lightweight tag | |
git tag <tag name> | |
# Tag a commit that happened previously | |
git tag -a <tag name> <commit hash> | |
git tag -a <Stable_Release> 9fceb02 | |
# Push a tag to the remote | |
git push origin <tag name> | |
# Push all tags to the remote | |
git push origin --tags | |
# Checkout a Pull Request | |
git fetch origin pull/351/head:pr-123 | |
git checkout pr-123 | |
# Checkout a tag | |
git checkout -b <branch name> <tag name> | |
# Get the common commits of two branches, and merge them onto <branch 1> | |
git rebase --onto <branch 1> <branch 2> <branch 3> | |
# Do an interactive Rebase. This allows us to squash a commit | |
git rebase -i <commit hash> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment