Wednesday 13 March 2024

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


Undoing Changes & Time Traveling

Checking Out Old Commits

To review or restore your project at a specific point in history:

git checkout <commit-hash>

This moves HEAD to point to the specified commit. Remember, this puts your repository in a “detached HEAD” state, where you’re no longer on a branch.

Re-Attaching Our Detached HEAD!

If you find yourself in a detached HEAD state and want to return to the latest commit on your current branch:

git switch -

Or, if you wish to keep changes from the detached HEAD state, create a new branch from it:

git switch -c new-branch-name

Referencing Commits Relative to HEAD

You can reference commits relative to HEAD using ~ for previous commits. For example, to see the commit before the current HEAD:

git checkout HEAD~1

Discarding Changes With Git Checkout

To discard changes in a specific file back to the last commit:

git checkout -- <file>

This command is handy for quickly undoing modifications that you don’t wish to keep.

Un-Modifying With Git Restore

Introduced in Git 2.23, git restore provides a more straightforward way to undo changes:

  • To restore a file to its state at the last commit:
    git restore <file>
    
  • To restore a file to how it was at a specific commit:
    git restore --source=<commit-hash> <file>
    

Un-Staging Changes With Git Restore

Similarly, if you’ve staged changes that you decide not to commit, you can unstage them:

git restore --staged <file>

Undoing Commits With Git Reset

To undo commits, git reset moves the current branch backward to a previous commit, effectively erasing the subsequent commits:

  • To undo the last commit but keep the changes in the working directory:
    git reset --soft HEAD~1
    
  • To completely remove the last commit and all changes:
    git reset --hard HEAD~1
    

Reverting Commits With…Git Revert

Rather than erasing commits, git revert creates a new commit that undoes the changes of a previous commit:

git revert <commit-hash>

This is safer for changes that have been shared with others.

Undoing Changes Exercise

Let’s practice with some safe undoing techniques:

  1. Make a small change in README.md and commit it.
  2. Undo the commit with git revert HEAD.
  3. Make another change in README.md, stage it but then decide to unstage it using git restore --staged README.md.
  4. Modify README.md again, but then use git restore README.md to discard the changes.

Through these exercises, you’ve learned various ways to undo changes and navigate your project’s history, giving you flexibility in managing your development workflow and correcting mistakes.

Continuing with “SampleProject,” let’s explore GitHub basics. GitHub extends Git’s capabilities by providing a cloud-based platform for hosting Git repositories, enhancing collaboration, and simplifying the management and sharing of code.

Github: The Basics

What Does Github Do For Us?

GitHub hosts your Git repositories in the cloud, making it easier to collaborate with others. It provides tools for issue tracking, pull requests, code review, and more, facilitating open-source projects and team-based workflows.

Why You Should Use Github!

  • Collaboration: Share your work with others, contribute to other projects, and collaborate on code.
  • Version Control: Keep a backup of your work and access it from anywhere.
  • Portfolio: Showcase your projects and contributions to potential employers or collaborators.

Cloning Github Repos With Git Clone

To copy a GitHub repository to your local machine:

git clone <repository-url>

This creates a local copy of the repository for you to work on.

Cloning Non-Github Repos

The git clone command works with any Git repository, not just those on GitHub:

git clone <repo-url>

Replace <repo-url> with the URL of the repository you want to clone.

Github Setup: SSH Config

Setting up SSH keys for GitHub allows you to push and pull without entering your username and password each time.

  • Generate a new SSH key (if you don’t have one) and add it to your GitHub account.
  • For detailed instructions, visit GitHub’s documentation on SSH key generation and adding it to your account.

Creating Our First Github Repo!

  1. On GitHub, click the “New repository” button.
  2. Fill out the details for your repository and click “Create repository.”
  3. Follow the instructions to push an existing repository or create a new one.

A Crash Course on Git Remotes

A remote in Git is a common repository that all team members use to exchange their changes. The default remote for most projects is called “origin,” which typically points to a repository on GitHub or another online host.

  • To add a remote: git remote add origin <repo-url>
  • To view remotes: git remote -v

Introducing Git Push

To upload your local repository changes to a GitHub repository:

