At Monarch Digital, we use Git (http://git-scm.com) to manage our source code. Overall the process of storing code in a repository with a timeline of changes is referred to as version control. We decided to use Git for our version control workflow because it is one of the most popular systems in use (for ubiquity) and because it is well-designed to support distributed teams working asynchronously on projects.
This post may be used as a reference of basic Git commands and is useful for those who are new to Git as well as a refresher for those who use Git everyday.
First, here are a few terms to keep in mind:
Clone: Make a copy of a repository.
Commit: A saved version of your repository. Every commit has a unique hexadecimal id.
Remote: A connection to another repository. This is most often a connection to a hosted repository on Bitbucket or Github
Branch: A branch is a named pointer to a specific commit in the repository.
Use git clone to download a copy of the repository. The first argument is the url of the repository and the second argument is the directory clone into.
- Clone a repository:
git clone [email protected]:myaccount/repo.git cd repo
- Clone a repository into the current (existing) directory.
git clone [email protected]:myaccount/repo.git ./
- Clone a specific branch of a repository.
git clone --branch=d7 [email protected]:myaccount/repo.git ./
- Clone a “bare” (no working directory) repository.
git clone --bare [email protected]:myaccount/repo.git ./
Use git branch to view and manage branches.
- Sow a list of branches. (The * designates the current branch)
git branch git branch -a
- Create a new branch. Note: this creates the branch but does not switch to it.
git branch new-branch
- Delete a branch.
git branch -d new-branch git branch -D new-branch(forced delete)
Use git remote to view and manage connections to other repositories.
- List all remotes.
git remote -v
- Add a new remote.
git remote add repo2 [email protected]:myaccount/repo2.git
- Delete a remote.
git remote rm repo2
Use git checkout to switch between branches.
- Switch to an existing branch.
git checkout deploy
- Switch to a new branch.
git checkout -b new-branch
git fetch, merge, and pull
Use git fetch, merge and pull to update your repository.
- Fetch changes from a single remote.
git fetch origin
- Fetch changes from all remotes.
git fetch --all
- Merge remote branch changes into the working directory.
git checkout deploy git merge origin/deploy
- Pull is a shortcut for fetch/merge. Pull can have unexpected behavior if you’re not careful and I personally recommend using fetch and merge separately instead.
git pull origin master
git add and commit
Use git add to stage files for the next commit. Use git rm to remove a file from the repository. Use git commit to apply staged changes to the repository.
- Add a single file or directory to the commit stage (git add is recursive by default).
git add myfile.txt git add mydirectory/
- Remove a file from the repository (removal is staged for the next commit similar to add)
git rm deleteme.txt
- Add everything (including deleted files) to the commit stage.
git add --all ./
- Review changes staged for commit.
git diff --cached
- Commit all staged changes.
git commit -m “My changes”
- Add and commit all changes at once. This is an easy way to accidentally commit extra files and I personally do not recommend this method.
git commit -am “My changes”
Use git push to send changes to a remote repository.
- Push changes to a remote.
git push origin deploy
* * *
Hopefully, this article can be a useful resource for you to help you get started using Git or to reference as you learn. Mastering a new version control system may seem daunting at first, but the benefits to having a versioned timeline of code changes for your projects is invaluable, and well worth the effort put in to learn.