diff options
Diffstat (limited to 'git.html.markdown')
| -rw-r--r-- | git.html.markdown | 179 | 
1 files changed, 145 insertions, 34 deletions
| diff --git a/git.html.markdown b/git.html.markdown index 00f38d60..b1347309 100644 --- a/git.html.markdown +++ b/git.html.markdown @@ -2,22 +2,23 @@  category: tool  tool: git  contributors: -    - ["Jake Prather", "http:#github.com/JakeHP"] +    - ["Jake Prather", "http://github.com/JakeHP"] +    - ["Leo Rudberg" , "http://github.com/LOZORD"] +    - ["Betsy Lorton" , "http://github.com/schbetsy"]  filename: LearnGit.txt -  --- -Git is a distributed version control and source code management system.  +Git is a distributed version control and source code management system. -It does this through a series of snapshots of your project, and it works  -with those snapshots to provide you with functionality to version and  +It does this through a series of snapshots of your project, and it works +with those snapshots to provide you with functionality to version and  manage your source code.  ## Versioning Concepts  ### What is version control? -Version control is a system that records changes to a file, or set of files, over time. +Version control is a system that records changes to a file(s), over time.  ### Centralized Versioning VS Distributed Versioning @@ -41,8 +42,9 @@ Version control is a system that records changes to a file, or set of files, ove  ### Repository -A set of files, directories, historical records, commits, and heads. Imagine it as a source code datastructure,  -with the attribute that each source code "element" gives you access to its revision history, among other things. +A set of files, directories, historical records, commits, and heads. Imagine it +as a source code data structure, with the attribute that each source code +"element" gives you access to its revision history, among other things.  A git repository is comprised of the .git directory & working tree. @@ -53,31 +55,37 @@ The .git directory contains all the configurations, logs, branches, HEAD, and mo  ### Working Tree (component of repository) -This is basically the directories and files in your repository. It is often referred to -as your working directory. +This is basically the directories and files in your repository. It is often +referred to as your working directory.  ### Index (component of .git dir)  The Index is the staging area in git. It's basically a layer that separates your working tree -from the Git repository. This gives developers more power over what gets sent to the Git -repository. +from the Git repository. This gives developers more power over what gets sent +to the Git repository.  ### Commit -A git commit is a snapshot of a set of changes, or manipulations to your Working Tree. -For example, if you added 5 files, and removed 2 others, these changes will be contained -in a commit (or snapshot). This commit can then be pushed to other repositories, or not! +A git commit is a snapshot of a set of changes, or manipulations to your Working +Tree. For example, if you added 5 files, and removed 2 others, these changes +will be contained in a commit (or snapshot). This commit can then be pushed to +other repositories, or not!  ### Branch -A branch is essentially a pointer that points to the last commit you made. As you commit, -this pointer will automatically update and point to the latest commit. +A branch is essentially a pointer to the last commit you made. As you go on +committing, this pointer will automatically update to point the latest commit.  ### HEAD and head (component of .git dir)  HEAD is a pointer that points to the current branch. A repository only has 1 *active* HEAD.  head is a pointer that points to any commit. A repository can have any number of heads. +### Stages of Git +* Modified - Changes have been made to a file but file has not been committed to Git Database yet +* Staged - Marks a modified file to go into your next commit snapshot +* Committed - Files have been committed to the Git Database +  ### Conceptual Resources  * [Git For Computer Scientists](http://eagain.net/articles/git-for-computer-scientists/) @@ -89,7 +97,7 @@ head is a pointer that points to any commit. A repository can have any number of  ### init -Create an empty Git repository. The Git repository's settings, stored information,  +Create an empty Git repository. The Git repository's settings, stored information,  and more is stored in a directory (a folder) named ".git".  ```bash @@ -98,15 +106,12 @@ $ git init  ### config -To configure settings. Whether it be for the repository, the system itself, or global -configurations. +To configure settings. Whether it be for the repository, the system itself, +or global configurations ( global config file is `~/.gitconfig` ).  ```bash  # Print & Set Some Basic Config Variables (Global) -$ git config --global user.email -$ git config --global user.name -  $ git config --global user.email "MyEmail@Zoho.com"  $ git config --global user.name "My Name"  ``` @@ -130,12 +135,26 @@ $ git help -a  $ git help add  $ git help commit  $ git help init +# or git <command_here> --help +$ git add --help +$ git commit --help +$ git init --help  ``` +### ignore files + +To intentionally untrack file(s) & folder(s) from git. Typically meant for +private & temp files which would otherwise be shared in the repository. +```bash +$ echo "temp/" >> .gitignore +$ echo "private_key" >> .gitignore +``` + +  ### status -To show differences between the index file (basically your working copy/repo) and the current -HEAD commit. +To show differences between the index file (basically your working copy/repo) +and the current HEAD commit.  ```bash @@ -148,8 +167,8 @@ $ git help status  ### add -To add files to the current working tree/directory/repo. If you do not `git add` new files to the -working tree/directory, they will not be included in commits! +To add files to the staging area/index. If you do not `git add` new files to the +staging area/index, they will not be included in commits!  ```bash  # add a file in your current working directory @@ -162,6 +181,9 @@ $ git add /path/to/file/HelloWorld.c  $ git add ./*.java  ``` +This only adds a file to the staging area/index, it doesn't commit it to the +working directory/repo. +  ### branch  Manage your branches. You can view, edit, create, delete branches using this command. @@ -193,7 +215,8 @@ Updates all files in the working tree to match the version in the index, or spec  $ git checkout  # Checkout a specified branch  $ git checkout branchName -# Create a new branch & switch to it, like: "git branch <name>; git checkout <name>" +# Create a new branch & switch to it +# equivalent to "git branch <name>; git checkout <name>"  $ git checkout -b newBranch  ``` @@ -206,6 +229,10 @@ to a remote branch.  ```bash  # Clone learnxinyminutes-docs  $ git clone https://github.com/adambard/learnxinyminutes-docs.git +# shallow clone - faster cloning that pulls only latest snapshot +$ git clone --depth 1 https://github.com/adambard/learnxinyminutes-docs.git +# clone only a specific branch +$ git clone -b master-cn https://github.com/adambard/learnxinyminutes-docs.git --single-branch  ```  ### commit @@ -216,6 +243,12 @@ the changes made and a message created by the user.  ```bash  # commit with a message  $ git commit -m "Added multiplyNumbers() function to HelloWorld.c" + +# automatically stage modified or deleted files, except new files, and then commit +$ git commit -a -m "Modified foo.php and removed bar.php" + +# change last commit (this deletes previous commit with a fresh commit) +$ git commit --amend -m "Correct message"  ```  ### diff @@ -253,7 +286,7 @@ $ git config --global alias.g "grep --break --heading --line-number"  $ git grep 'variableName' -- '*.java'  # Search for a line that contains "arrayListName" and, "add" or "remove" -$ git grep -e 'arrayListName' --and \( -e add -e remove \)  +$ git grep -e 'arrayListName' --and \( -e add -e remove \)  ```  Google is your friend; for more examples @@ -267,8 +300,8 @@ Display commits to the repository.  # Show all commits  $ git log -# Show X number of commits -$ git log -n 10 +# Show only commit message & ref +$ git log --oneline  # Show merge commits only  $ git log --merges @@ -288,7 +321,7 @@ $ git merge --no-ff branchName  ### mv -Rename or move a file	 +Rename or move a file  ```bash  # Renaming a file @@ -310,7 +343,12 @@ Pulls from a repository and merges it with another branch.  # Update your local repo, by merging in new changes  # from the remote "origin" and "master" branch.  # git pull <remote> <branch> +# git pull => implicitly defaults to => git pull origin master  $ git pull origin master + +# Merge in changes from remote branch and rebase +# branch commits onto your local repo, like: "git pull <remote> <branch>, git rebase <branch>" +$ git pull origin master --rebase  ```  ### push @@ -318,14 +356,81 @@ $ git pull origin master  Push and merge changes from a branch to a remote & branch.  ```bash -# Push and merge changes from a local repo to a  +# Push and merge changes from a local repo to a  # remote named "origin" and "master" branch.  # git push <remote> <branch>  # git push => implicitly defaults to => git push origin master  $ git push origin master + +# To link up current local branch with a remote branch, add -u flag: +$ git push -u origin master +# Now, anytime you want to push from that same local branch, use shortcut: +$ git push +``` + +### stash + +Stashing takes the dirty state of your working directory and saves it on a stack +of unfinished changes that you can reapply at any time. + +Let's say you've been doing some work in your git repo, but you want to pull +from the remote. Since you have dirty (uncommited) changes to some files, you +are not able to run `git pull`. Instead, you can run `git stash` to save your +changes onto a stack! + +```bash +$ git stash +Saved working directory and index state \ +  "WIP on master: 049d078 added the index file" +  HEAD is now at 049d078 added the index file +  (To restore them type "git stash apply") +``` + +Now you can pull! + +```bash +git pull +``` +`...changes apply...` + +Now check that everything is OK + +```bash +$ git status +# On branch master +nothing to commit, working directory clean +``` + +You can see what "hunks" you've stashed so far using `git stash list`. +Since the "hunks" are stored in a Last-In-First-Out stack, our most recent change will be at top. + +```bash +$ git stash list +stash@{0}: WIP on master: 049d078 added the index file +stash@{1}: WIP on master: c264051 Revert "added file_size" +stash@{2}: WIP on master: 21d80a5 added number to log  ``` -### rebase (caution)  +Now let's apply our dirty changes back by popping them off the stack. + +```bash +$ git stash pop +# On branch master +# Changes not staged for commit: +#   (use "git add <file>..." to update what will be committed) +# +#      modified:   index.html +#      modified:   lib/simplegit.rb +# +``` + +`git stash apply` does the same thing + +Now you're ready to get back to work on your stuff! + +[Additional Reading.](http://git-scm.com/book/en/v1/Git-Tools-Stashing) + +### rebase (caution)  Take all changes that were committed on one branch, and replay them onto another branch.  *Do not rebase commits that you have pushed to a public repo*. @@ -377,6 +482,8 @@ $ git rm /pather/to/the/file/HelloWorld.c  * [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1) +* [Udemy Git Tutorial: A Comprehensive Guide](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/) +  * [git-scm - Video Tutorials](http://git-scm.com/videos)  * [git-scm - Documentation](http://git-scm.com/docs) @@ -386,3 +493,7 @@ $ git rm /pather/to/the/file/HelloWorld.c  * [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)  * [GitGuys](http://www.gitguys.com/) + +* [Git - the simple guide](http://rogerdudler.github.io/git-guide/index.html) + +* [Pro Git](http://www.git-scm.com/book/en/v2) | 