git push origin main

This command pushes your commits to the “main” branch of the “origin” remote.

Touring A Github Repo

Explore the GitHub repository interface to find:

  • Code files and directories.
  • Commit history.
  • Branches.
  • Issues and pull requests for tracking tasks and discussing changes.

Practice With Git Push

Make a change in your local repository, commit it, and push it to GitHub:

  1. Edit a file, then stage and commit the change.
  2. Push the changes: git push origin main

A Closer Look At Git Push

  • git push uploads your local branch commits to the remote repository.
  • If the branch doesn’t exist on the remote, it will be created.

What does “git push -u” mean?

git push -u origin main sets the upstream (tracking) reference for the current branch to “main” on “origin.” This allows you to use git push and git pull without specifying the remote and branch in the future.

Another Github Workflow: Cloning First

Instead of creating a local repository and then pushing it to GitHub, you can start by cloning an existing GitHub repository. This is common when contributing to other projects.

Main & Master: Github Default Branches

New GitHub repositories now use “main” as the default branch name. This change reflects an industry shift to use more inclusive language. Older repositories might still use “master.”

Github Basics Exercise

  1. Create a new GitHub repository for “SampleProject.”
  2. Add a remote to your local repository: git remote add origin <repository-url>
  3. Push your project to GitHub: git push -u origin main
  4. Make a change locally, commit it, and push it again to practice.

Through these exercises, you’ve gained foundational knowledge of GitHub, learning how to host your projects, collaborate with others, and utilize Git more effectively in your development workflow.

Continuing with our “SampleProject” and its GitHub counterpart, let’s delve into the concepts of fetching and pulling from remote repositories. These operations are crucial for collaborating on projects and keeping your local repository up to date with changes made by others.

Fetching and Pulling

Remote Tracking Branches: WTF Are They?

Remote tracking branches are references to the state of branches in your remote repositories. They help you understand the differences between your local branches and those on the remote, letting you track progress and changes.

Checking Out Remote Tracking Branches

You can check out a remote tracking branch to inspect it or start a new branch from it:

git checkout -b new-local-branch origin/remote-branch-name

This creates a new local branch (new-local-branch) that tracks the remote branch (remote-branch-name).

Working With Remote Branches

To update your list of remote branches and fetch their latest changes without merging:

git fetch origin

This command updates your remote tracking branches (origin/remote-branch-name).

Git Fetch: The Basics

git fetch downloads the latest changes from the remote but doesn’t merge them into your local branches. It’s a safe way to see what others have done without integrating those changes into your local work.

Demonstrating Git Fetch

After running git fetch origin, you can compare a remote branch with your local branch:

git diff main origin/main

This shows the differences between your local main branch and the main branch on the remote named origin.

Git Pull: The Basics

git pull is essentially a combination of git fetch followed by git merge. It fetches changes from the remote branch and immediately tries to merge them into your current branch:

git pull origin main

This pulls changes from the main branch of the remote origin into your current local branch.

Git Pull & Merge Conflicts

If git pull tries to merge changes that conflict with your local changes, you’ll encounter a merge conflict. You must manually resolve these conflicts before you can continue. Tools like VSCode can help visualize and resolve these conflicts easily.

A Shorter Syntax For Git Pull?

If your current branch is set up to track a remote branch, you can simply use:

git pull

This will fetch and merge changes from the tracked remote branch into your current branch without specifying the remote and branch names.

Exercise with “SampleProject”

  1. Fetch changes: Assume other contributions have been made to the “SampleProject” on GitHub. First, fetch these changes without merging:
    git fetch origin
    
  2. Compare changes: Compare what was fetched to your local main branch:
    git diff main origin/main
    
  3. Pull changes: Now, pull the changes to integrate them with your local main branch:
    git pull origin main
    
  4. Resolve conflicts (if any): If there are merge conflicts, resolve them using your preferred text editor or a GUI tool.

Through this process, you’ve learned how to safely update your local repository with changes from a remote, how to inspect these updates before integrating them, and how to handle potential conflicts that may arise during the process.

Labels:

0 Comments:

Post a Comment

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

<< Home