diff options
author | Suzane Sant Ana <tetestonaldo@gmail.com> | 2017-12-31 14:27:06 -0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-31 14:27:06 -0200 |
commit | 42f9329bb3a028d374d6397991ac48b44064741e (patch) | |
tree | 1e75e2b3e122aeb863e3ffa037f6f64c4027fbf8 /git.html.markdown | |
parent | e6b77595f2669d66ac7be43c6e6083cbff80a9a7 (diff) | |
parent | 70a36c9bd970b928adde06afb2bd69f6ba8e5d5c (diff) |
Merge pull request #1 from adambard/master
update
Diffstat (limited to 'git.html.markdown')
-rw-r--r-- | git.html.markdown | 279 |
1 files changed, 212 insertions, 67 deletions
diff --git a/git.html.markdown b/git.html.markdown index 04350dd5..582f8863 100644 --- a/git.html.markdown +++ b/git.html.markdown @@ -5,26 +5,33 @@ contributors: - ["Jake Prather", "http://github.com/JakeHP"] - ["Leo Rudberg" , "http://github.com/LOZORD"] - ["Betsy Lorton" , "http://github.com/schbetsy"] + - ["Bruno Volcov", "http://github.com/volcov"] + - ["Andrew Taylor", "http://github.com/andrewjt71"] + - ["Jason Stathopulos", "http://github.com/SpiritBreaker226"] + - ["Milo Gilad", "http://github.com/Myl0g"] 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 +### Centralized Versioning vs. Distributed Versioning -* Centralized version control focuses on synchronizing, tracking, and backing up files. -* Distributed version control focuses on sharing changes. Every change has a unique id. -* Distributed systems have no defined structure. You could easily have a SVN style, centralized system, with git. +* Centralized version control focuses on synchronizing, tracking, and backing +up files. +* Distributed version control focuses on sharing changes. Every change has a +unique id. +* Distributed systems have no defined structure. You could easily have a SVN +style, centralized system, with git. [Additional Information](http://git-scm.com/book/en/Getting-Started-About-Version-Control) @@ -33,65 +40,79 @@ Version control is a system that records changes to a file, or set of files, ove * Can work offline. * Collaborating with others is easy! * Branching is easy! +* Branching is fast! * Merging is easy! * Git is fast. * Git is flexible. ## Git Architecture - ### Repository -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 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. ### .git Directory (component of repository) -The .git directory contains all the configurations, logs, branches, HEAD, and more. +The .git directory contains all the configurations, logs, branches, HEAD, and +more. [Detailed List.](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html) ### 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. +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. ### 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. + +### Tag + +A tag is a mark on specific point in history. Typically people use this +functionality to mark release points (v1.0, and so on) ### 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. +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/) * [Git For Designers](http://hoth.entp.com/output/git_for_designers.html) - ## Commands - ### init -Create an empty Git repository. The Git repository's settings, stored information, -and more is stored in a directory (a folder) named ".git". +Create an empty Git repository. The Git repository's settings, stored +information, and more is stored in a directory (a folder) named ".git". ```bash $ git init @@ -99,15 +120,11 @@ $ 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" ``` @@ -131,13 +148,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 ``` -### status +### ignore files -To show differences between the index file (basically your working copy/repo) and the current -HEAD commit. +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. ```bash # Will display the branch, untracked files, changes and other differences @@ -149,8 +179,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 @@ -161,11 +191,18 @@ $ git add /path/to/file/HelloWorld.c # Regular Expression support! $ git add ./*.java + +# You can also add everything in your working directory to the staging area. +$ git add -A ``` +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. +Manage your branches. You can view, edit, create, delete branches using this +command. ```bash # list existing branches & remotes @@ -185,38 +222,85 @@ $ git branch -m myBranchName myNewBranchName $ git branch myBranchName --edit-description ``` +### tag + +Manage your tags + +```bash +# List tags +$ git tag + +# Create a annotated tag +# The -m specifies a tagging message, which is stored with the tag. +# If you don’t specify a message for an annotated tag, +# Git launches your editor so you can type it in. +$ git tag -a v2.0 -m 'my version 2.0' + +# Show info about tag +# That shows the tagger information, the date the commit was tagged, +# and the annotation message before showing the commit information. +$ git show v2.0 + +# Push a single tag to remote +$ git push origin v2.0 + +# Push a lot of tags to remote +$ git push origin --tags +``` + ### checkout -Updates all files in the working tree to match the version in the index, or specified tree. +Updates all files in the working tree to match the version in the index, or +specified tree. ```bash # Checkout a repo - defaults to master branch $ 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 ``` ### clone Clones, or copies, an existing repository into a new directory. It also adds -remote-tracking branches for each branch in the cloned repo, which allows you to push -to a remote branch. +remote-tracking branches for each branch in the cloned repo, which allows you +to push 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 -Stores the current contents of the index in a new "commit." This commit contains -the changes made and a message created by the user. +Stores the current contents of the index in a new "commit." This commit +contains the changes made and a message created by the user. ```bash # commit with a message $ git commit -m "Added multiplyNumbers() function to HelloWorld.c" + +# signed commit with a message (user.signingkey must have been set +# with your GPG key e.g. git config --global user.signingkey 5173AAD5) +$ git commit -S -m "signed commit message" + +# 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 @@ -254,7 +338,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 @@ -268,11 +352,14 @@ 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 + +# Show all commits represented by an ASCII graph +$ git log --graph ``` ### merge @@ -289,7 +376,7 @@ $ git merge --no-ff branchName ### mv -Rename or move a file +Rename or move a file ```bash # Renaming a file @@ -311,11 +398,15 @@ 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 +# By default, git pull will update your current branch +# by merging in new changes from its remote-tracking branch +$ git pull + # Merge in changes from remote branch and rebase -# branch commits onto your local repo, like: "git pull <remote> <branch>, git rebase <branch>" +# branch commits onto your local repo, like: "git fetch <remote> <branch>, git +# rebase <remote>/<branch>" $ git pull origin master --rebase ``` @@ -324,32 +415,37 @@ $ git pull origin master --rebase 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 +# By default, git push will push and merge changes from +# the current branch to its remote-tracking branch +$ git push + # 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 +$ 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. +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! +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 (uncommitted) 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") + (To restore them type "git stash apply") ``` Now you can pull! @@ -368,7 +464,8 @@ 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. +Since the "hunks" are stored in a Last-In-First-Out stack, our most recent +change will be at top. ```bash $ git stash list @@ -396,9 +493,10 @@ 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) +### rebase (caution) -Take all changes that were committed on one branch, and replay them onto another branch. +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*. ```bash @@ -412,8 +510,8 @@ $ git rebase master experimentBranch ### reset (caution) Reset the current HEAD to the specified state. This allows you to undo merges, -pulls, commits, adds, and more. It's a great command but also dangerous if you don't -know what you are doing. +pulls, commits, adds, and more. It's a great command but also dangerous if you +don't know what you are doing. ```bash # Reset the staging area, to match the latest commit (leaves dir unchanged) @@ -427,11 +525,48 @@ $ git reset --hard $ git reset 31f2bb1 # Moves the current branch tip backward to the specified commit -# and makes the working dir match (deletes uncommited changes and all commits +# and makes the working dir match (deletes uncommitted changes and all commits # after the specified commit). $ git reset --hard 31f2bb1 ``` +### reflog (caution) + +Reflog will list most of the git commands you have done for a given time period, +default 90 days. + +This give you the chance to reverse any git commands that have gone wrong +(for instance, if a rebase has broken your application). + +You can do this: + +1. `git reflog` to list all of the git commands for the rebase + +``` +38b323f HEAD@{0}: rebase -i (finish): returning to refs/heads/feature/add_git_reflog +38b323f HEAD@{1}: rebase -i (pick): Clarify inc/dec operators +4fff859 HEAD@{2}: rebase -i (pick): Update java.html.markdown +34ed963 HEAD@{3}: rebase -i (pick): [yaml/en] Add more resources (#1666) +ed8ddf2 HEAD@{4}: rebase -i (pick): pythonstatcomp spanish translation (#1748) +2e6c386 HEAD@{5}: rebase -i (start): checkout 02fb96d +``` +2. Select where to reset to, in our case its `2e6c386`, or `HEAD@{5}` +3. 'git reset --hard HEAD@{5}' this will reset your repo to that head +4. You can start the rebase again or leave it alone. + +[Additional Reading.](https://git-scm.com/docs/git-reflog) + +### revert + +Revert can be used to undo a commit. It should not be confused with reset which +restores the state of a project to a previous point. Revert will add a new +commit which is the inverse of the specified commit, thus reverting it. + +```bash +# Revert a specified commit +$ git revert <commit> +``` + ### rm The opposite of git add, git rm removes files from the current working tree. @@ -448,14 +583,24 @@ $ git rm /pather/to/the/file/HelloWorld.c * [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1) +* [Learn Git Branching - the most visual and interactive way to learn Git on the web](http://learngitbranching.js.org/) + +* [Udemy Git Tutorial: A Comprehensive Guide](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/) + +* [Git Immersion - A Guided tour that walks through the fundamentals of git](http://gitimmersion.com/) + * [git-scm - Video Tutorials](http://git-scm.com/videos) * [git-scm - Documentation](http://git-scm.com/docs) * [Atlassian Git - Tutorials & Workflows](https://www.atlassian.com/git/) -* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf) +* [SalesForce Cheat Sheet](http://res.cloudinary.com/hy4kyit2a/image/upload/SF_git_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) + +* [An introduction to Git and GitHub for Beginners (Tutorial)](http://product.hubspot.com/blog/git-and-github-tutorial-for-beginners) |