Chapter 4 Using Git and GitHub
4.1 Git and GitHub Overview
Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.
GitHub serves as a web-based interface for Git while also providing many otehr valuable features for version control and collaboration.
Downloading Git can be done at https://git-scm.com/downloads. GitHub is free to create to create an account at https://github.com/.
During this section, we will work through the basics of Git and GitHub!
4.2 Important Git Commands
git clone # clone a repository into a new directorygit init # initialize local directory as a git repositorygit add . # add files to local repositorygit commit- m “commit message” # commit files you staged in local repositorygit remote add origin URL # sets remote location of Github repository with URLgit remote -v # verifies remote locationgit push # pushes changes from local repository to remote repository git branch # check which branch you are currently working ongit checkout branch-name # change branchesgit checkout -b branch-name # create a new branch if branch-name does not exist git status # displays state of working directory and changed statesgit pull # update local version of a repository from remote repository4.3 Git and GitHub Simple Walkthrough
Let's learn some basics of Git and GitHub through an example.
To start, make a new directory and cd into it.
mkdir myGitTutorial
cd myGitTutorialNow, create a readme file and add a description within our new directory.
touch readme.md
echo this is the read me for my Git tutorial > readme.mdInitialize the Git repository.
git initAdd and commit files to local git repository.
git add .
git commit -m “initial commit.”Now, if neeeded, create a GitHub account and then create a new repository.
Back in command line, set remote location of repository
git remote add origin link/of/your/GitHub/repoNow, we can push contents to the remote repository on GitHub.
git push -u origin masterThe repository is now set up and can be viewed in Github! You will see the readme file we created in the repo.
Next, we can create a new branch.
git checkout -b tutorial-branchMake a new file in the tutorial branch
touch branch.txt
echo this is for the tutorial branch > branch.txtStage and commit files to branch.
git add .
git commit -m "txt file to branch"Push staged files to branch.
git push --set-upstream origin tutorial-branchWhen we return to github we will notice that there are two branches!
Notice that the master branch does not have the newly created branch.txt file. If we navigate to tutorial-branch, we will see the branch.txt file!
Let's update the master branch with the added files from the branch. To update the master branch, we will need to create a pull request.
Click the green button that says ‘compare and pull request’.
Then, click ‘create pull request’. Notice how there are no conflicts with the master branch!
Now click ‘Merge pull request’ and then ‘confirm merge’.
Once the merge is done processing we can return to the master branch and we will see that branch.txt is now available on the master!