Difference between revisions of "Basic Git Tutorial"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Examples)
(Examples)
Line 281: Line 281:
 
git add .
 
git add .
 
git commit –m “First Commit”
 
git commit –m “First Commit”
git remote add origin git@<nowiki>gitlab.com</nowiki>:username/<nowiki>projectname.gi</nowiki>t
+
git remote add origin git@gitlab.com:username/projectname.git
 
git remote –v
 
git remote –v
 
git push origin master
 
git push origin master
Line 294: Line 294:
  
 
<source lang="bash">
 
<source lang="bash">
git clone git@<nowiki>gitlab.com</nowiki>:username/<nowiki>projectname.gi</nowiki>t
+
git clone git@gitlab.com:username/projectname.git
 
</source>
 
</source>
  

Revision as of 16:18, 23 September 2015

WORK IN PROGRESS

Git uses the concept of branches where multiple people could work on different parts of a project at the same time. Eventually they could merge all their work together to create a complete project. They could also work on it some more should their be a need for bug fixes, updates, etc. Developers can pick and choose what features they want to add or remove and can see a log of the work done and changes made by their peers. In this tutorial, I will be focusing on the command line for Git and not the GUI. I will also be focusing on Git Lab because that is our class’s main focus. This tutorial will give a very basic overview of Git. There are many different kinds of commands, but only a few need to be used.

Anything within “< >” is an option.


Getting Started

Download the Git program here. https://git-scm.com/downloads

You will now be able use it via command line, or with the GUI. It is suggested that you use the command line, as there are more features. To use the Windows command line, go to the start menu -> type “cmd” -> enter. If you wish to use a nicer version of the Windows command line, it is suggested you download ConEmu available here http://www.fosshub.com/ConEmu.html.


If Git does not work on ConEmu, then make sure the options matches the one in the picture. Access options by “windows key + alt + p”. Go to Startup -> tasks and then press the ‘+’ button. Now name it “Git Bash” and then type in the bottom box and make sure it looks like the picture shown below:

"C:\Program Files\Git\bin\sh.exe" --login –I
GitTutorial ConEmu Setup.JPG

Do not touch the parameter box. Press Save. You will now be able to access Git in a new cmd window. Press the drop down menu button near the green ‘+’ button. Press the {Git Bash} option. You are now ready to use Git. Your terminal should look something like this:

GitTutorial ConEmu.JPG


Setting up SSH key

Go to www.gitlab.com -> Profile settings -> Add SSH Key -> Now open up terminal and type in:

Command Description

ssh-keygen -t rsa -C "youremail@email.com"

cat ~/.ssh/id_rsa.pub

Generates the key

Shows your key


After concatenating the key, copy and paste the SSH key into the “Key” box. If you are on windows and can’t copy paste type in: clip < ~/.ssh/id_rsa.pub


Your SSH Key is now set up.


Setting a new Git Repository

If you are working on an existing remote repository, then this part is unnecessary. However, if you are working on something, and there is no Git repository, then you must set up a new project on the Git website.


Setting up a new project is simple on Git Lab. Simply go to www.gitlab.com and then click on the green button that says “Add Project”. Type in your project name in the “Project path” box. Add any descriptions about your project. Although it is optional, it is highly suggested you briefly describe your project. Press Create Project once done.


Your project path will now look something like this “https://gitlab.com/username/projectname”. From here, anyone can grab your project via SSH or HTTPS.

SSH path example: git@gitlab.com:username/projectname.git

HTTPS path example: https://gitlab.com/username/projectname.git

See the example on how to upload your local repository to the server.


Setting it up on Eclipse

Currently, many people, myself included, are having issues with SSH in Eclipse, but I will go ahead and give instructions on how to set it up. Get your .ssh directory by typing in ConEmu "cd ~/.ssh" -> "pwd". Take that directory and go into the Eclipse's preferences. Go to General -> Network Connections -> SSH2 -> Input your .ssh directory and then Add private key (id_rsa, id_rsa.pub).

GitTutorial EclipseSSH.JPG

More instructions can be found on using Gitlab with Eclipse here: http://www.socialledge.com/sjsu/index.php?title=Eclipse_%26_Gitlab_Tutorial

Commands

INIT

