Skip to main content

Getting Started with Git

This article is meant to get you started with GIT fast. There won't be too much clutter as this article can be used as a reference later on. The following will focus mainly on Windows users.
  1. Download Git: https://git-scm.com/downloads
  2. Install Git leaving everything as default
  3. Open the newly installed Git Bash Shell. 
    • You'll also notice Git CMD and Git GUI have been installed.
    • Git CMD and Git Bash will essentially have the same Git commands.
    • Git GUI is a graphical way of using Git.
  4. Edit the Git author configurations
    • git config --global user.email "dinocajic@gmail.com"
    • git config --global user.name "Dino Cajic"
    • Omitting --global lets you specify different users for different repositories
  5. Create a directory on your computer. Place a file such as index.html in it if you choose to do so.
  6. Go back to your Git Bash shell and navigate to your folder using the cd command.
  7. Once you're inside your folder type git-init
    • This creates a Git repository. If you go to your directory you'll notice a .git directory has been created. It may be hidden so you may have to enable viewing hidden files and folders.
  8. Type in git status
    • You'll see that there are untracked files since git doesn't automatically track files. You'll only want to track source files.
  9. To add a file to the Git repository type git add index.html
    • You have just staged index.html. You can stage multiple files before committing them to revision Staging tells Git which files to include in the next commit.
    • You can add more than one file to the git add command.
      • i.e. git add index.html about.html
  10. To check the status again, type git status
    • You'll notice the index.html file under Changes to be committed
  11. Type git commit 
    • If you didn't make any changes to your index.html file, a message will be display letting you know that there weren't any changes.
    • A message may pop-up with something like E325: ATTENTION Found a swap file by the name ... 
      • In that case type E to Edit Anyway
    • You can also type in git commit -m "Some message goes here" to add a message without going to step 12.
    • If working on a file and you want to skip the git add command, you can type in git commit -a -m "Message" to add and commit. The -a flag can only be included on tracked files.
  12. The comment section will open up with in a vim editor.
    • Use your arrow keys to go to the last # symbol.
    • Press the insert key on your keyboard.
    • Press Enter to go down below the pound symbol (#) or delete the # symbol.
    • Enter a note such as Added news section to main page.
    • Hit the esc (Escape) key on your keyboard.
    • Type :wq and press the Enter key.
    • You'll get a message such as [master 55ad34a] Added news section to main page.
  13. To view the commitment history type git log
    • To read the messages only, type git log --oneline
    • To see changes only to one particular file, type git log index.html
Whenever you modify or add new pages you'll have to repeat steps 9 to 12 again.

To revert back to previous states, you'll have to do the following:
  1. Type git log --oneline.
    • Just provides you with a unique id and a message. You'll need the id
  2. Type git checkout 0ba2fe8
    • Replace the id with an id you want to view
    • You'll have to create a new branch to commit changes since you're technically not in a branch anymore. If you type in git status, you'll see that the HEAD is detached. 
  3. A message will appear letting you know that you're in detached HEAD state.
  4. If you go back to your directory, you'll notice that your entire directory has been reverted back to that snapshot.
  5. To return back to the current state, type git checkout master
  6. To change the id from a random to number to a version number, type git tag -a v1.0 -m "The website works and is ready for release"
    • To see all of the tags, type git tag
  7. To checkout with a tag, instead of id, type git checkout v1.0
  8. To revert back you'll type in git revert dea8855 
    • Of course you'll enter your id. This will revert back to a time right before that change was made.
    • You can enter a message or type in :q to quit with the default message
  9. You'll notice that the changes you made have been reverted back to a time before that commit.
  10. If you haven't committed the changes, but modified a tracked files, you can type in git reset --hard to revert back to the previous saved point.
    • It will not revert back new files that have not been added to the repository.
  11. If you created a new file and did not add it to the repository, and want to remove it, you'll need to type in git clean -f
Traditionally if you want to try out a new feature on your site you'll have to copy the entire directory to a new directory and do the experimentation that way. With Git, you can create a branch and work on it without affecting the master branch. If you're satisfied with the changes, you can merge the branch with the master branch or delete the branch if you're not satisfied with the results. Create branches for major changes to your project.
  1. To view an existing branch, type git branch
  2. To create a new branch, type git create mytitle (replacing mytitle with a branch name)
  3. Once you create a branch, and want to use it, type git checkout mytitle
  4. When you make a change to a file, you'll follow steps 9 through 12 in the first list
    • git add somefile.html
    • git commit -m "Modifications done to Some File"
  5. When you made a change to a file and committed that file on this new branch, you've "forked" from the master branch.
  6. To merge the changes from your experimental branch to your master branch, you'll have to return to the master branch first: git checkout master
  7. Then you'll type in git merge mytitle. This will merge the changes you made in mytitle with the master branch.
    • You may get a merge error. Easiest for me is to type git status and fix the merge conflicts if the error doesn't appear in the CONFLICT section immediately underneath your command. After you've modified the conflict, use git add and git commit to finish.
  8. After the merge, both branches have the same history. Unless you plan to do more changes, it's good practice to delete your mytitle branch. To delete the branch, type in git branch -d mytitle. If you're wanting to delete a branch that's not merged, you'll have to use a capital D flag: git branch -D mytitle.
When you rename a file you'll have to remove the previous instance of the file from the git repository and add the updated file name to git. Git knows that you just renamed a file.
  1. Go to your directory and rename the file: i.e. aboutUs.html to about.html
  2. Go to your Git Bash shell
  3. Type git rm aboutUs.html
  4. Then type in git add about.html to add the new file
  5. Finally, type git commit -m "Changed file name from aboutUs.html to about.html"

Comments

Popular posts from this blog

Beginner Java Exercise: Sentinel Values and Do-While Loops

In my previous post on while loops, we used a loop-continuation-condition to test the arguments. In this example, we'll loop at a sentinel-controlled loop. The sentinel value is a special input value that tests the condition within the while loop. To jump right to it, we'll test if an int variable is not equal to 0. The data != 0 within the while (data != 0) { ... } is the sentinel-controlled-condition. In the following example, we'll keep adding an integer to itself until the user enters 0. Once the user enters 0, the loop will break and the user will be displayed with the sum of all of the integers that he/she has entered. As you can see from the code above, the code is somewhat redundant. It asks the user to enter an integer twice: Once before the loop begins, and an x amount of times within the loop (until the user enters 0). A better approach would be through a do-while loop. In a do-while loop, you "do" something "while" the condition

Programming Language Concepts Questions/Answers Part 3

1. What is an associative array? - An unordered collection of data elements that are indexed by keys. 2. Each element of an associative array is a pair consisting of a _______ and a _______. - key and a value 3. True or False? Java supports associative arrays? - True. As a matter of fact, Perl, Python, Ruby, C++, C# and F# do too. 4. What are associative arrays called in Perl? - hashes 5. Why are associative arrays in Perl called hashes? - Because their elements are stored and retrieved with a hash function 6. What character does a hash in Perl begin with? % 7. In Perl, each key is a _____ and each value is a _______. - string - scalar 8. In Perl, subscripting is done using _______ and _______. - braces and keys 9. In Perl, how are elements removed from hashes? - using delete 10. In Perl, the ________ operator tests whether a particular value is a key in a hash. - exists 11. What are associative arrays called in Python? - dictionaries 12. What is a dif

Creating your own ArrayList in Java

Wanted to show that certain data structures in Java can be created by you. In this example, we'll go ahead and create an ArrayList data structure that has some of the methods that the built in ArrayList class has. We'll create 2 constructors: The default constructor that creates an ArrayList with a default size of 10. Constructor that allows an initial size to be passed to the array. We'll also create a number of methods: void add(Object x);  A method that allows you to place an Object at the end of the ArrayList. void add(int index, Object x);  A method that allows you to place a value at a given location. Object get(int index):  Allows you to retrieve a value of the arrayList array from a given location. int size();  Allows you to get the number of elements currently in the Arraylist. boolean isEmpty();  Tests to see if the Arraylist is empty. boolean isIn(Object x);  A method that sees if a particular object exist in the arrayList. int find(Object x);