GIT good book pro git book by Scott Chacon great youtube channel with git tutorial by David Mahler: https://www.youtube.com/watch?v=uR6G2v_WsRA -Used for version control and to save and store snapshots of versions of code. -Code can be uploaded (pushed) to repositories like GitHub where other developers can access and work on it. INSTALLING: http://git-scm.com/download/ download the installer, comes with gitbash (use this) which is a command line tool (can be used over the standard command line in windows, similar to linux). Note: you can use git on either the windows command line or with gitbash command line tool (recommended), it works in both. To start GitBash, right click anywhere in a folder or desktop and select GitBash here option in the context menu. This makes that location the local repository. --------------- COMMANDS: Main Commands: $ git init - starts a local repository and creates a folder for it on your computer. creates a .git folder which is hidden by default (usually don't need to go into this folder) $ git add - adds files to a staging area of the index, and makes them ready for a commit. $ git status - used to see what you have in the staging area which is ready for commit. $ git commit - takes everything that's in the staging area index and sends it to the local repository (i.e. on your computer). $ git push - takes the local repository and pushes it to a remote repository (like github). $ git pull - pull the latest file/version from the remote repository (github). $ git clone - pulls an entire project (all files, not just the latest) or module from the remote repository to your local repository. $ touch [filename] (ex: touch index.html) <--- this creates a file. ------------------------------------ OTHER COMMANDS: GIT IGNORE <---USED IF YOU DON'T WANT SOMETHING INCLUDED (even if you use add ., it will skip and ignore these files -- usually log.txt log files, for example.). -Create a .gitignore file with the command $ touch .gitignore -in the .gitignore file, simply type the name of the file(s) you want to ignore (i.e. log.txt, etc.) --git will then exclude these files from future commands. Note: you can also ignore entire directories/folders (use the forward slash, and then type the name of the folder: /dirName) Ex: in .gitignore file, type: /directoryName (/jsfolder) etc. -can also ignore entire types of files with *.fileExt (ex: *.txt <---this ignores all txt files). Misc Commands: $ clear <---clears the content of the window to clean it up. ------------------------------- ====================================================================== TO USE: Note: Start your project or work from the local repository and don't edit on gitHub - do all editing locally to keep histories from not synching. 1) Initialize the current working folder as a repository: $ git init --this initializes the current folder. A .git folder is created in the directory (it is hidden by default) 2) set up your username and email: $ git config --global user.name 'Firstname Lastname' <--insert your name here $ git config --global user.email 'email@email.com' <--your email 3) Add the file(s) to your local repository (staging area): $ git add [filename] <---Ex: git add index.html -To check what file(s) are in the staging area, use $ git status command. -If you want to remove a file from the staging area: $ git rm --cached [filename] <----Ex: git rm --cached index.html (this removes index.html from the staging area. also: git reset filename.txt Will remove a file named filename.txt from the current index, the "about to be committed" area, without changing anything else. To undo git add . use git reset (no dot). -To add files of the same type: use *. ($ git add --cached *.html) -To add everything to the staging area: use . ($ git add --cached .) Note: if a file is modified and saved after being put in the staging area, it will be removed and you will be notified that it has been modified - you then need to re add it to the staging area. 4) COMMIT the file(s): $ git commit or $ git commit -m 'comment goes here' <---this is faster and better SHORTCUT TO STAGE AND COMMIT THE FILE(S): $ git commit -a -m 'comment goes here' ***This opens up the VIM editor by default. You need to press 'i' on the keyboard to enter insert mode to be able to type!*** Type your comment ('initial commit' for ex.), then press ESCAPE to get out of insert mode, then type in :wq and ENTER to quit. If VIM doesn't pop up then write your message and save and quit the editor. -All files will be removed from the staging area and repository after being committed. ***SHORTER WAY TO COMMIT: $ git commit -m 'comment goes here' <---use the -m option and add the comment afterwords in quote marks -- this skips the editing step with the editor. --------------------------------------------------------- GITHUB -- CREATE A REMOTE REPOSITORY: 1) CREATE A REMOTE REPOSITORY: go to github and login, then select the + menu on the top right and create a new repository. -after creation, for the project you should add a README.md file to describe the application (the extension stands for 'mark down' - and formats it a certain way). -the instructions are for steps to take in the command line initially (you may already have done some of them if you have a local repository set up). Skip to the git remote add origin (as instructed on the repository webpage): git $ git remote add origin https://github.com/BrentGrammer/sample.git instruction and copy and paste that into the command line in gitBash. 2) PUSH THE FILES TO THE REMOTE REPOSITORY IN GITBASH: input: $ git push -u origin master (as instructed on the remote repository webpage) <---this pushes the project to the master branch on the remote repository (it will ask you to log in to github first). Create a README.md with $ touch README.md in GitBash (look up the syntax of markdown online for the formatting here: https://guides.github.com/features/mastering-markdown/). re -Add the new file (in this case readme.md) to the staging area and commit it to the local repository. -Send the commit to the remite repository: $ git push Note: use $ git remote command to test if there is a remote repository connected (it will return 'origin' in the command prompt). -------------------------------------------------- TO CLONE OR DOWNLOAD THE PROJECT FROM GITHUB: -Go to the top right on the app page in github and click Clone or Download button. You get a link. -copy the link location, and in GitBash use the clone command: $ git clone [paste the link here] ex: $ git clone https://github.com/BrentGrammer/sample.git -a new folder is created and all of the contents of the project are downloaded to it. ---------------------------- IF CHANGES ARE MADE BY OTHER PEOPLE IN THE PROJECT: -use $ git pull command to update the project files with changes made in the remote repository. Ex: $ git pull ------------------------------------- BRANCHES: Used when you are assigned a specific task in a project working with other people (i.e. your job is to design a login function for the application). Your work is stored on a separate branch from the Master branch. To Create a Branch: $ git branch [nameOfBranch] (i.e. $ git branch login). Note: this just creates the branch and does not change to it. To switch to the created branch: $ git checkout [nameOfBranch] <--Ex: $ git checkout login (switches to the login branch) NOTE: All changes made to files in the branch are only visible and applicable inside that branch. If you switch to another branch (i.e. master, etc.) the files are not visible and changes made are not applied. To add the changes in a branch to the master branch (i.e. once the project and functionality is complete), use merge command: $ git merge [nameOfBranch] <---i.e. $ git merge login The files and changes in the branch will now be visible and applied to the master branch files. ----------------------------------------------- BRANCHING AND MERGING: https://www.youtube.com/watch?v=FyAAIHHClqI Terms: Branch: pointer that points to a particular commit Head: symbolic pointer points to which branch is currently checked out. Checked out: means "selected" or "active" Checkout: "select to work on" Alias command: you can create custom commands to shorten frequently used commands: synatx: $alias [cmd name] [""] ex: $alias graph="git log --all --decorate --oneline --graph" $ graph //this will create a command $ graph which will run the $git log command it's assigned to. --- commands you can use: format: $git mainCommand (optional parameters) CHECK WHICH BRANCH IS CHECKED OUT (you're on) $git log --all --decorate --oneline --graph or $git status CREATE NEW BRANCH: $git branch (name) Ex: $git branch loginstuff (created a branch named 'loginstuff'. LIST OF BRANCHES: $git branch (shows all branches and which the head is pointing to in green) CHECKOUT TO A DIFFERENT BRANCH: $git checkout [branchName] //this moves the head pointer to the specified branch Shortcut command for creating a branch: $git checkout -b [branchName] //this breaks down the two cmd process of $git branch [name] and then $git checkout [name] to one command. ----- MERGING: -- Fast-Forward Merging: Can be used when there is a direct path to the master (master is the first direct parent branch) The master branch is moved to the checked out branch for this example: SDN = [the name of the branch] s1 = [a file being edited] Commands: $git checkout master //move head to master branch; $git diff master..SDN //shows differences and what will change with merge; $git merge SDN //from the master branch merge the new branch; $cat s1 //shows changes made to file 's1' from the merge; $graph (this is an alias cmd-see above) //checks that the HEAD is pointed to the master and the merged branch (the master and SDN branch point to the same commit) -- Now that the merge is complete, the branch can be deleted if desired: Check it branches are merged: $git branch --merged //shows what branches are merged; $git branch -d SDN //deletes the branch named SDN --------------- 3 WAY MERGE: -Used when there is not a direct path to the master branch from the merging branch (the last commit of the master branch was not the commit that the branch was originally created from and copied.) commands: $git status //confirms you are on master branch $git merge auth //you can accept default comment or make one with /m '' ---merge completed now---- $git status //check the merge - it should say "merge made by the 'recursive strategy'" $graph //check the head pointer --Delete the branch-- $git branch --merged //check if it's safe to delete $git branch -d auth //deletes the branch named auth that has been merged with master ------------------- Dealing with Merge Conflicts: -Occurs when the same lines in the same files were changed in two branches that are being merged. Note: when merging branches to the master, if there is a difference in a line of code in two different commits on different branches being merged to the master, then the line of code with a change will be merged into the master over the line of code with no change. ***If there is a change on both commits being merged to master on the same line of code, then git doesn't know which change to use. commands: to abort the merge: $git merge --abort to continue with the merge: $git merge dev //tries to merge the branch with conflicting commits $gitstatus //check to show that git modified conflicting file for analysis ---- To resolve conflict: $vi [fileName] //edits the file to see where the conflicts are -- this looks like this <<<<<< HEAD //indicates head is pointing->lineofcode1 lineofcode1 =========== //this separates the two branches lineofcode2 >>>>>> dev //indicates what brach lineofcode2 is in You can now choose which version you want and then delete the ===== and HEAD and branch markers. ---- Now, add the modified file to the staging area: $git add [modifiededitedfile] $git status //shows that there are no conflicts and the edited file is in the staging area (Note: at this stage you are still in the merging process. when changes are committed, git will complete the merge commit as well) $git commit -m 'commenthere' You can now delete the merged branch $git branch -d [branchName] ------------------------ Dealing with Detached Head State: Detached Head -when the head is pointing to a specific commit and not a branch. To resolve: $git checkout master //takes the head and points it to the master branch so the head is not detached. --------------------------- Errors from not having a CLEAN STATE: Clean State: clean working tree and staging area (there are no modified files in the staging area and no staging files that are not committed) When you don't have a clean state, you can be blocked from changing branches or complicate merging. To get a clean State: $git stash //this command saves the changes made in files without staging/committing them and stores that info as a stash point and makes a clean state, so you can then go to another branch etc. $git stash list //shows list of saved stash points on the branch $git stash list -p //with this option you can see the edits in each stash point $git stash apply //this applies the most recent stash point and puts the modified files back into the work tree for staging and commit Once you are done with the stash point, you need to pop or remove it from the storage list of stashpoints: $git stash pop //applies the stash point and removes it from list To apply a specific stash from the list use: $git stash apply [label] //ex: stash@{1} $git diff //shows the file to confirm changes from stashpoint save were saved and there. --- $git stash save "commenthere" //this adds a comment to a stashpoint for easier referencing in the list --------------------------------------------- TO GET A PREVIOUS VERSION OF A FILE FROM THE LAST COMMIT: $ git checkout HEAD [filename] --this pulls the last commit version of the file to your local repository. ------ Misc commands: See newest change to file: $cat [fileName]