User Tools

Site Tools


programming:git

Supplemental Git Information

What is Git?

Git is an open source version control system for code management. Let's break that down a little.

Open source - open source is a description of software where the source code is made freely available (or open) to all.
Version control - a system that records changes to a file or set of files over time so that you can recall specific versions later.

What that means is that by using Git (or other version control system software) you are able to more easily track changes you've made to code as you work on it. This is particularly useful for protecting yourself when testing out new features or when working collaboratively with others on a single project.

When you hear about Git you will almost immediately hear about GitHub. GitHub is a cloud based code hosting platform that developers use in conjunction with Git so that they can access and work on code from anywhere they have internet access.
Here's a helpful git cheat sheet https://github.com/remigiusz-suwalski/programming-cheatsheets/tree/master/cpp
Here's the official Git ebook conveniently turned to the getting started page where they explain all of that in significantly more detail
https://git-scm.com/book/en/v1/Getting-Started
This is another version control tutorial that uses git
http://smutch.github.io/VersionControlTutorial/
For those that prefer an approach with less reading required, here's a youtube video covering these basics in a bit more depth
https://www.youtube.com/watch?v=3RjQznt-8kE ===== Getting Started ===== First, you need to get Git! Since we are using the command line for this, you need to download the command line tool. Go to: * http://gitforwindows.org/ Install Git, use default options.
Here's a video showing installation
https://www.youtube.com/watch?v=MFtsLRphqDM You will need to create a folder on your machine that you want to be your git Repository. You may use a pre-existing project folder. I have created mine on my desktop. Run your git command line program. This will emulate a linux/unix shell. If you need help with the command line, «broken link for some examples» are some simple commands to help you. Using your terminal, navigate to the folder that you created for your repository. Todo this type cd and the full path of your file like I did here. You may be able to find the path by opening the folder and clicking on the bar. Type the command “git init” in your terminal after you have successfully navigated to the folder. Congratulations! You have created a git repository. Back to the Programming Main Page =====Turn Local Repository into an online Repository===== So you made a local Repo but now you want to access it from everywhere here is how you do it. For this example i made a txt file with some text in it. You can type “git status” this shows what has yet to be committed or the status of your repo. See how “Text.txt” is red this is an untracked file we would like to start tracking it so we can “pull” it later if need be. A simple way we can do this is with the command “git add .” or “git add -A” As you can see now when we type “git status” it has become green and says “new file”. We now need to type git commit -m “your comment here” do not forget your quotes on this command they are necessary to save yourself a headache. YOU ARE NOT DONE YET. We still need to “push your stuff to you git account. As you can see it says you have a clean working directory when you run command “git status” that just means everything on you local machine is staged. We now need to use “git push” however we need some information first. Go to Your Github after you login click your profile image and select Your repositories. Click New.
This assumes you do not have an online repo yet 1st NAME YOUR REPO this part is very necessary you can not create the repo without giving it a name. 2nd click public or private. As of this tutorial private repos are free 3rd you have the option to initialize your repo for intent clone capabilities however we already have a repo on our machine so we do not check this box. After your done Click Create. When you Click create it will bring up some command line options for you we use this one. Copy the link yourself or click add to clipboard button. Paste commands into your terminal. AND BAM!!! Your stuff is on github Click the repo name to refresh the web page to see your stuff. Congratulations! You have created an online git repository. Back to the Programming Main Page ===== Getting your files “clone” ===== If you are creating a repository for a project that already has files for it, you will need to get those files. Git uses “clone” to copy over an already existing repository to your new folder. For this example I will start with the repo we created from earlier in the tutorial with some slight alteration done to it to simulate work done. To “clone” open a terminal window in the folder you wish to “clone” or download your files to. keep in mind that when you clone it will create a folder in the location you specify Goto the git repo you wish to “clone”. Click clone or download button you will see some options however for this example we will be using the HTTPS link. Type “git clone <GIT PATH HERE>” like shown below. Congratulations! You have cloned an online git repository. Back to the Programming Main Page ===== Add ===== Okay, so your project is now in a repository. If you make any new files, they need to be inside of your repository otherwise they won't be saved. Once they are all inside of your repository you need to add them. You do this with the “add” command of git. If you are following from the top this will look very similar to previous if not here it is in its entirety. For this example i made a txt file with some text in it. You can type “git status” this shows what has yet to be committed or the status of your repo. See how “Text.txt” is red this is an untracked file we would like to start tracking it so we can “pull” it later if need be. A simple way we can do this is with the command “git add .” or “git add -A” Note: If you do not want to add all your files you can type them in individually using there full name. As you can see now when we type “git status” it has become green and says “new file”. Back to the Programming Main Page ===== Commit/Push ===== When you are ready to save your changes to the repository you need to use a command called “commit” If you do not commit, any changes made will be local to your repository but will not be picked up if you push or someone pulls from your repository. We now need to type git commit -m “your comment here” do not forget your quotes on this command they are necessary to save yourself a headache. Type “git push” all commited files will be pushed. Back to the Programming Main Page ===== Pull ===== Say the online repo has been worked on and you need all the new code. You can type “git fetch” to retrieve to update the commit log on you local repo As you can see the change in status before and after the “fetch” Next all you need to do is run command “git pull” Congratulations!! All your files are up to date with the master branch. Back to the Programming Main Page ===== Pull Request ===== NOTE: This is different than a pull A “pull request” is you the programer requesting the “owner/collaborator” of the repo to pull your code; To review your code; And potentially commit that code to a branch in the repo. So you forked a repo (see Below) and are ready to share your code with the “owner/collaborator” First you will need to go to the forked repo on github in your repolist Once there click pull request what will pop up next is a bar where you can compare your changes and submit them to the colaborator Congratulations!!! You made a pull request Back to the Programming Main Page ===== Branch ===== Sometimes you will have multiple contributors in a project as well as multiple versions that you may need to switch between. We can do this by adding branches. Here is how you make a branch. First lets see what “branch” we are on. We can list them by typing “git branch”. To add a branch we type “git branch NEW_BRANCH_NAME_HERE”. I called the new branch name “sam” NOTE: The branch name can be anything. So you now have a branch named “WHAT_EVER_YOU_PUT” As you can see in the picture above when you type “git branch” master is green this means that you are working in master NOT your newly created branch. So how do we switch branches. It's simple just type “git checkout BRANCH_NAME”. As you can see “sam” is now green. I am now working in the “sam” branch. Now would be a good time to push your new branch to the online repo just type “git push -u origin BRANCH_NAME_HERE” Congratulations!!! You Have Created A Branch and Are Working In Said Branch. Back to the Programming Main Page ===== Merge ===== For this example i have modified some files to cause some merge errors that may occur when trying to merge. To merge two branches first GOTO the branch that you wish to put all changes into. For example I want to put everything back into my “master” branch because that branch is my production branch or in other words the branch that is “publicly available” or the branch with little or “no” errors. Now that you are in the “master” branch simply type “git merge BRANCH_NAME” for me the branch i want is called sam. As you can see I ran into some errors this error is simple i can either accept or reject the incoming “code” or if need be I can accept both changes if it is relevant. For this example I am using Visual Studio Code however if you are not you may need a merge tool if you have errors. I am simply going to click “Accept current change” because that bit of text looks better. After conflict has been resolved you need to mark the file as resolved as you can see below. Just type “git add .” then “git commit -m “MESSAGE” ” then “git push” Congratulations!!! Merge Complete. Back to the Programming Main Page ===== Fork ===== A Fork is used when someone else owns a repository in which you yourself would like to contribute to.(this creates a copy of their repo and puts it into your repo list) Making a fork is simple goto the repo on github that you wish to contribute to. at this time the button to fork is in the top right corner like shown below. After you have created a fork go to your repo list. clone the fork like normal. (view above for clone instructions) After you make changes that are “significant” you can do a pull request (see Above) this asks the owner to review your code and if it is “what they are looking for” they may add it to the project. Congratulations !!! You Forked a Repository Back to the Programming Main Page

programming/git.txt · Last modified: 2019/04/01 20:09 by tima