Source: Restful Brilliance Blog

Restful Brilliance Blog Git 101 (Tutorial)

This document provides an overview of git and its most useful commands. Git can appear daunting to beginners, and many people prefer to use GUIs to handle their git interactions. These tools and GUIs can be helpful for beginners, but you'll find git more useful and powerful once you understand the underlying commands.Learning git via the command line is actually much simpler than most people believe. Hopefully this post will help you on your journey to becoming a git commandline guru!Repository CreationThere are two main ways to create new git repositories:New local repository on the file system.Cloning an existing remote repository which is hosted elsewhere (e.g. Github).Creating a Local Repository (on your filesystem)Even if you are only planning on building software locally, it's a great idea to use git. This will let you keep track of your changes, roll-back if neccessary, and manage your code across branches.To create a local git repository, simply go to the directory that will become your repository and initialize it. Don't worry about initializing a new git repository in a directory that already contains files, it won't modify any existing files.cd ~/projects/my-repo git initNow you can continue to create / modify source code in that directory. A best practice is to get your initial commit in place as quickly as possible. When you're ready, you'll want to stage your source code for the initial commit. To stage file(s) for a commit use the git add command. You can stage an individual file or directory, or stage all files and directories from your current working directory.To stage a single file use: git add {filename}'. Example: git add my-file.htmlTo stage a directory and all files inside that directory use: git add {dir}. Example: git add ./my-directoryTo stage all files and directories that are inside your current working directory use: git add .In pracite the git add . command is the most commonly used. It's intelligent enough to only stage files that have changed since the last commit, and it won't stage a file more than once. If you haven't made an initial commit, this means that all files (with some exceptions like the .git directory) will be staged.Here's where many people get confused. Staging files does not create a commit into the local repository. Instead it's getting files ready for a commit. This allows you to pick and choose what files will be included within the commit. Go ahead and stage all the files in your local repository.git add .You can check the files that are staged for a commit by using the git status command.git statusWhen you're ready, commit the files that are staged using the git commit command. It's a good pratice to add a message for the commit by using the -m flag.git commit -m "my first commit, like a boss yo!"You might be wondering what the point of staging files is? It's useful if you want to break up your changes into two or more commits. For example, let's say you haven't committed in a while, and you've made lots of changes. You would have preferred to manke more granual commits, but it's too late for that. With git it's no problem. You just stage the files you want in your first commit, then commit. Then stage the next set of files for your second commit, then commit, etc.Now you know the basics of staging and commit files to a local repository.Cloning an Existing Repository from a Remote System (e.g. Github)If you've already created a repository somewhere else (e.g. Github) or you're jumping on to work with somebody else that already has a repository setup, you'll need to clone a repository.The default branch for repositories is called master. Unless your team tells you otherwise, you should clone this branch to get started.To clone a repository's master branch, use the git clone command. Make sure you're located in the directory in which you want to build a new directory for the repository.git clone {url to remote repository}For example if you're in ~/projects and run:git clone https://github.com/rstrube/myproject.gitGit will clone the repository to: ~/projects/myproject.git.You can also add an additional parameter which determines which directory the repository should be cloned into.For example if you're in ~/projects and run:git clone https://github.com/rstrube/myproject.git super_duper_special_dirGit will clone the repository to ~/projects/super_duper_special_dir.To clone a specific branch of a repository use the -b {branch} flag.For example if you're in ~/projects and run:git clone -b prototype https://github.com/rstrube/rstrube.github.io myproj_prototypeGit will clone the prototype branch to ~/projects/myproj_prototype.Getting the Status of your ChangesAs we mentioned earlier, the git status can be used to get the statuses of the files in your local repository. To elaborate, files can exist in one of several categories:Staged - these are files that will be included in the next commit to your local repository.Unstaged - these are files that will not be included in the next commit.Untracked - these are files that are ignored and will not be included in the next commit.Committed - these are files that have been committed to the local repository, but still need to be pushed to the remote repository(ies).To view all of your changes you can use the status command.git statusYou'll see something like:# On branch master # Changes to be committed: # (use "git reset HEAD ..." to unstage) # #modified: ModifiedClass.cs # # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # #modified: NewClass.cs # # Untracked files: # (use "git add ..." to include in what will be committed) # #Untracked.configStaging FilesWe talked about staging files eariler, but let's go into more detail.To stage files, you must use the git add command.git add {directory}Will stage all modified files in the specified directory and all sub-directories within that directory.Thereforegit add .Will stage all modified files in the current working directory and sub-directories of the current working directory.git add {filename}Will stage a specific file. This can also be important when staging a brand new file.As mentioned eariler, it's common to break up lots of changes across several commits. To do this, stage the specific files you wish to to include in the first commit, then commit. Then repeat the process with the files to be included in the second commit, and so on.Committing FilesWe talked about committing files eariler, but let's go into more detail.It's important to remember that committing really means committing to your local repository. To push your changes to your remote repository (e.g. Github) you need to execute a push.To commit all staged files to your local git repository use:git commitThis will open up a text editor and ask you for a commit message.Sometimes it's easier just to provide the message directly as part of the commit command.git commit -m "super duper awesome commit!"Working with Remote RepositoriesOne of the most important aspects to using git as a source control system is that you utilize a remote repository to store your code. Although technically you can get by with using only a local repository, if you were to have problems with your local machine, your code would be gone. Using a service like Github ensures that you can backup your code without the danger of losing anything.In order to associate your local repository with a remote repository, you need to create at least one remote. Think of remotes as a simple way of creating an alias or a bookmark to a URL which points to a remote repository.To begin with, you can always list out all existing remotes for a repository by using:git remote -vYou can easily add a remote to an existing local repository using the git remote add command. Every remote requires a unique name.git remote add myremote https://github.com/rstrube/rstrube.github.ioNote: the default remote when using Github is called origin.To remove a remote you can use:git remote remove myremoteTo rename a remote you can use:git remote rename {old name} {new name}Pulling and PushingPulling and pushing allows you to download commits and upload local commits to and from the remote repository.The command to push all local commits to a remote repository is:git push {remote name} {branch}If for some reason your local repository's branch is different than that branch name in the remote repository you want to push to, you can slightly modify the push commandgit push {remote name} {local branch}:{remote branch}Example: if we are working on the stable branch in our local repository but we want to push it to the master branch of the remote repository we would use:git push {remote name} stable:masterThe command to pull changes from a remote repository is:git pull {remote name} {branch}Example:git pull origin masterTo pull the master branch from the origin remote repository.Listing CommitsIn order to list all commits in your local repository (including those pulled from remote repositories) use the git log command.There are many variations of this command, but the most useful are:git logLists all commits in a default view, you can press space to move to the next page, and q to exit.Example output:commit fb74d318ee5552a3656bf7b96d55cdacd4895f86 Author: [email protected] <[email protected]> Date: Tue Dec 16 11:27:49 2014 -0600 intial commit of ghost to azure commit 62d55ac165e8d3ce96c87ddff1cf3357d5ee4a67 Merge: 5d69839 1c452a6 Author: xxxx <[email protected]> Date: Mon Dec 15 20:02:11 2014 +0000 Merge branch 'master' @ 0.5.7 into stable commit 1c452a605299861630a1d53ea2133f5a1f2f283f Author: xxxx <[email protected]> Date: Mon Dec 15 17:09:24 2014 +0000 Version bump to 0.5.7 commit 7d0deebc086618835853f64489005f1a001b60aa Merge: 4c12f5a ff321e0 Author: xxxx <[email protected]> Date: Mon Dec 15 15:58:08 2014 +0000 Merge pull request #4653 from jaswilli/client-

Read full article »
Est. Annual Revenue
$100K-5.0M
Agree?
Est. Employees
1-25
Agree?
CEO Avatar

President

Robert Strube

CEO Approval Rating

90/100

Read more