Git is a tool used for version control: it allows you to store past versions of a project. To store the current state of your work in the project history, you must commit your work. A commit is like a digital snapshot in time of your folders and files. When working, you may want to add a new feature to your project. Branches are used to safeguard your current code from being broken by that new feature you're adding. Each branch is the sum of the commits that forms it. Branches are timelines of commits that diverge from your master branch (generally speaking, the "main" branch) at a specified point in time. If your new feature works, then your test branch can be fused into your master branch via a merge command. At that point, the commits from the test branch become an inseparable part of the master branch's history. It is then safe to delete your test branch and continue working on your master branch.
You can use git commands to push your local work up to GitHub (i.e. "the cloud"), where it can be cloned down to other coders' computers, or forked over to their own GitHub accounts online. Git and GitHub are invaluable tools for working with others on group projects. Individual coders work on their own branches and then submit pull requests to merge their branches into the group project's master branch. Pull requests are used to solicit peer review on proposed branch merges before they are permanently committed into the master branch.
To install git on linux, type the following into terminal.
Up to Fedora 21:
$ yum install git
Fedora 22 and later:
$ dnf install git
Debian or Ubuntu:
$ apt-get install git
If you get an error, try adding sudo in front as this will give you access to install git.
To verify which version of git you have installed, type the following into terminal:
$ git -- version
If you are on a mac, git may already be installed. To verify type this in the terminal:
$ git --version
If a version is displayed, you have it. If not, proceed to download git from the official page. This will download a DMG file. Double click on it and follow the steps. At the end open a terminal and verify with git --version if it is there.
Now you need to set the username and user.email associated with git. This is relevant because services like github, or bitbucket will check your user.email, when you push something. If user.email is different from that set on github, you might encounter some problems.
Username:
$ git config --global user.name "your username"
User.email:
$ git config --global user.email "yourEmailUsedOnGithub@gmail.com"
Last step is to set up a ssh key. This is useful because otherwise you will be asked to enter your username and password often in the terminal when you use most git commands.
The steps are best described here.
Create a new local repository
$ git init
Clone an existing repository
$ git clone http://URL/REPOSITORY.git
Show changed files
$ git status
Show changes in tracked files (that haven't been staged yet)
$ git diff
Add files to your staging area
$ git add <files>
Add all files from your current directory
$ git add .
Commit all local changes to the files in your staging area (i.e. files that have already been added)
$ git commit -m <"descriptive message">
Add all tracked files to the staging area and commit all local changes to these files
$ git commit -a -m <"descriptive message">
Amend changes to your last commit
$ git commit --amend
Show all commits, ordered from newest to oldest
$ git log
Show all of your commits in a tree-like view
$ git log --graph --oneline --all
Create a new branch
$ git branch <new-branch>
Switch branch
$ git checkout <branch>
Create a new branch and switch to it
$ git checkout -b <branch>
Delete a local branch
$ git branch -d <branch>
List the URLs of all remote repositories
(e.g. the remote repo that you cloned from is called origin
by default)
$ git remote -v
Add new remote repository
$ git remote add <shortname> <url>
Push committed changes on your local branch up to a remote server
$ git push <remote> <branch>
Pull all files from a branch of the remote repository, and merge this into the current local branch
$ git pull <remote> <branch>
Merge a branch into your current HEAD (commit history will read: commits from checked out branch first, then merged branch commits)
$ git merge <branch>
Rebase a branch into your current HEAD (commit history will read chronologically, regardless of which branch they were from)
$ git rebase <branch>
Unstages files i.e. resets back to previous commit
$ git reset HEAD
Discard local changes in a specific file
$ git checkout <file>
- Ebooks:
- Online Courses: