Sourced by Active Specialized Support Group (ASS-G)
- What Is Version Control?
- What Is Git?
- Getting Started
- Configure tooling
- Create repositories
- Make changes
- Group changes
- Review history
- Redo commits
- Synchronize changes
- Workflow Summary
Version control helps developers track and manage changes to a software project’s code. As a software project grows, version control becomes essential. Take WordPress…
At this point, WordPress is a pretty big project. If a core developer wanted to work on one specific part of the WordPress codebase, it wouldn’t be safe or efficient to have them directly edit the “official” source code.
Instead, version control lets developers safely work through branching and merging.
With branching, a developer duplicates part of the source code (called the repository). The developer can then safely make changes to that part of the code without affecting the rest of the project.
Then, once the developer gets his or her part of the code working properly, he or she can merge that code back into the main source code to make it official.
All of these changes are then tracked and can be reverted if need be.
Git is a specific open-source version control system created by Linus Torvalds in 2005.
Specifically, Git is a distributed version control system, which means that the entire codebase and history is available on every developer’s computer, which allows for easy branching and merging.
According to a Stack Overflow developer survey, over 87% of developers use Git.
Follow the instructions here to install git (if it's not already installed). Note that for this tutorial we will be using git on the command line only. While there are some great git GUIs (graphical user interfaces), We think it's easier to learn git using git-specific commands first and then to try out a git GUI once you're more comfortable with the command.
Git distributions for Linux and POSIX systems are available on the official Git SCM website.
Alternatively, GitHub provides desktop clients that include a graphical user interface for the most common repository actions.
Sets the name you want attached to your commit transactions
$ git config --global user.name "[name]"
Sets the email you want attached to your commit transactions
$ git config --global user.email "[email address]"
Creates a new local repository with the specified name
$ git init [project-name]
Downloads a project and its entire version history
$ git clone [url]
Lists all new or modified files to be committed
$ git status
Shows file differences not yet staged
$ git diff
Snapshots the file in preparation for versioning
$ git add [file]
Shows file differences between staging and the last file version (file in previous commit)
$ git diff --staged
Unstages the file, but preserves its contents
$ git reset [file]
Lists all local branches in the current repository
$ git branch
Creates a new branch
$ git branch [branch-name]
Switches to the specified branch and updates working directory
$ git checkout [branch-name]
Combines the specified branch’s history into the current branch
$ git merge [branch-name]
Deletes the specified branch
$ git branch -d [branch-name]
Lists version history for the current branch
$ git log
Lists version history for the file, including renames
$ git log --follow [file]
Outputs metadata and content changes of the specified commit
$ git show [commit]
Undoes all commits after [commit], preserving changes locally
$ git reset [commit]
Discards all history and changes back to the specified commit
$ git reset --hard [commit]
Downloads all history from the remote repository
$ git fetch [remote]
Combines the remote branch into the current local branch
$ git merge [remote]/[branch]
Uploads all local branch commits to GitHub
$ git push [remote] [branch]
Downloads bookmark history and incorporates changes
$ git pull
You now have everything you need to successfully clone a repository, create a branch, make changes, and push those changes up to a master branch. Your workflow should look something like this:
git clone <url>
(clone a Git repository)cd
(change directory) into your newly cloned repositorygit checkout -b <branch-name
> (create and switch to your new branch)git status
(the output of this should be pretty boring right now)- make your changes
git status
(you should see a list of the files you changed now)git diff
(make sure you actually want these changes)git add
(add your changes to your staging index)git status
(yes, it’s my favorite command, do it)git commit
(commit your changes)git push
(push your changes up to the remote branch)- merge your changes in GitHub or other cloud VCS services
git checkout master
(switch back to the master branch)git pull
(pull down the changes your just merged)git status
(just to make sure everything is nice and clean)- repeat! (except step 1, you only need to clone once)