Skip to content

Instantly share code, notes, and snippets.

@starikovs
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starikovs/9390657 to your computer and use it in GitHub Desktop.
Save starikovs/9390657 to your computer and use it in GitHub Desktop.
--- install git ---
git ---version (in Debian git is installed)
dpkg -s git | grep Status
dpkg -s git
apt-cache pkgnames git
apt-get install git
--- bare repo on server ---
(bare repositories shouldn’t have a working directory)
(at server)
ssh user@host
git init --bare /path/to/repo.git (.git extension to indicate it's a bare repo)
(at client)
git clone ssh://user@host/path/to/repo.git
(git automatically adds a shortcut called "origin" that points back to the “parent” repository,
under the assumption that you'll want to interact with it further on down the road)
origin - is the remote connection to the central repository created after "clone"
--- new repo ---
git init
--- status, what changed ---
git status
git diff (changes befor add)
git diff --staged (changes between last commit and add)
git diff 2f34 2730 (diff between commits)
git diff --color (show colored diff)
git cherry -v origin/somebranch (show unpushed commits)
git cherry -v somebranch
--- ignore ---
.gitignore
.git/info/exclude
--- new github repo --
touch README.md
git init
git add README.md
git commit -m "first commit"
git commit -v (open editor and show diff before commit)
git commit -a -m "comment"
# creates a remote named "origin" pointing at your GitHub repository
git remote add origin https://github.com/starikovs/conf.git
# sends your commits in the "master" branch to GitHub
git push -u origin master (push and set upstream, he same way as "git branch --set-upstream" does)
--- add ---
http://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add
git add -A (stages All)
git add . (stages new and modified, without deleted)
git add -u (stages modified and deleted, without new)
--- remove files ---
rm file
git rm file
OR
git rm file (without rm)
git rm -f file (if file has been indexed)
git rm \*~
git rm -r dir/ (remove recursively)
git rm -rf dir/ (remove recusively even if there're local modifications)
--- move (rename) file ---
git mv file_from file_to
(the same as above)
mv README.txt README
git rm README.txt
git add README
(git detects moving automatically)
--- push existing repo ---
git remote add origin https://github.com/starikovs/conf.git
git push -u origin master
--- to unstage, before commit recently added files ---
git reset HEAD README.md (when it's commited already)
git rm --cached README.md (remove file from git but saves on disk)
--- discard changes in working directory ---
git checkout -- README.md
git checkout -- . (for all unstaged files use)
--- change last commit ---
git commit --amend
git commit --amend -m 'Class that echoes "Hello, World!"' (change commit message)
git commit -m 'initial commit'
git add forgotten_file
git commit --amend
(!!don't amend after push)
(to undo amend:)
git reset --soft HEAD@{1} (reset to previous commit before amend)
git commit -C HEAD@{1} (new commit with the same message)
--- clone github repo ---
git clone https://github.com/starikovs/conf.git
git clone https://github.com/starikovs/conf.git ./
--- history ---
git log
git log -p (diff)
git log -p -2
git log -p -1 --stat (get additional stat - files updated, etc)
git log -3 --pretty=oneline (short, full, fuller)
git log -2 --pretty=format:"%h - %an, %ar : %s" (%an - author name, %ar - relative author date)
git log --pretty=format:"%h %s" --graph (%h - short hash, %s - comment)
git log --since=2.weeks = git log --after=2.weeks
git log --until=2.weeks = git log --before=2.weeks
git log -2 --author="Vacheslav Starikov" (filter by author)
git log -2 --grep="bash" (filter by commit message)
git log -2 --author="Vacheslav Starikov" --grep="bash" (OR)
git log -2 --author="Vacheslav Starikov" --grep="bash" --all-match (AND)
git show 36b314c (--stat - additional stat, -p - diffs)
git log -- filepath
git show commitId (like 36b314c for example) -- filepath (show a diff on a specific file with a specific commit)
git show --pretty="format:" --name-only bd61ad98 (show changed files for commit)
git diff firstCommitId secondCommitId
git diff firstCommitId secondCommitId -- filepath
--- reset --
git reset --hard 82ba34
--- remotes repos ---
git remote (list of short names of remote repos)
origin - default name
git remove -v (show url)
git remote add [shortname] [url] (add remote repo)
git remote add pb git://github.com/paulboone/ticgit.git
git fetch pb
git fetch [remote-name] (get data from remote repo)
git fetch origin (get data but doesn't merge, merge manually)
git fetch -v --progress origin develop:remotes/origin/develop
git pull (get data from remote branch (fetch) and merge automatically (merge))
git push [url] [branch]
git push origin master (origin - remote connection, master - push local master -> origin's master)
git clone - create "origin" and "master" automatically
git remote show origin (more info about origin)
git remote rename pb paul (rename link shortname)
git remote rm paul (remove link)
--- tags ---
v1.0, v2.0 etc.
git tag (show tags)
git tag -l 'v1.4.2.*' (show all 1.4.2.*)
git tag -a v1.4 -m 'my version 1.4' (create tag of "-a" type)
git show v1.4 (show tagged commit and tag info)
git tag v1.4.1 (create lightweight tag, don't pass "-a", "-s" or "-m")
git tag -a v1.2 9fceb02 (tag custom commit, 9fceb02 - begining of hash)
git push v1.4 (push tag, tags aren't pushed with branch push)
git push --tags (push all tags)
git push origin --tags
git tag -a 0.1 -m "Initial public release" master
git push --tags
--- branches ---
good info about branches http://nvie.com/posts/a-successful-git-branching-model/
master - default branch name and default branch
git branch test - new branch but HEAD still points to "master"
HEAD - pointer to current branch
git branch
git branch -a (local and remote)
git branch -r (only remote branches)
git remote show origin
git checkout test - switch to test branch, now HEAD points to branch "test"
git commit -a -m "commit to test"
git checkout master - return to "master"
git commit -a -m "commit to master"
git checkout -b hotfix - create new branch "iss53" and switch to it
vim index.html
git commit -a -m "hotfix!"
git checkout master
git merge hotfix - merge "hotfix" branch to "master"
"Fast forward"
git branch -d hotfix - delete "hotfix" branch
git checkout iss53
vim index.html
git commit -a -m "commit to iss53"
git checkout master
git merge iss53
"Merge made by recursive"
git merge --no-ff helloworld (create merge commit, no fast forward)
git branch -d helloworld (delete branch)
git merge --no-ff --no-commit myfeaturebranch
git branch (show branches)
git branch -v (show branches and last commits)
git branch --merged, git branch --no-mergred
git push origin serverfix
git push origin serverfix:serverfix
git push origin serverfix:awesomebranch
git fetch origin (* [new branch] serverfix -> origin/serverfix)
git checkout -b serverfix origin/serverfix
git checkout --track origin/serverfix = git checkout -t origin/serverfix (create new branch & set up "upstream")
git checkout -b sf origin/serverfix
git checkout -b some-feature develop (base the feature branch on develop)
git push origin :serverfix ([deleted] serverfix)
http://stackoverflow.com/questions/1519006/git-how-to-create-remote-branch
git checkout origin/<branch-name> -b <branch-name>
git push -u <remote-name> <branch-name>
git branch -vv (show local branches and their tracking branches - upstream branchs)
git remote show origin (show local tracked branches)
git branch -m oldname newname (rename branch)
git branch -m newname (for current branch)
(delete branch on github)
http://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely
git push origin --delete <branchName> (As of Git v1.7.0, you can delete a remote branch using this)
git push origin :<branchName> (the same, which was added in Git v1.5.0)
(existing git branch track a remote branch, set upstream)
git branch --set-upstream branch-name origin/branch-name
--- squash merge ---
git merge --squash bugfix (this takes all commits from the bugfix branch, squash them into 1 commit and then merge it with your master branch)
git commit (without -m param to keep all commits' messages)
--- rebase ---
git checkout feature
git rebase master
(when resolving conflict with rebase:)
git rebase --abort (abort rebase)
(resolve, then:)
git add...
git rebase --continue (continue rebase after resolving conflicts)
git rebase --skip (skip commit)
(pull/push with rebase)
git pull --rebase origin master
git push origin master
rebase не является надстройкой над merge, он является надстройкой (а точнее автоматизацией) cherry-pick-ов
--- stash ---
git stash
git stash save doing crazy things (stash with comment "doing crazy things")
git stash list
git stash apply
git stash apply --index
git stash drop stash@{0}
git stash pop
git stash show -p stash@{0} | git apply -R (unapply)
git stash branch testchanges (create branch from stash)
git stash show (show files)
git stash show -p (show patch - changes)
git stash show -p stash@{0} (show patch for stash@{0})
--- config ---
git config --global user.name "Billy Everyteen"
git config --global user.email johndoe@example.com
git config --global core.editor vim
git config --global merge.tool vimdiff
git config --list (check)
git config --global user.name
git config --global user.email
git config --global core.editor
git config --global merge.tool
/etc/gitconfig -> git config --system
~/.gitconfig -> git config --global
.git/config -> git config (for repo by default, --local)
--- help ---
git help <command>
git <command> --help
man git-<command>
--- windows putty key ---
1. Open PuTTY Key Generator
2. Load *.ppk
3. Conversion->Export OpenSSH key
4. Save it to "~\.ssh\id_rsa" and to "C:\Program Files (x86)\Git\.ssh\id_rsa"
5. Add "C:\Program Files (x86)\Git\bin\" and "C:\Program Files (x86)\Git\cmd\" to PATH
6. Test ssh gituser@git.crimea.yessoftware.com, which shows what repos are allowed to write
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment