Wednesday 13 March 2024

The Git & Github Bootcamp Part1 - Master on essentials and the tricky bits: rebasing, squashing, stashing, reflogs, blobs, trees, & more!


Installation & Setup

1. Installing Git: Terminal Vs. GUIs

  • Terminal: A text-based interface to run commands directly on your computer.
  • GUIs (Graphical User Interfaces): Software applications with graphical elements, like buttons and icons, making it easier to perform Git operations without memorizing commands.

2. WINDOWS Git Installation

  • Step 1: Visit git-scm.com and download the Windows version of Git.
  • Step 2: Open the downloaded file and follow the installation instructions. Leave the default settings unless you have a specific need to change them.

3. MAC Git Installation

  • Step 1: Open the Terminal.
  • Step 2: Type git --version and press Enter. If Git is not already installed, this will prompt you to install it.
  • Alternatively, you can use Homebrew by typing brew install git if you have Homebrew installed.

4. Configuring Your Git Name & Email

Before you start using Git, you need to introduce yourself to Git by configuring your user name and email address. This information is important because every Git commit uses this information.

  • Command:
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    

5. Installing GitKraken (Our GUI)

GitKraken is a graphical Git client that makes Git commands more user-friendly.

  • Step 1: Go to gitkraken.com and download GitKraken.
  • Step 2: Run the installer and follow the setup instructions.

6. Terminal Crash Course: Creating Files & Folders

Creating files and folders from the terminal is a basic but essential skill.

  • Creating a Folder: Use the mkdir command followed by the name of the folder. For example, mkdir MyProject creates a new folder named MyProject.
  • Creating a File: Use the touch command followed by the filename. For example, touch README.md creates a new file named README.md.

7. Terminal Crash Course: Deleting Files & Folders

Sometimes, you need to clean up or remove unnecessary files and folders.

  • Deleting a File: Use the rm command followed by the filename. For example, rm README.md deletes the README.md file.
  • Deleting a Folder: Use the rm -r command followed by the folder name to remove a folder and its contents. For example, rm -r MyProject deletes the MyProject folder and everything inside it.

Putting It All Together: Creating a New Sample Project

Now that you have Git and GitKraken installed, and you know how to manage files and folders from the terminal, let’s start a new sample project.

  1. Open the Terminal or Git Bash on Windows.
  2. Navigate to where you want to create your project using the cd command (e.g., cd Documents).
  3. Create a new folder for your project (e.g., mkdir SampleProject).
  4. Navigate into your project folder (e.g., cd SampleProject).
  5. Initialize a new Git repository:
    • Command: git init
    • This command creates a new Git repository in your project folder. You’ll see a message like “Initialized empty Git repository in [your project path]/.git/”.
  6. Create your first file (e.g., touch README.md) and open it with a text editor to add some content, such as “This is my first Git project!”.
  7. Add your file to the staging area with Git:
    • Command: git add README.md
    • This command tells Git to start tracking changes to the README.md file.
  8. Commit your changes:
    • Command: git commit -m "Initial commit"
    • This command saves your changes to the repository with a message describing what you did.

Very Basics of Git: Adding & Committing

10. What Is A Git Repo?

A Git repository (repo) is a folder on your computer where Git tracks the changes to your project files. It allows you to save different versions of your project, so you can recall specific versions later. The repository was initialized in your project folder when you ran git init.

11. Our First Commands: git init and git status

  • git init: This command was used to initialize your project folder as a Git repository. It allows Git to start tracking changes in the folder.
  • git status: Use this command to see which changes Git has noticed but not yet recorded. Let’s try it:
    cd SampleProject
    git status
    
    This command will show the status of your repository, including any files that have been added, modified, or are untracked.

12. The Mysterious .git Folder

When you initialize a Git repository, Git creates a hidden folder named .git in your project directory. This folder contains all the information necessary for version control, including logs, configurations, and the status of each file. You typically won’t need to interact with this folder directly.

13. A Common Early Git Mistake

