| | 1 | This page contains a basic tutorial for working with git. There aren't any advanced concepts here, just the basics. In this tutorial we'll be working with a testing git repo and fake files. |
| | 2 | |
| | 3 | Its always a good thing to remember that while working with Git is a decentralized VCS, even though you may choose to push changes all to one server, you don't have to. Each repo contains a full history and set of objects in the .git directory. |
| | 4 | |
| | 5 | You will also only see a .git directory in the top level unlike svn where you have .svn in each sub directory as well as the top level. |
| | 6 | |
| | 7 | Unlike svn, git uses hashes to identify commits instead of revision numbers. |
| | 8 | |
| | 9 | These are just some basic tasks, the man pages can go into a lot more detail. |
| | 10 | |
| | 11 | === Initializing a Git Repo === |
| | 12 | |
| | 13 | Create a empty working directory: |
| | 14 | |
| | 15 | {{{ |
| | 16 | mkdir git-tutorial |
| | 17 | cd git-tutorial |
| | 18 | }}} |
| | 19 | |
| | 20 | Initialize the local Git Repo: |
| | 21 | |
| | 22 | {{{ |
| | 23 | git init |
| | 24 | }}} |
| | 25 | |
| | 26 | === Committing files to the Repo === |
| | 27 | |
| | 28 | Lets create a test file: |
| | 29 | |
| | 30 | {{{ |
| | 31 | echo "Test Data" > testfile |
| | 32 | }}} |
| | 33 | |
| | 34 | Stage the file to be included into the next commit: |
| | 35 | |
| | 36 | {{{ |
| | 37 | git add testfile |
| | 38 | }}} |
| | 39 | |
| | 40 | Finally commit the file to the repo: |
| | 41 | |
| | 42 | {{{ |
| | 43 | git commit -m "Add a test file" |
| | 44 | }}} |
| | 45 | |
| | 46 | === Viewing changes to files before committing them === |
| | 47 | |
| | 48 | First lets make a change to our test file: |
| | 49 | |
| | 50 | {{{ |
| | 51 | echo "Different Test Data" > testfile |
| | 52 | }}} |
| | 53 | |
| | 54 | View the changes: |
| | 55 | |
| | 56 | {{{ |
| | 57 | git diff |
| | 58 | }}} |
| | 59 | |
| | 60 | You could also execute the following or something similar: |
| | 61 | |
| | 62 | {{{ |
| | 63 | git diff testfile |
| | 64 | }}} |
| | 65 | |
| | 66 | === Listing files with changes === |
| | 67 | |
| | 68 | You can use the following command to list the status of all files not ignored by Git: |
| | 69 | |
| | 70 | {{{ |
| | 71 | git status |
| | 72 | }}} |
| | 73 | |
| | 74 | You can also use the command on a path, the same as git diff. |
| | 75 | |
| | 76 | === Revert a file back to a specified commit === |
| | 77 | |
| | 78 | In this case we want to revert to the current working ref, so we wont specify one: |
| | 79 | |
| | 80 | {{{ |
| | 81 | git checkout testfile |
| | 82 | }}} |
| | 83 | |
| | 84 | === Reverting the entire working tree === |
| | 85 | |
| | 86 | You can use the following command to revert the entire working tree to the last commit in the branch you're working with: |
| | 87 | |
| | 88 | {{{ |
| | 89 | git reset --hard |
| | 90 | }}} |