Pick up basic Git commands with a hands-on tutorial
To get things done in Git, know your commands. Open up the version control system and replicate this quick tutorial to learn how to run crucial inputs.
Version control attracts users ranging from coders to administrators and even project managers. It provides a means to track and manage complex application code and configuration files with a path to backtrack if any change causes problems.
But to get the benefits of version control, chances are you'll need to learn Git.
Git handles all of these tasks to manage application and deployment code via basic commands that are the same across all operating systems.
To grasp the likes of git init, git log and other essential commands, download and install Git locally, and follow along with this simple Git tutorial.
Author's note: It's OK to go with the suggested defaults within the Git installer for this demonstration.
Basic Git commands for start up
Once installed, Git requires a name and email address for configuration. Git configures these items with the git config command. Start a command prompt, or terminal window, to run the Git commands.
Fill in these two key entries to get started with Git:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
Then, create a repository locally. Git calls this action initializing, which is why the command is git init. Create a folder in which to store the Git repository, with a name like coderepo. Drop to a command prompt, navigate to the folder and use the git init command to initialize a Git managed folder. In addition to the folder, this command also creates an array of hidden files that enable Git to track changes.
At this point, we can start to do useful things with Git. Using your text editor of choice, create a file, sample.txt. Input the word "one." To add this file to Git, use the following command:
git add sample.txt
These basic Git commands merely trigger the version control system to start working and to track files, but do not create versions. Next, work with point-in-time copies of files.
Commits and differences
Once you create a test file, issue a commit command, which is essentially a point-in-time copy of all the files under version control. We can revert back to any of these copies as needed. Issue this command, with descriptive comment:
git commit -m "This is my first commit"
The author of a commit provides a description with the -m message option, giving a high-level description of what the commit changes. The Git commit message should read clearly to anyone who might need to work with the file.
Here are the seven rules that encapsulate what goes into a well-written Git commit. Follow these practices to make your repository's log coherent.
Here's an example of commit output:
Add more files at any time with the git add command, and try wildcards to work with several files at once. The commit command tells Git to snapshot all files submitted to version control in a given folder, as well as the changes it has tracked.
To create a new file version, change the text of the file to read "Two," save it and add the file to the Git repo, then commit the file with the following:
git commit -am "This is a revised commit"
In this case, we combine the -a option, which adds the file to Git, with the -m option. This saves you a line of commands to re-add the file.
Use the git log command to see a list of commits, as shown here:
In this example, I made a change and submitted a third commit. Then, I used the git log -f command -- the -f option brings up the files involved.
As you can see, this command displays the additions and removals to sample.txt. All lines starting with + are additions or changes between the versions, and - lines are removals.
The commands and workflow examined in this simple exercise demonstrate the fundamental use of Git -- i.e., its role as a versioning system to manage basic local changes. It is essential to understand and be familiar with the basics before moving on to more advanced Git concepts. Spend the time to fully comprehend the basic Git commands covered in this tutorial, and how to use them, before you cast aside the command-line interface and explore the many GUIs available for Git, as well as commercial version control tools based on Git.
Murky on the basics of version control? Check out this cheat sheet