A frequent mistake beginners make is forgetting to stage changes before committing them. Git requires you to “add” changes to the staging area before you can “commit” them to your repository’s history. This two-step process gives you control over exactly what changes you include in a commit.

14. Staging Changes With git add

  • Example: Suppose you’ve edited the README.md file to add more project details. To prepare these changes for a commit, you use the git add command.
    git add README.md
    
  • This command moves the changes in README.md to the staging area, making them ready to be committed.

15. The git log Command (And More Committing)

  • git log: Shows a history of all commits in the repository. Each commit is displayed with its unique ID, the author’s name and email, the date, and the commit message.
    git log
    
  • After staging your changes with git add, commit them with a message describing what you did:
    git commit -m "Updated README with more project details."
    

16. Committing Exercise

Let’s practice adding and committing with a new file:

  1. Create a new file: touch notes.txt
  2. Add some content to notes.txt (use a text editor).
  3. Stage the file: git add notes.txt
  4. Commit the changes with a message: git commit -m "Added notes.txt with project notes."
  5. Check the log: git log

By performing these steps, you’ve practiced adding a new file to your repository, staging it, and committing it with a descriptive message. Remember, git status is a helpful command to use throughout this process to see what changes are staged, unstaged, or untracked.

Expanding on our example project, “SampleProject,” let’s explore commits in detail, focusing on best practices, tools, and techniques to manage your project efficiently.

Commits in Detail

1. Commit Messages: Present Or Past Tense?

Commit messages should be written in the imperative mood, as if giving a command or instruction. This convention matches the messages generated by Git itself for automated commits.

  • Example: Instead of writing “Added feature X,” write “Add feature X.”

2. Escaping VIM & Configuring Git’s Default Editor

When you commit without specifying a message directly in the command line (git commit without -m "message"), Git opens the default text editor (often VIM) to write a commit message. Exiting VIM can be confusing for new users:

  • To exit VIM after adding your commit message, press Esc, type :wq (write and quit), and press Enter.
  • To change the default editor to something you’re more comfortable with (like Nano, which is simpler), run:
    git config --global core.editor "nano"
    
    Now, when you run git commit, Nano will open instead of VIM.

3. A Closer Look At The Git Log Command

git log provides a history of commits. To see more than just the commit hash, author, date, and message, you can use:

  • git log --stat: Shows the number of changes (additions and deletions) per file.
  • git log --pretty=oneline: Shows each commit on a single line, making it easier to read through many commits.
  • git log --graph: Displays a text-based graph of the commit history, useful for visualizing branch merges.

4. Committing With A GUI

Git GUI clients like GitKraken can make committing changes more intuitive for those uncomfortable with the command line. Using our project as an example:

  • Open GitKraken and navigate to your “SampleProject” repository.
  • You’ll see uncommitted changes in a panel or section often labeled “Unstaged Files.”
  • Drag the files you want to commit from “Unstaged” to “Staged.”
  • Write your commit message in the provided field and click the “Commit” button.

5. Fixing Mistakes With Amend

If you make a mistake in your last commit (e.g., forgot to add a file or made a typo in the commit message), you can correct it with git commit --amend. This combines your changes with the previous commit instead of creating a new one.

  • Make the necessary changes or add any missed files.
  • Stage the changes: git add .
  • Amend the commit: git commit --amend -m "New commit message"

6. Ignoring Files w/ .gitignore

Sometimes, there are files or directories you don’t want Git to track (e.g., temporary files, build folders). You can create a .gitignore file in your repository’s root directory and list the patterns for files to ignore.

  • Create a .gitignore file: touch .gitignore
  • Open .gitignore in a text editor and add patterns for files to ignore. For example:
    # Ignore all log files
    *.log
    
    # Ignore the node_modules directory
    node_modules/
    
  • Add and commit the .gitignore file:
    git add .gitignore
    git commit -m "Add .gitignore file"
    

By understanding these detailed aspects of committing in Git, you can maintain a clean, efficient workflow for your projects.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home