You Don't Know Git!
I’m going to put it out there. This blog is not for senior developers or git gurus; I know you know git. This post is for the noobs, the career-changers like me. If I could go back in time, after I had graduated from my three month web development bootcamp, I would tell myself, “You don’t know git!”
I can hear myself saying, “But I know the workflow. I know to pull from master before starting a new branch. I know to avoid merge conflicts. I git add -A. I know what’s up.”
No. No. No.
Fix Your Workflow
If there’s one command you want to know, it’s this:
<code> git add -p </code>
This command changed my entire workflow and was tremendously helpful. In bootcamp, you learn the basics of git and move on. You generally learn:
<code>git add -A</code>
or
<code>git add .</code>
This stages all the changes that you’ve made to the repository or all the changes you’ve made from your current directory. This worked during bootcamp because the changes were small, and I was often just committing to my own repository. Once I switched over to Drupal and started working with Features, I realized that after I would make updates, not all of the files that were showing up in code were things I had changed. How could that be?!
Work with Your Team
I was working on a project with other developers who were also working on the same feature. I had to learn -p so that I could be a responsible member of the team and only commit what I had changed. That’s why it’s so important to use this command:
<code>git add -p</code>
If you’re ever unsure about a command in git, just type this command:
<code>git add --help</code>
The git manual will then show you all the options you can use, like this:
-p, --patch
Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.
Essentially, it allows you to review each file to determine what changed, and if you want to stage it or not.
In the example above, I made changes to my .gitignore file. I deleted the line in red, and added the line in green. Then it asks you what you want to do with those changes. If you type in ‘?’ and push enter, it will explain what your options are.
Not only does it help by preventing you from staging code that isn’t yours, it’s also helpful as a new developer to see what changed. In Drupal, you can think that you’re making a small change in the UI, but then see a ton of altered files. Using -p has helped me figure out how Drupal works, and I’m a lot more confident about what I’m staging now.
Now go out there and <code>git add -p</code> all of your changes and be the granular committer I know you can be!