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 installation will focus mainly on Windows users.
1 — Download Git: https://git-scm.com/download
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 “email@example.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 “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.
- ou 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”