// 1. Create a new repository
$ git init --bare sample.git
// 2. Addding a existing project to git
1)Initialize the local directory as a Git repository.
$ git init
2)Add the files in your new local repository. This stages them for the first commit.
$ git add .
3)Commit the files that you've staged in your local repository.
$ git commit -m "First commit"4)In Terminal, add the URL for the remote repository where your local repository will be pushed.
// Sets the new remote
$ git remote add origin remote repository URL
// Verifies the new remote URL
$ git remote -v
5)Push the changes in your local repository to GitHub.
// Pushes the changes in your local repository up to the remote repository you specified as the origin
$ git push -u origin master
操作 branch
123456789101112131415161718192021222324
// List all existing branches
$ git branch
// Switch HEAD branch
$ git checkout <branch>
// Create a new branch based on your current HEAD
$ git branch <new-branch>
// Create a new tracking branch based on a remote branch
$ git branch --track <new-branch> <remote-branch>
// Delete a local branch
$ git branch -d <branch>
// Delete a remote branch
$ git push origin --delete <branch>
// Rename a branch locally
$ git branch -m <old name> <new name>
// Rename a branch on remote
$ git push <remote> :<old name>
$ git push <remote> <new name>
// Add all current changes in file to the next commit
$ git add <file>
// Delete file and add its deletion to next commit
$ git rm <file>
// Rename file and add it to next commit
$ git mv <file> <new file name>
// Show changes over time for a specific file
$ git log -p <file>
// Who changed what and when in file
$ git blame <file>
// Remove file from all previous commits but keep it locally
// 如何不再控制已经提交到仓库的文件?
$ git rm --cached <file>
// Manually resolve conflicts using your editor and mark file as resolved
$ git add <resolved-file>
$ git rm <resolved-file>
// Discard local changes in a specific file
$ git checkout HEAD <file>
// Revert a commit by providing a new commit with contrary changes
$ git revert <commit>
// Restore a specific file from a previous commit
$ git checkout <commit> <file>
// 如何将另一分支上的某文件复制到当前分支上来?
$ git checkout currentBranch
$ git checkout anotherBranch -- filenameYourWantedCopy
// 不再跟踪已经删除的文件
$ git add --update
// Shows the commits that changed specific file
$ git log --follow builtin/rev-list.c
// Shows the commits that changed builtin/rev-list.c, including those commits that occurred before the file was given its present name.
// Viewing a Deleted File
$ git show HEAD^:path/to/file
// You can use an explicit commit identifier or HEAD~n to see older versions or if therehas been more than one commit since you deleted it.
// Finally, most commands that take filenames will optionally allow you to precede any
// filename by a commit, to specify a particular version of the file:
$ git diff v2.5:Makefile HEAD:Makefile.in
// You can also use git show to see any such file:
$ git show v2.5:Makefile
Reference:[Viewing a Deleted File in Git](http://stackoverflow.com/questions/1395445/viewing-a-deleted-file-in-git)// How to take a file out of another commit
// Example 1
$ git checkout master~2 Makefile
// Example 2
$ git checkout 4280f4a14319752308007124cb2a15fffd696025 Networking.podspec
// Check whether file tracked or not
// Ref: https://stackoverflow.com/questions/2405305/how-to-tell-if-a-file-is-git-tracked-by-shell-exit-code
$ git ls-files --error-unmatch <file name>
操作 commit
1234567891011121314
// Commit previously staged changes
$ git commit
// Show all commits
$ git log
// Show changes over time for a specific committer
$ git log --author=<committer name>
// Change the last commit
$ git commit -amend
// Change published commit message
$ git rebase -i HEAD~3
问题汇总
1.如何将目录覆盖下一级的同名目录?
123456789101112
// 背景
|-A
|-B
|-A
// 目标
|-B
|-A
// 解决办法
1. rsync -a A/ B/A
2. (cd A && tar c .)|(cd B/A && tar xf -)
2.insufficient permission for adding an object to repository database ./objects
3.warning: remote HEAD refers to nonexistent ref, unable to checkout
Solution:问题的原因是远程仓库的默认分支名不为master。
1234567891011
// Login to remote server
git branch -a
git branch -m branchName master
// localgit clone .... // Now the warn will be disappear
git fetch
git add .
git commit
git push ...
A: If you use git rm git will remove all versions of that path from the index so your resolve action will leave you without either version.
You can use git checkout --ours file to chose the version from the branch onto which you are rebasing or git checkout --theirs file to chose the version from the branch which you are rebasing.
If you want a blend you need to use a merge tool or edit it manually.
Don’t rewrite your history and try to push again, and don’t push to a parallel
Git repository to collaborate with fellow Git developers at the same time. Subversion can have only a single linear history, and confusing it is very easy. If you’re working with a team, and some are using SVN and others are using Git, make sure everyone is using the SVN server to collaborate – doing so will make your life easier.
Two solutions (or rather workarounds) that come to mind are:
Use shallow clone i.e. git clone –depth=1, then deepen this clone using git fetch –depth=N, with increasing N. You can use git fetch –unshallow (since 1.8.0.3) to download all remaining revisions.
Ask somebody to bundle up to some tagged release (see git-bundle(1) manpage). The bundle itself is an ordinary file, which you can download any way, via HTTP/FTP with resume support, via BitTorrent, via rsync, etc. The you can create clone from bundle, fix configuration, and do further fetches from official LibreOffice repository.
Reference:How to complete a git clone for a big project on an unstable connection?
15.fatal: unable to access 'https://github.com/BroadleafCommerce/BroadleafCommerce.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443