version 1.0.0
Branchesare just labels associated to commits and they happen on single repositoryRemoteare references of remote repo to local repo
-
git --version: check git version -
git status: check the status of local machine and remote repository -
git help: -
git diff:working directory&staging area -
git diff --staged:staging area&most recent commit -
git diff commit1 commit2:commit1&commit2 -
git diff branch1 branch2:branch1&branch2 -
git add <filename>: add new file or file with changes to github -
git add -A: add all to github -
git add .: add all to github -
git commit -m 'messages': new file or changes committed but still on local machine -
git commit -a -m 'messages': commit all the changes -
git log: get the history of all the commits or branch -
git pull origin master: pull master branch from origin to the local branch we are working on -
git push origin master: push changes in local branch we are working on to remote origin master branch -
git clone github_url: download repo from the github, create connection through origin -
git remote -v: viewing info about the remote repository -
git branch -a: viewing info about the branch -
git branch -av: viewing info about the branch -
git branch -u bb/master重新调整local git repo track 哪一个remote,这里是track bb 的master branch -
git remote add origin <url_of_repo>: (if you don't have the origin) -
git init: initialize the local repo -
rm -rf .git: to remove the folder from git repository, to stop tracking this project on github -
touch .gitignore: To create the list of files to ignore during version control -
git reset <filename>: remove file in the staging area -
git reset: remove everything in the staging area -
git fetch: download a copy of remote repo to localorigin/masterbookmark -
git fetch origin: update all the local copies from origin remote -
git log origin/masterorgit diff origin/master master: check out the local copies -
git merge: operates on commits -
git merge master origin/master: merge localorigin/masterwith local master branch -
git pull=git fetch+git merge master origin/master -
git checkout -b new_branch=git branch new_branch+git checkout new_branch -
git show commit_name: show the difference of this commit with its parent commit -
git log --graph: show the commits history graph
2. Github by tasks:
- git config --global user.name "Stanley Song"
- git config --global user.email "luge1123@gmail.com"
- git config --list
- git help config or git config --help: so we can use
git add --helpas well
- git clone <url_of_repo_others>
- git remote -v: show the references connected to your repo
- git branch -a: check the branches locally and remotely
- If origin is in the original repo that you wanna copy, add the orginal repo as upstream, delete origins and add your own repo, as below:
- git remote rm origin
- git remote add origin <url_of_repo_own>
- git config master.remote origin
- git config master.merge refs/heads/master
- git pull origin master --allow-unrelated-histories
- make some changes: touch README.md
- git status: check the status
- git commit -m 'message'
- git log: check whether commit successfully
- git push origin master: push updates from local master branch to the origin remote
- git clone < your-repo-URL >
- git remote add upstream <url_of_original_repo>: 把原创作者加到上游
- git remote -v: check the remote status
- git fetch upstream: fetch updates from the upstream
- git merge upstream/master: merge upstream branch into our working branch in local machine
- 或者直接 git pull upstream master
- git push origin master: push changes of local directory to my origin repo in github website
- create a repo in github and initialize with a README file
- plan: make changes to local repo and get synced with the repo in github
- mkdir test1: create a dir in local machine
- cd test1: change directory to the sub-directory:
- git init: Initialize git
- git remote add origin <url_of_repo>: Add remote that allows you easily reference your github layer
- Then make some changes in your local machine
- git status: confirm git has detected untracked file
- git add .: stage the file to be commited
- git commit -m 'message': commit the changes
- git push origin master: which direct git to push the committed changes on the master branch to the origin that we defined earlier
Unfortunately, the push action is rejected since git detects the README file in github but not in your local machine. To fix the problem:
- git pull origin master, then git push origin master; if got
refusing to merge unrelated historiesproblem, try git pull origin master --allow-unrelated-histories
Another three approaches to help you aviod this situation in the begining:
-
- Do not create the README file when you create the github repo in the beginning
-
- Using the cloning approach:(It automatically downloads the repo and stores it in a sub directory of your working directory; it also automatically sets up the origin remote so you don't have to add the remote manually; local repo and github repo are already in sync)
- Create the repo with README
- git clone <url_of_repo>
- Make changes to local repo
- git add .
- git commit -m 'message'
- git push origin master
-
- 或者
git fetch origin: update all the local copies of every branch for the origin remotegit log origin/master: inspect local copies- or
git diff origin/master master: 看local origin/master与当前branch的difference git merge master origin/master: 如果merge出现错误,手动解决错误,然后 add,commit etc...或者强行mergegit fetch origin: Fetch commits on "origin", and update all your remote tracking referencegit reset --hard origin/master: Force the local master to "origin/master"
- git init
- git add . : add files from working directory to staging area
- git reset [file] : if you want to unstage
- git commit -m "First commit"
- git remote add origin remote_epository_URL (所以先要在github创建repo)
- git remote -v
- git push origin master: push files in local master to remote origin (master branch)
- touch .gitignore
- Open the
.gitignorefile with editor(sublime): subl .gitignore - key in the list of files to ignore
*.DS_Store*.project*.pyc*.ipynb_checkpoints*.spyproject*Icon
- Untrack files already added to git repository based on .gitignore
- git rm -r --cached . : This removes everything from the index
- git add .
- git commit -m ".gitignore is now working"
- git push -u origin master
- 1.如果对某一文件
calc.py作出改变,但是我们不想要此改变,想 restore 到之前的状态- git status
- git diff
- git checkout calc.py
- git checkout -b branch_name
- git add .
- git commit -m "changes you've made"
- git push -u branch_name
- git checkout master
- git pull --rebase
- git push -u origin master
Check out the branch, and rebase from master.
- git checkout branch_name
- git rebase master
- git log to check which commit you want to revert
- git revert commit_id
- git push origin master
-
1.Unable to push code to my private repo
- 报错:
repository access denied. access via a deployment key is read-only.fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. - 测试
ssh -T git@bitbucket.org:authenticated via a deploy key. You can use git or hg to connect to Bitbucket. Shell access is disabled. This deploy key has read access to the following repositories: myName/repoName: xxxxxx -- my_email_address - 解决:
- 在这里真的是忍不住吐槽
- Remove the SSH key from the repo. (Click on repo name > Settings > Access Keys)
- Add SSH key to Account settings SSH keys. (Click on your avatar > Bitbucket Settings > SSH Keys)
- 报错:
-
2.如果不小心 commit 了大文件, 采取以下步骤:
- 删除github repo,并重新建立repo
- add origin 来建立连接
- git push -u origin master
上面这个方法太笨,看以下:- To remove the last commit from git, you can simply run
git reset --hard HEAD^. If you are removing multiple commits from the top, you can rungit reset --hard HEAD~2to remove the last two commits. You can increase the number to remove even more commits. - If you want to "uncommit" the commits, but keep the changes around for reworking, remove the "--hard":
git reset HEAD^which will evict the commits from the branch and from the index, but leave the working tree around. - If you want to save the commits on a new branch name, then run
git branch newbranchnamebefore doing the git reset. - Delete first commit:
git update-ref -d HEAD
- To remove the last commit from git, you can simply run
-
3.git push Out of memory, malloc failed:
- git gc --auto --prune=today --aggressive
- git repack
- git config --global http.postbuffer 524288000
- git config --global pack.windowMemory 256m
-
4.How to fix Git always asking for user credentials
- Update the URL of origin remote using SSH instead of HTTPS: git remote set-url origin git@github.com:username/repo.git
git branch calc-divide: assumme we are editing a function within a python file calc-divide- 或者加上
-b:git branch -b calc-divide git checkout calc-divide: now we are switching to work on this branch calc-divide, then we can make changes to the python filegit status: take a look at the changesgit commit -m "Divide Function": push the changes to the staging area, this will have no effect on the local master brunch or remote repogit diff --staged: Optional, do this before commit, check the difference bettween the staging area and the most recent commit
git push -u origin calc-divide:-utells git that we wanna associate the local calc-divide branch with the remote calc-divide branch, so in future we can just callgit pullgit pushdirectly and git knows these branches are associated; 在这里,remote也自动创造了这个calc-divide的分枝git branch -a: check both local and remote branches
- (local先merge, push之后remote也跟着merge)
git checkout master: switch to work on master branchgit pull origin master: 养成良好的从 remote pull 的好习惯git branch merged: check the branch we've merged so fargit merge calc-divide: merge calc-divide with the local master branchgit push origin master: after this, the remote calc-divide branch will merge with the remote master branch as well
git branch --merged: check the branch we've merged once againgit branch -d calc-divide: 砍掉local的分枝calc-dividegit branch -agit push origin --delete calc-divide: 砍掉remote repo的分枝calc-divide
git branch subtractgit checkout subtract: then make changes tosubtractgit add .git commit -m "editing subtract"git push -u origin subtractgit branch -agit checkout mastergit pull origin mastergit branch --mergedgit merge subtractgit push origin mastergit branch --mergedgit branch -d subtractgit branch -agit push origin --delete subtract
diff -u <file1> <file2>- Concept Map: init, add, staging area
- Draw an updated diagram
- Concept Map: GitHub, Push, Pull, Remote
- start a working area (see also: git help tutorial)
- clone: Clone a repository into a new directory
- init: Create an empty Git repository or reinitialize an existing one
- work on the current change (see also: git help everyday)
- add: Add file contents to the index
- mv: Move or rename a file, a directory, or a symlink
- reset: Reset current HEAD to the specified state
- rm: Remove files from the working tree and from the index
- examine the history and state (see also: git help revisions)
- bisect: Use binary search to find the commit that introduced a bug
- grep: Print lines matching a pattern
- log: Show commit logs
- show: Show various types of objects
- status: Show the working tree status
- grow, mark and tweak your common history
- branch: List, create, or delete branches
- checkout: Switch branches or restore working tree files
- commit: Record changes to the repository
- diff: Show changes between commits, commit and working tree, etc
- merge: Join two or more development histories together
- rebase: Reapply commits on top of another base tip
- tag: Create, list, delete or verify a tag object signed with GPG
- collaborate (see also: git help workflows)
- fetch: Download objects and refs from another repository
- pull: Fetch from and integrate with another repository or a local branch
- push: Update remote refs along with associated objects
Open terminal and type:
1. Create a directory at ~/bin:
mkdir ~/bin
2. Copy sublime executable to your ~/bin directory:
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl
3a. If using bash (Mac OS default) add line to ~/.bash_profile file:
echo 'export PATH=$PATH:$HOME/bin' >> ~/.bash_profile
4a. Set sublime as your default editor
echo "export EDITOR='subl -w'" >> ~/.bash_profile
or
3b. If using zsh add line to ~/.zshrc file:
echo 'export PATH=$PATH:$HOME/bin' >> ~/.zshrc
4b. Set sublime as your default editor
echo "export EDITOR='subl -w'" >> ~/.zshrc
5. Restart terminal and type:
subl .
Sublime should open up in the current directory.
6. Check unix commands:
ls
You should get a directory listing.
Put the C:\Program Files\Sublime Text 2 in your PATH.
Create a subl.bat file and save it in the directory: C:\Program Files\Sublime Text 2
Inside the file put: start sublime_text.exe %*
If you have questions email me:
Sublime Text includes a command line tool, subl, to work with files on the command line. This can be used to open files and projects in Sublime Text, as well working as an EDITOR for unix tools, such as git and subversion.
- Sublime text 2 or 3 installed in your system within
Applicationsfolder
In order to launch sublime from command line you only need to create a symlink /usr/local/bin/subl point to sublime app, to do so run the following in the command line.
ln -sv "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
ln -sv "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
type in command line
subl test.rb
it should open new file test.rb sublime text
NOTE: To accomplish this you must:
have created a directory where you actually place binaries /usr/local/bin if not make it before creating a symlink:
mkdir -p /usr/local/bin
have /usr/local/bin in your PATH environment variable, if not add by running the following command:
echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile
then reload the shell:
source ~/.bash_profile
test again.
Further info read from sublime docs
