Don's Pages and my Music

Monday, June 7, 2010

10 Git Tips and Tricks for Beginners — Developer.com

10 Git Tips and Tricks for Beginners

  • June 4, 2010
  • By Jason Gilmore
  • Send Email »
  • More Articles »

Emerging technologies tend to be the catalyst for moving certain development paradigms and best practices into the mainstream. Call it the "Can't Buy Me Love" phenomenon, named for the 80s movie in which a nerd boosts his popularity by hiring his high school crush to pose as his girlfriend. The latest example seems to be the emergence of Git, the open source version control system, greatly raising the profile of formalized source code management. I've used other solutions such as the CVS and Subversion version control systems for years, but Git makes source code management a much more natural part of my workflow -- even almost fun.

But like many technologies, Git's shallow learning curve encourages adoption, yet it offers so many features and options that it can easily overwhelm beginners. As I've become more experienced with the system, I've maintained a list of tips and tricks that have helped me better manage my Git projects. In this article, I'll highlight the ones I think will be of most benefit to you newbies.

1. Simultaneously Add Files When Committing

Git requires you to explicitly confirm your request to add newly tracked files to the repository before committing the latest round of changes. Therefore the typical command sequence when committing changes goes like this:

%>git add . %>git commit -m "Latest commit message" 


Save yourself the preliminary step and perform the task of both adding and committing files simultaneously using the -a flag:

%>git commit -a -m "Latest commit message" 


In many cases, however, you should not take this shortcut. Later in this article I'll provide you with at least one example demonstrating why.

2. Save Keystrokes with Git Aliases

Like many popular command-line utilities, Git allows you to save user preferences within a configuration file named .gitconfig. Within this file you'll typically define your name and e-mail address as it relates to your repository interactions, but you can also define timesaving aliases here. For instance, my .gitconfig file contains a few aliases to commonly used commands:

[alias] st = status co = checkout cm = commit pom = push origin master


If you happen to forget your defined aliases, you can quickly review your configuration file settings using the following command:

%>git config -l 


3. Selectively Staging Files

Sometimes you might work on several files simultaneously but wish to add only a select few to the upcoming commit round. To do so you can use the interactive addition feature. For instance, suppose I've created two new files, ShopController.php and ForumController.php, but want to commit only the former. I can fire up the interactive adder by passing the -i option to git add:

%>git add -i 


From there you'll be greeted with a menu that provides you with several options:

*** Commands *** 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked  5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp 


By selecting 4, you can interactively choose which files you'd like to add:

What now> 4 1: application/controllers/ForumController.php  2: application/controllers/ShopController.php Add untracked>> 


4. Ignoring Files and Directories with .gitignore

The very first thing I do after initializing a new Git repository is create a .gitignore file. The .gitignore file is used to filter out any files and directories that you don't want to be tracked within your Git repository. For instance when working on a new Zend Framework project I typically omit the project documentation, the website images, and a file named notes.txt from the repository, meaning my .gitignore file looks like this:

docs public/images notes.txt 


5. Removing Newly Added Files from the Commit List

In the heat of development you occasionally forget to add newly created files that you don't want included in the repository to your .gitignore file. You can remove these files from the list of changes to be committed (known as unstaging the file) using the rm command:

%>git rm --cached schema-notes.txt


When unstaged, you can add the file to your .gitignore file and begin the commit process anew.


Tags: source code, git, version control

Go there and read Pg2...
http://www.developer.com/features/article.php/3886146/10-Git-Tips-and-Tricks-for-Beginners.htm

Don

No comments:

Post a Comment