/
Git Basics

Git Basics

Important

In order to work as an effective you will need some kind of source control, gitlabs is what we chosen.  Each team member will need an account, but one team member will act as the master with the others contributing.

  1. Goto www.gitlab.com
  2. Sign up for your team on the GitLab server
  3. Confirm the email to gain access to the server
  4. Start creating your projects, make sure the project has a visibility of private

If you want to use the GUI (they both work with GitlaDownload GitHub console

  1. If you want the GUI, download it from here

You will be working from you local machine will want to types of apps to help you, GitGUI and GitBash, both can be obtained from https://git-scm.com/downloads

For a quick overview of how git works visit https://guides.github.com/activities/hello-world/

Make sure you do not work of the master, work of branches, when you are clear that what you have is worthy of pushing back into the project push your changes back to your branch and not master.  Merge the branches once you are ready to create an MVP. 

Don't leave large gaps between your commits.  Commit and update your branch on a regular basis.  Git will ensure that if you screw something up you can always go back to a working point but only if you engage in regular commits.

Use this cheat sheet as a quick start guide.

The git lifecycle

CommandAction
git diffuse this toe determine what has changed in your repo
git addtells git you want a file to be tracked, just because a file is in a working directory doesn't mean git is tracking it for changes
git checkoutget a file from the repo, even overwriting a current file on your drive if you haven't pushed it into the repo (basically an undo)
git commitadd a file to the repos timeline
git remote -v / git remote show originshow the origin url of the local repo
git remote set-url origin https://github.com/user/repo2.git
assign https://github.com/user/repo2.git to origin


You don't have to use fixed file locations on your harddrive, to initialise a file location that already has sources with git

  1. Open the git console and navigate to the location that you want manage with git
  2. Type git init - this will create the git database in that directory
  3. Type git add * - this will add all the files in the current directory and sub directories (recursively) the local git db, the files remain in staged state until committed 
  4. Type git commit -m <some message in single quotes> - this move the files from the staged state to commited

The files that have just been committed have not been pushed to the GitHub remote server so we need to do this

  1. Create a new repository or use an old one
  2. Type git remote add origin https://github.com/<your user name>/<repository name>.git - this will link the local directory to the remote GitHub
  3. Type git push -u origin master - this will pushed the committed the files to the remote GitHub

Branches

Don't work of the master, work of branches, use git branch <branch name> to create a new branch.  Then use git checkout <branch name> to switch to the branch.  If you already have a branch in the git repo execute git pull to get all the latest version history from the repo, you can switch to the branch using git branch <branch name>.

Creating a branch

shell command

git checkout -b <branch name>

Once a branch has been created any files created/modified in that branch will only tracked if you execute the command git add <filename> / .  You must commit your changes so that the changes are not visible to the other branches.

Once you add (mark the files to be tracked) the modified/created files, and commit them, you can switch to any other branch and work using the command 

shell command

git checkout <branch name>

You can delete branches the following command

Deleting branches

Local branch

git branch -d <branch name>

Remote branch

git push origin --delete <branch name>


Git add

Git add does more than add files to the local repository, to moves modified from modified state to the staged state, once in the staged state, they still need to be committed using git commit.  If you are sure the commit is definite, the execute a git push to push the committed changes up to git.

Working with stuff that's NOT on a local computer

  1. Create a directory (anywhere of your choosing) on the local machine
  2. Use git clone <url to github repository>
  3. You don't have to use git fetch < branch>
  4. Read more at especially the paragraph headed Fetching and Pulling from Your Remotes
  5. Once the clone has completed you can edit the content as normal