Skip to content

Instantly share code, notes, and snippets.

@jgeek
Last active April 24, 2016 13:48
Show Gist options
  • Save jgeek/31ed58cf33c7c38863c1 to your computer and use it in GitHub Desktop.
Save jgeek/31ed58cf33c7c38863c1 to your computer and use it in GitHub Desktop.
git commands
$ sudo apt-get install git
--------------
1. /etc/gitconfig file: Contains values for every user on the system and
all their repositories. If you pass the option --system to git config, it
reads and writes from this file specifically.
2. ~/.gitconfig or ~/.config/git/config file: Specific to your user.
You can make Git read and write to this file specifically by passing the --
global option.
3. config file in the Git directory (that is, .git/config) of whatever repos-
itory you’re currently using: Specific to that single repository.
-------------
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
-------------
$ git config --global core.editor emacs or vim
-------------------
$ git config --list
user.name=John Doe
[email protected]
$ git config user.name
John Doe
# store credential
$ git config credential.helper store
-------------
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
ex:
$ git help config
---------
Getting a Git Repository:
a1)
$ git init
$ git add *.c
$ git add LICENSE
$ git commit -m 'initial project version'
a2)
$ git init --bare
....
b)
$ git clone https://github.com/libgit2/libgit2 mylibgit
--------------
$ git status
$ git add README
git add is a multipurpose command – you use it to begin
tracking new files, to stage files, and to do other things like marking merge-
conflicted files as resolved.
---------------
$ git status -s
$ git status --short
-------------
$ git diff
$ git diff --staged (--cached)
$ git difftool
$ git difftool --tool-help
$ git difftool --tool=kompare
#see what is going to push in the next push
$ git diff --stat --cached origin/master
------------
$ git rm filename //remove it from your tracked files (more accurately, remove it from your staging area)
$ git rm --cached filename // you may want to keep the file on your hard drive but not have Git track it anymore.
$ git rm log/\*.log //removes all files that have the .log extension in the log/ directory
$ git rm \*~ // removes all files that end with ~
---------------------
$ git mv file_from file_to //rename
it is equivalent to:
$ mv from_file to_file
$ git rm from_file
$ git add to_file
-----------------
$ git log -p -2 //which shows the difference introduced in each commit.
//You can also use -2, which limits the output to only the
//last two entries
$ git log --stat //see some abbreviated stats for each commit
$ git log --pretty=oneline/short/full/fuller
$ 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 e-mail
%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
common option to 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).
$ git log --since=2.weeks
$ git log --until=yesterday
$ git log --until=wed //until wednesday
$ git log --until=24 //until 24th current month
$ git log --until="2015-6-30"
$ git log --author=mmad //author contains 'mmad'
$ git log --grep=adf //commit message contains 'adf'
$ git log --all-match --author=kar --grep=adf
-S option which takes a string and only shows the commits that introduced a change to the code that added or re-
moved that string. For instance, if you wanted to find the last commit that add-ed or removed a reference to a specific function, you could call:
$ git log -Sfunction_name
$ git log -- filename //list commits for filename file or directory
$ git log --committer mohammad
$ git log --since, --after,--until, --before
------------------
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend //attach forgotten_file to previous commit
------------------
Reverting:
$ git reset HEAD staged_file //unstages the staged_file
$ git checkout -- filename //revert filename to it's last commit state
$ git reset --soft HEAD^^ // revert local repo to it's last sync state with remote repo.
Revert to origin's master branch's version of file:
#Assuming you did not commit the file, or add it to the index, then:
$ git checkout filename
#Assuming you added it to the index, but did not commit it, then:
$ git reset HEAD filename
$ git checkout filename
# Assuming you did commit it, then:
$ git checkout origin/master filename
# Assuming you want to blow away all commits from your branch (VERY DESTRUCTIVE):
$ git reset --hard origin/master
-------------------
$ git remote
$ git remote -v //shows you the URLs that Git has stored for the shortname to be used when
//reading and writing to that remote
$ git remote add [remote_short_name] [remote_url]
$ git remote add pb https://github.com/paulboone/ticgit
$ git fetch pb //to get data from your remote projects
$ git push [remote-name] [branch-name]
$ $ git push origin master //works only if you cloned from a server to which you have
//write access and if nobody has pushed in the meantime.
Inspecting a Remote:
$ git remote show origin
Removing and Renaming Remotes:
$ git remote rename pb paul
$ git remote rm paul
-------------------
Tagging:
$ git tag //show all tags
$ git tag -l 'v1.8.5*' // If you’re only interested in looking at the 1.8.5 series
$ git tag -a v1.4 -m 'my version 1.4' //creates annotated tag
$ git tag v1.4-lw //create lightweight tag;don’t supply the -a, -s, or -m option.
$ git tag -a v1.2 9fceb02(part of past commit checksum) //tag a past commit which forgotten to be tagged
$ git push origin v1.4 //share a tag
$ git push origin --tags //share all tags
$ git checkout -b [branchname] [tagname]: //Checking out Tags
$ git checkout -b version2 v2.0.0
--------------
$ git config --global alias.st status //define st alias for status
$ git config --global alias.unstage 'reset HEAD --' :
This makes the following two commands equivalent:
$ git unstage fileA
$ git reset HEAD fileA
$ git config --global alias.last 'log -1 HEAD'
$ git last // list the last commit
$ git config --global alias.visual '!gitk' //putting ! to run external command
----------------------
Git Branching:
$ git branch testing
$ git log --oneline --decorate // shows you where the branch pointers are pointing.
$ git checkout testing // switch to an existing branch
//This moves HEAD to point to the testing branch.
$ git checkout -b iss53 //create new branch and switch to it at the same time
//it is equals to : $ git branch iss53
// $ git checkout iss53
//merging two branch
$ git checkout master //switch from hotfix branch to master
$ git merge hotfix //merge master branch with hotfix
$ git branch -d hotfix
Deleted branch hotfix (3a0874c).
$ git mergetool
http://www.rosipov.com/blog/use-vimdiff-as-git-mergetool/
$ git config --global merge.tool vimdiff
$ git config --global diff.tool vimdiff
$ git branch
iss53
* master //* indicates the branch that you currently have checked out (i.e., the branch that HEAD points to)
testing
#To see the last commit on each branch
$ git branch -v
#see branches which already merged with current branch
$ git branch --merged
iss53
* master
#To see all the branches that contain work you haven’t yet merged in
$ git branch --no-merged
# if we want to delete branched that contain works that isn’t merged in
#yet, trying to delete it with git branch -d will fail:
error: The branch 'testing' is not fully merged.
If you are sure you want to delete it, run 'git branch -D testing'
#force to delete a branch
$ git branch -D testing
--------------------
Remote Branches:
$ git ls-remote (remote) or git remote show (remote)
#“ORIGIN” IS NOT SPECIAL
#If you run git clone -o booyah instead, then you will have booyah/master as your default remote branch.
$ git clone -o booyah
# This command looks up which server “origin” is (in this case, it’s git.ourcompa-
ny.com), fetches any data from it that you don’t yet have, and updates your lo-
cal database, moving your origin/master pointer to its new, more up-to-date
position.
$ git fetch origin
$ git push origin serverfix(refs/heads/serverfix:refs/heads/serverfix)
# to push your local serverfix branch to the awesomebranch branch on the remote project
$ git push origin serverfix:awesomebranch
#Branch serverfix set up to track remote branch serverfix from origin.
#Switched to a new branch 'serverfix'
$ git checkout -b [branch] [remotename]/[branch]
$ git checkout -b serverfix origin/serverfix
$ git checkout --track origin/serverfix //creates a branch with same name of remote branch
#Branch serverfix set up to track remote branch serverfix from origin.
$ git branch -u origin/serverfix (-u or --set-upstream-to)
UPSTREAM SHORTHAND
@{upstream} or @{u}
$git merge @{u} == git merge origin/master
# to see what tracking branches you have set up
$ git branch -vv
master bed1e6e [origin/master: behind/ahead 1] 13 added // behind 1 means there is 1 commit on server
// which current branch did not get it
// ahead 1 means current branch has 1 commit
// which does not push it to server yet
It’s important to note that these numbers are only since the last time you
fetched from each server. This command does not reach out to the servers, it’s
telling you about what it has cached from these servers locally. If you want to-
tally up to date ahead and behind numbers, you’ll need to fetch from all your
remotes right before running this. You could do that like this:
$ git fetch --all
$ git branch -vv
--------------
$ git pull == $ git fetch; $ git merge;
---------
#Deleting Remote Branches
$ git push origin --delete serverfix
------------
Rebasing:
$ git checkout experimental
$ git rebase master
#rebasing makes for a cleaner history than merge
#Check out the client branch, figure out the patches from
the common ancestor of the client and server branches, and then replay
them onto master.
$ git rebase --onto master server client
#then
$ git checkout master
$ git merge client
rebase the server branch onto the master branch without having
to check it out first by running git rebase [basebranch] [topicbranch]
– which checks out the topic branch (in this case, server) for you and
replays it onto the base branch (master):
$ git rebase master server
#then
$ git checkout master
$ git merge server
#deleting local branch
$ git branch -d client
-------------
7.Git tools:
$ git show 1c002dd4b536e7479f(SHA-1)
#the output will use shorter values but keep them unique
$ git log --abbrev-commit --pretty=oneline
# show the last commit object on a branch
$ git show topic_branch
# show last commit sha-1 on a branch
$ git rev-parse topic1
ca82a6dff817ec66f44342007202690a93763949
# a log of where your HEAD and branch references have been for the last few months.
#It’s important to note that the reflog information is strictly local – it’s a log of
#what you’ve done in your repository.
$ git reflog
734713b HEAD@{0}: commit: fixed refs handling, added gc auto, updated
d921970 HEAD@{1}: merge phedders/rdocs: Merge made by recursive.
# to see the fifth prior value of the HEAD of your repository
$ git show HEAD@{5}
$ git show HEAD@{2.months.ago}
# to see where your master branch was yesterday
$ git show master@{yesterday}
#To see reflog information formatted like the git log output
$ git log -g master
Ancestry References:
$ git log --pretty=format:'%h %s' --graph
#Then, you can see the previous commit by specifying HEAD^, which means
#“the parent of HEAD”:
$ git show HEAD^
$ git show d921970^
# second parent of commit 'd921970'.
# This syntax is only useful for merge commits, which have more than one parent.
# The first parent is the branch you were on when you merged, and the second is
# the commit on the branch that you merged in
$ git show d921970^2
# ~ means first parent of
$ git show HEAD~ == git show HEAD^
# first parent of first parent(grand parent)
$ git show HEAD~2 == git show HEAD^^
#second parent of the previous references
$ git show HEAD~3^2
Commit Ranges:
#all commits reachable by experiment that aren’t reachable by master.
$ git log master..experimentsudo apt-get install openssh-server
# to see what you’re about to push to a remote:
$ git log origin/master..HEAD
# too many open files error
$ sudo vim /etc/security/limits.conf :
username - nofile 64000
------ --------------
install ssh:
sudo apt-get install openssh-clients
sudo apt-get install openssh-server
-------------
Squash my last X commits together using Git:
You can do this fairly easily without git rebase or git merge --squash. In this example, we'll squash the last 3 commits.
If you want to write the new commit message from scratch, this suffices:
git reset --soft HEAD~3 &&
git commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment