Part 2
Working with Git

Now that you have Git and your repository set up, it’s time to start learning how to interact with Git. A handful of commands are all you need to get you through most tasks. Once you finish the tasks in this part, you’ll know them all.

As we saw in the introduction, the workflow in Git is different from other version control systems and definitely different from working without any version control system. Each time you make a change you want to track, you need to commit it.

The workflow goes like this. First, create your repository—either create a new repository or clone an existing one. Then make some changes, test that they do what you want, commit those changes, make some more changes, and so on. Finally, you share those changes when they’re ready.

One thing to keep in mind when working with a distributed version control system (DVCS) like Git is that committing a change and sharing that change are two different processes. This is different from centralized VCS such as Subversion and CVS, where the two actions are synonymous.

This separation provides you with a lot of freedom. You can experiment locally, try a whole bunch of things, and then share the best solution, but to paraphrase an old uncle, “With great freedom comes great responsibility.”

Lots of small, discrete changes that touch very specific portions of the code are better than a few monolithic changes. Make sure you don’t sit on a whole bunch of changes until they’re perfect. First, they’ll never be perfect. There’s always something else to refactor and abstract away. Second, the bigger the change becomes, the harder it becomes to fully understand, review, and test.

Third, it makes tracking down bugs later easier. Tools such as git bisect (see Task 39, Finding Bugs with bisect) make finding which commit introduced a bug easy. Smaller commits mean that once you know which commit caused the bug, you can figure out the exact change that much faster.

We’ve already covered how to create a new repository or clone an existing one (git init and git clone in Task 3, Creating a New Repository and Task 4, Creating a Local Copy of an Existing Repository, respectively). Making changes and testing are up to you and how you interact with the code in your project. Seeing what changes need to be committed is where we pick up. The tasks in this part are ordered roughly the same way you’ll use them in Git.

Covered in this part:

  • The first thing is seeing what has changed. We cover this in Task 5, Seeing What Has Changed, which shows you how to compare your working tree with what the repository knows about.

  • After you know what has changed, then you need to stage the changes you want to commit. This is covered in Task 6, Staging Changes to Commit.

  • The changes are staged; now it’s time to commit them. Task 7, Committing Changes shows you how to create a commit and add a log message to it.

  • With any project, files will be generated that you don’t need to commit. Task 8, Ignoring Files teaches you how to tell Git to ignore those files.

  • What happens when you accidentally stage a file you didn’t mean to or you decide that you want to get rid of a change that you made to a file before committing it? Task 9, Undoing Uncommitted Changes covers how to undo those staged changes so you don’t accidentally commit something.

  • Files sometimes need to change where they live. A new project layout is adopted, or files or directories are renamed. Task 10, Moving Files in Git shows you how to handle these inevitable changes.

  • Likewise, some files or directories outlive their usefulness. Since the repository keeps a record of all files that it has ever tracked, you can delete those old files without worrying about not having them to reference later if you need to do so. Task 11, Deleting Files in Git shows you how.

  • Finally, Task 12, Sharing Changes is a whirlwind tour of how to share changes with other developers. It’s done at 30,000 feet and is enough to get you started. A lot more about collaboration is covered in Part IV, Working with a Team.

Now, let’s dive into the specifics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.120.57