The first thing you want to do before you init is to add a project in the Git website see “Setting up a new Git repository” section. If you have a folder with code that is not on Git, and you wish to put it on the Git server, then you need to initialize Git into your folder. This creates a .git folder, and the current directory is now a Git repository. The .git folder contains Git information such as branches. Initializing your folder is local to your computer and is not yet uploaded onto the server.


Command Description

git init

git init <directory>

Initialize current directory

Initialize selected directory


CLONE

If you see a repository that you want to work on, you can “clone” it into a directory and start working on it. Cloning it will download the entire repository as well as a .git folder. Note that clone is different from “pull”. This will be explained later. Just use this command once at the beginning of the project unless you want multiple folders.


Command Description

git clone <repo>

git clone <repo> <directory>

Downloads entire repository to current directory

Downloads entire repository to selected directory

Collaboration

Once you’ve worked on the code and you wish to upload your work to the server, you must first “add” the current snapshot of your work to the index, and then “commit” it. After doing these tasks, “push” your work onto the master. If the master has been updated with someone else’s code, you may pull it to fetch and merge your repository.



Uploading commands

ADD

When you feel like you’ve made changes that you wish to upload to the master, you must first “add” your code. Adding your code puts the current snapshot of your work into the index. This is a local command and does not add anything to the server yet.


If you created your repository, and it has not yet been uploaded, then use “git remote add”. This will communicate your repository to the git server. This still does not upload your repository.

Command Description

git add <directory/file>

Adds snapshot of current code to index

git remote add origin <git url>

Adds remote to existing repository. Use this if you started a local repository, and have not yet pushed anything to the server. See Example.



COMMIT

After adding your work, you must “commit” it. If you don’t commit and you try to push, you may receive an error saying that you must first commit. Committing your work puts your work’s index into the Head. When you commit, you also set a checkpoint. Each time you commit you move forward and create new checkpoints. When merging with master, the master moves forward to the current checkpoint. Committing is essentially a history log about changes made. When you commit you include a message saying what changes you made so others can see it as well. Other developers can see the changes you made, and you can see the changes they made. After committing, you are ready to push your work onto the master

Command Description

git commit –m “My changes…”

Pushes your work from index to the head. ‘-m’ allows you to write a messages. Make sure the message is meaningful so that others can understand it.



PUSH

After adding and committing your work, you can now push onto the master. This will make changes in the master and everyone may now pull the updated master to their repositories with the changes you’ve made. Because you’ve committed, people can now view the messages and changes you’ve made to the code.


Command Description

git push origin master

Pushes your remote repository to master

git push origin branchname

Pushes your remote repository to branch



Downloading commands

PULL

To update your current repository with a newer one, use the “pull” command. This command updates the current repository with the current version on the Git server. Pull is different from clone in that it fetches and merges the repository to your local repository, that way you can continue working. Cloning does not do this, and you get a completely new repository.

Command Description

git pull origin master

Updates your repository with master.

git pull origin branch

Updates your repository with a branch.

Branches

BRANCH

Use branch when you want to deviate from the workflow and start on working something else. For example, after working on master, you want to add a new feature, and someone else wants to work on a new feature. You can both use branch to split apart from the master. Eventually, you will merge in with the master once you've added your features. This command allows you to see other branches as well as create and delete branches.

CHECKOUT

If you want to change to another branch, use the checkout command. You can change to any branch you wish.

MERGE

This command merges other branches together. When merging with master, the master goes to where the branch is.

FINISH THIS PAGE

Examples

Starting a new repository

www.gitlab.com

Add project

Create Project

git init
git add .
git commit –m “First Commit”
git remote add origin git@gitlab.com:username/projectname.git
git remote –v
git push origin master

People will now be able to make changes to your code.


Cloning

You see a code you want to work with, so you will clone it into your current directory.

git clone git@gitlab.com:username/projectname.git

Pushing

After working for some time on a code, you’ve made some changes. Now, you want to push it to master

git add .
git commit –m “Commit comment.”
git push origin master

If you wish to push to a different branch

git push origin branchname


Pulling

Teammates made some changes to the code.

Time to update your repository.

git pull origin master

Likewise, you can pull from a branch

git pull origin branchname


ADD BRANCH EXAMPLE