├── images └── gitdiffexample.JPG └── README.md /images/gitdiffexample.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Priyanka488/git-handbook/HEAD/images/gitdiffexample.JPG -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-handbook 2 | Just so that I don't forget the git commands ! 3 | 4 | ## What is Version Control ? 5 | 6 | A version control system is just a software that helps you manage different versions of your project. 7 | When you use version control, you create safe points that save your project, giving you total freedom and security to change anything about your project, and not worry about losing any of your data. 8 | 9 | The main point of a VCS is to help us maintain a detailed history of the project, and to work onn different versions of it too. 10 | 11 | A Source Code Manager(SCM) is another name for a version control system. 12 | 13 | ## Types of Version Control System Models 14 | 15 | 1. Centralised : A central computer hosts all the projects, and all the interactions should happen through it. 16 | 2. Distributed : There's no such central repository, each developer has a complete copy of the project on their system. 17 | 18 | ## Most Popular Version Control Systems 19 | 20 | 1. git 21 | 2. Subversion 22 | 3. Mercurial 23 | 24 | ## Git vs Github : 25 | 26 | Git is a distributed version control system. Git and Github are different from each other. Git is a version control tool while Github is a service that hosts git projects. 27 | 28 | ## git- Key Terms 29 | 30 | ### 1. Version control system (VCS)- 31 | 32 | A VCS allows you to revert a file/project to a previous state, to see who made the changes and when or who introduced an issue etc. 33 | 34 | ## 1. Git init 35 | The command is used to initialise a local repo as git repo. 36 | ``` git init``` 37 | 38 | ## 2. Git clone 39 | 40 | To clone a repo : 41 | 42 | ``` git clone ``` 43 | 44 | To clone a repo in a directory with a different name 45 | 46 | ``` git clone dir-name ``` 47 | 48 | At this point of time, you must be having a Git repo on your local machine, and a checkout/ working-copy of all the files.
49 | So now, you make changes.
50 | And at any point you wish to record the state of the project, you commit the snapshot of those changes
51 |
52 | 53 | ### States of a File : 54 | 55 | 1. Tracked - the files that Git knows about, can be modified, unmodified or staged. 56 | 2. Untracked - any file that wasn't present in your last snapshot. 57 | 58 | Note : Initially, all files are tracked and unmodified.Once you make changes to any file, Git marks them as modified (since it has changed in comparison to the last commit), and then as you work, you selectively stage the modified files and commit the staged changes. And the cycle repeats !
59 | 60 | Untracked -> Staged (Add the file)
61 | Unmodified -> Modified (Edit the file)
62 | Modified -> Staged (Stage the file )
63 | Staged -> Unmodified (Committed the file)
64 | Unmodified -> Untracked (removed the file)
65 | 66 | ## 3. Git Status 67 | 68 | used to determine which files are in which state. 69 | 70 | ``` git status 71 | On branch master 72 | Your branch is up-to-date with 'origin/master'. 73 | nothing to commit, working directory clean 74 | ``` 75 | 76 | Means - 77 | 1. Tells that you are on the branch master. 78 | 2. Informs that the the branch has not diverged from the same branch on the server. 79 | 3. Clean working directory - meaning by that none of the tracked files have been modified. 80 | 4. If it shows "Changes to be committed:" then it means that the following files have been staged. 81 | 5. If it shows "Changes not staged for commit :" means that a file that is tracked has been modified in the working directory but not yet staged. To stage it, you run the ```git add``` command. 82 | 5. If it shows "Untracked files", then you can track/stage it using ```git add```. 83 | 84 | ## 4. Git add 85 | 86 | used to track new files. 87 | 88 | ``` git add ``` 89 | 90 | Once you run ```git add``` on a file, it gets tracked and staged. 91 | 92 | Basically, ``` git add``` is a multipurpose command, you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. It may be helpful to think of it more as “add precisely this content to the next commit” rather than “add this file to the project”. 93 | 94 | Note : Git stages a file exactly as it is when you run the git add command. So, If you modify a file after you run git add, you have to run git add again to stage the latest version of the file. 95 | 96 | Suppose, you modified a file after running git add, and do ```git status```, the file name will be shown both under the staged section, as well as the unstaged section. And in case you do, ```git commit``` from here , the state of the file on that previous git add gets committed , and not the current state. 97 | 98 | So, to get the current state of the file to get committed, you must run ``` git add ``` again. 99 | 100 | ## 5. Git ignore 101 | 102 | For files such as log files, or build system files that you don't want Git to add or show them as untracked, you can ask Git to ignore them using the ```.gitignore``` file. 103 | 104 | ``` 105 | cat .gitignore 106 | *.[oa] 107 | *~ 108 | node_modules/ 109 | ``` 110 | 111 | Here, the first line archives all the files ending with ```.o``` or ```.a```.
112 | The second line archives all the files ending with ```.~```.
113 | The third line archives the entire directory called node_modules.
114 | 115 | Note : Always set up a ```.gitignore``` file for a new project, before you get going so that you don't accidently commit any such files. 116 | 117 | ## 6. Git diff 118 | 119 | The command is used to know what exactly were the changes, and not just the files where you made the changes. 120 | ``` git diff``` shows you the exact lines added and removed- the patch, as they were. The command basically answers two questions - What changes are yet to be staged ( use ``` git diff ```) ? and What staged changes are yet to get committed ?( use ```git diff --staged```)
121 | 122 | 123 | Suppose you have two files, ```readme.txt``` and ```contr.txt```, and you've staged readme but not contr, you get the following response - 124 | 125 | 126 | --------------------------------------------------------------------------------