Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Learning Objectives:

  • Use an advanced understanding of Git

  • Skip the staging area to delete and move files within Git

  • Amend and roll back commits

  • Explain the concept of branching and merging

  • Create new branches

  • Use merging to combine branched data

  • Manage and handle merge conflicts

...

Table of Contents:

Table of Contents

...

Advanced Git Interaction

Skipping the Staging Area

If we already know that the current changes are the ones that we want to commit, we can skip the staging step and go directly to the commit. We do this by using the -a flag to the git commit command.

...

This flag automatically stages every file that's tracked and modified before doing the commit letting it skip the git add step.

...

This is useful, but if we need to look at the actual lines that changed in each commit, we can do this with git log -p. The p comes from patch, because using this flag gives us the patch that was created.

If we don't want to scroll down until we find the commit that we're actually interested in, another option is to use the git show command. This command takes a commit ID as a parameter, and will display the information about the commit and the associated patch.

...

Another interesting flag for git log is the --stat flag. This will cause git log to show some stats about the changes in the commit, like which files were changed and how many lines were added or removed.

...

To help us keep track git gives us the git diff command. This format is equivalent to the diff -u output that we saw in an earlier video.

...

Something else we can do to review changes before adding them is to use the -p flag with the git add command. When we use this flag, git will show us the change being added and ask us if we want to stage it or not.

...

git diff shows only unstaged changes by default. Instead, we can call git diff -- staged to see the changes that are staged but not committed.

...

You can remove files from your repository with the git rm command, which will stop the file from being tracked by git and remove it from the git directory.

...

What if you have a file that isn't accurately named?

You can use the git mv command to rename files in the repository.

...

To do this, we can use the .gitignore file. Inside this file, we'll specify rules to tell git which files to skip for the current repo. To do this, we'll create a .gitignore file containing the name of this file.

Remember that the dot prefix in a Unix-like file system indicates that the file or directory is hidden and won't show up when you do the normal directory listing. That's why we have to use ls -la to see all files.

We've added a .gitignore file to our repo but we haven't committed it yet. This file needs to get tracked just like the rest of the files in the repo.

...

You can change a file back to its earlier committed state by using the git checkout command followed by the name of the file you want to revert.

...

With that, we've demonstrated how we can use git checkout to revert changes to modify files before they get staged. This command will restore the file to the latest storage snapshot, which can be either committed or staged.

If you need to check out individual changes instead of the whole file, you can do that using the - p flag. This will ask you change by change if you want to go back to the previous snapshot or not.

...

We can unstage our changes by using the git reset command. Staging changes that we don't actually intend to commit happens all the time. Especially if we use a command like git add star, where the star is a file glob pattern used in Bash that expands to all files. This command will end up adding any change done in the working tree to the staging area.

...

The example output mentions the head alias, the current checked out snapshot. So by running the suggested command, we're resetting our changes to whatever's in the current snapshot.

You can use git reset - p to get git to ask you which specific changes you want to reset.

...

We can solve problems like these using the --amend option of the git commit command. When we run git commit --amend, git will take whatever is currently in our staging area and run the git commit workflow to overwrite the previous commit.

...

You could also just update the message of the previous commit by running the git commit --amend command with no changes in the staging area.

...

We can revert the latest commit by using the head alias that we mentioned before. Since we can think of head as a pointer to the snapshot of your current commit, when we pass head to the revert command we tell Git to rewind that current commit.

...

So once we issue that git revert HEAD command, we're presented with the text editor commit interface that we've all seen before. In this case, we can see that git has automatically added some text to the command indicating it's a rollback. The first-line mentions that it's reverting the commit we just did called “Add call to disk full function”. The extra description even includes the identifier of the commit that got reverted.

...

Let's look at the last two entries in the log using -p and -2 as parameters.

...

As demonstrated before, the -p parameter lets us see the patch created by the commit while the -2 perimeter limits the output to the last two entries.

...

Remember our discussion about fixing commits with the --amend command? Each time we amend a commit, the commit ID will change. This is why it's important not to use dash dash amend on commits that have been made public.

...

Now, once we save and exit the commit message, Git will actually perform the rollback and generate a new commit with its own ID.

Git Revert Cheat Sheet

Branching and Merging

What is a branch?

  • git checkout is effectively used to switch branches.

  • git reset basically resets the repo, throwing away some changes. It’s somewhat difficult to understand, so reading the examples in the documentation may be a bit more useful.

  • There are some other useful articles online, which discuss more aggressive approaches to resetting the repo.

  • git commit --amend is used to make changes to commits after-the-fact, which can be useful for making notes about a given commit.

  • git revert makes a new commit which effectively rolls back a previous commit. It’s a bit like an undo command.

There are a few ways you can rollback commits in Git.

There are some interesting considerations about how git object data is stored, such as the usage of sha-1.

Feel free to read more here:

Branching and Merging

What is a branch?

Creating New Branches

Working with Branches

...

Git Branches and Merging Cheat Sheet

Module 2 Review

Module 2 Wrap Up: Using Git Locally