- Feb 2, 2019 - If you want to download an entire project from GitHub without version control data, you can use the Download ZIP option of the website.
- On GitHub, navigate to the main page of the repository. Note: If the repository is empty, you can manually copy the repository page's URL from your browser and skip to step four. Under the repository name, click Clone or download.
In August, we hosted a Women Who Code meetupat HubSpot and led a workshop for beginners on using git and GitHub. I first walked through a slide presentation on the basics and background of git and then we broke out into groups to run through a tutorial I created to simulate working on a large, collaborative project. We got feedback after the event that it was a helpful, hands-on introduction. So if you're new to git, too, follow the steps below to get comfortable making changes to the code base, opening up a pull request (PR), and merging code into the master branch. Any important git and GitHub terms are in bold with links to the official git reference materials.
Cloning (getting) code from Git repository to Visual Studio. There are lots of open source projects hosted on Git repositories as well as bloggers and authors publishing demos, patterns and code samples on web-hosted Git repositories such as GitHub, Bitbucket, Codeplex (Note: Codeplex has several source control options and Git is just one of them). On GitHub, navigate to the main page of the repository. Note: If the repository is empty, you can manually copy the repository page's URL from your browser and skip to step four. Under the repository name, click Clone or download. In this article, we will follow a step-by-step approach to deploy an Angular calculator application to GitHub. We will create a project using Angular CLI and then deploy that to GitHub.
Step 0: Install git and create a GitHub account
The first two things you'll want to do are install git and create a free GitHub account.
Follow the instructions here to install git (if it's not already installed). Note that for this tutorial we will be using git on the command line only. While there are some great git GUIs (graphical user interfaces), I think it's easier to learn git using git-specific commands first and then to try out a git GUI once you're more comfortable with the command.
Once you've done that, create a GitHub account here. (Accounts are free for public repositories, but there's a charge for private repositories.)
Step 1: Create a local git repository
When creating a new project on your local machine using git, you'll first create a new repository(or often, 'repo', for short).
To use git we'll be using the terminal. If you don't have much experience with the terminal and basic commands, check out this tutorial (especially the 'Navigating the Filesystem' and 'Moving Around' sections).
To begin, open up a terminal and move to where you want to place the project on your local machine using the cd
(change directory) command. For example, if you have a 'projects' folder on your desktop, you'd do something like:
To initialize a git repository in the root of the folder, run the git initcommand:
Step 2: Add a new file to the repo
Go ahead and add a new file to the project, using any text editor you like or running atouch command.
Once you've added or modified files in a folder containing a git repo, git will notice that changes have been made inside the repo. But, git won't officially keep track of the file (that is, put it in a commit - we'll talk more about commits next) unless you explicitly tell it to.
After creating the new file, you can use the git status
command to see which files git knows exist.
What this basically says is, 'Hey, we noticed you created a new file called mnelson.txt, but unless you use the 'git add'
command we aren't going to do anything with it.'
An interlude: The staging environment, the commit, and you
One of the most confusing parts when you're first learning git is the concept of the staging environment and how it relates to a commit.
A commitis a record of what files you have changed since the last time you made a commit. Essentially, you make changes to your repo (for example, adding a file or modifying one) and then tell git to put those files into a commit.
Commits make up the essence of your project and allow you to go back to the state of a project at any point.
So, how do you tell git which files to put into a commit? This is where the staging environment or index come in. As seen in Step 2, when you make changes to your repo, git notices that a file has changed but won't do anything with it (like adding it in a commit).
To add a file to a commit, you first need to add it to the staging environment. To do this, you can use the git add <filename>command (see Step 3 below).
Once you've used the git add command to add all the files you want to the staging environment, you can then tell git to package them into a commit using the git commitcommand.
Note: The staging environment, also called 'staging', is the new preferred term for this, but you can also see it referred to as the 'index'.
Step 3: Add a file to the staging environment
Add a file to the staging environment using the git add command.
If you rerun the git status command, you'll see that git has added the file to the staging environment (notice the 'Changes to be committed' line).
To reiterate, the file has not yet been added to a commit, but it's about to be.
Step 4: Create a commit
It's time to create your first commit!
Run the command git commit -m 'Your message about the commit'
The message at the end of the commit should be something related to what the commit contains - maybe it's a new feature, maybe it's a bug fix, maybe it's just fixing a typo. Don't put a message like 'asdfadsf' or 'foobar'. That makes the other people who see your commit sad. Very, very, sad.
Step 5: Create a new branch
Now that you've made a new commit, let's try something a little more advanced.
Say you want to make a new feature but are worried about making changes to the main project while developing the feature. This is where git branchescome in.
Branches allow you to move back and forth between 'states' of a project. For instance, if you want to add a new page to your website you can create a new branch just for that page without affecting the main part of the project. Once you're done with the page, you can merge your changes from your branch into the master branch. When you create a new branch, Git keeps track of which commit your branch 'branched' off of, so it knows the history behind all the files.
Let's say you are on the master branch and want to create a new branch to develop your web page. Here's what you'll do: Rungit checkout -b <my branch name>
. This command will automatically create a new branch and then 'check you out' on it, meaning git will move you to that branch, off of the master branch.
After running the above command, you can use the git branch
command to confirm that your branch was created:
The branch name with the asterisk next to it indicates which branch you're pointed to at that given time.
Now, if you switch back to the master branch and make some more commits, your new branch won't see any of those changes until you merge those changes onto your new branch.
Step 6: Create a new repository on GitHub
If you only want to keep track of your code locally, you don't need to use GitHub. But if you want to work with a team, you can use GitHub to collaboratively modify the project's code.
To create a new repo on GitHub, log in and go to the GitHub home page. You should see a green '+ New repository' button:
After clicking the button, GitHub will ask you to name your repo and provide a brief description:
When you're done filling out the information, press the 'Create repository' button to make your new repo.
GitHub will ask if you want to create a new repo from scratch or if you want to add a repo you have created locally. In this case, since we've already created a new repo locally, we want to push that onto GitHub so follow the '....or push an existing repository from the command line' section:
(You'll want to change the URL in the first command line to what GitHub lists in this section since your GitHub username and repo name are different.)
Step 7: Push a branch to GitHub
Now we'll push the commit in your branch to your new GitHub repo. This allows other people to see the changes you've made. If they're approved by the repository's owner, the changes can then be merged into the master branch.
To push changes onto a new branch on GitHub, you'll want to run git push origin yourbranchname.
GitHub will automatically create the branch for you on the remote repository:
You might be wondering what that 'origin' word means in the command above. What happens is that when you clone a remote repository to your local machine, git creates an alias for you. In nearly all cases this alias is called 'origin.' It's essentially shorthand for the remote repository's URL. So, to push your changes to the remote repository, you could've used either the command:git push git@github.com:git/git.git yourbranchname
or git push origin yourbranchname
(If this is your first time using GitHub locally, it might prompt you to log in with your GitHub username and password.)
If you refresh the GitHub page, you'll see note saying a branch with your name has just been pushed into the repository. You can also click the 'branches' link to see your branch listed there.
Now click the green button in the screenshot above. We're going to make a pull request!
Step 8: Create a Pull Request (PR)
A pull request (or PR) is a way to alert a repo's owners that you want to make some changes to their code. It allows them to review the code and make sure it looks good before putting your changes on the master branch.
This is what the PR page looks like before you've submitted it:
And this is what it looks like once you've submitted the PR request:
You might see a big green button at the bottom that says 'Merge pull request'. Clicking this means you'll merge your changes into the master branch.
Note that this button won't always be green. In some cases it'll be grey, which means you're faced with a merge conflict. This is when there is a change in one file that conflicts with a change in another file and git can't figure out which version to use. You'll have to manually go in and tell git which version to use.
Sometimes you'll be a co-owner or the sole owner of a repo, in which case you may not need to create a PR to merge your changes. However, it's still a good idea to make one so you can keep a more complete history of your updates and to make sure you always create a new branch when making changes.
Step 9: Merge a PR
Go ahead and click the green 'Merge pull request' button. This will merge your changes into the master branch.
When you're done, I recommend deleting your branch (too many branches can become messy), so hit that grey 'Delete branch' button as well.
You can double check that your commits were merged by clicking on the 'Commits' link on the first page of your new repo.
This will show you a list of all the commits in that branch. You can see the one I just merged right up top (Merge pull request #2).
You can also see the hash code of the commit on the right hand side. A hash code is a unique identifier for that specific commit. It's useful for referring to specific commits and when undoing changes (use the git revert <hash code number>
command to backtrack).
Step 10: Get changes on GitHub back to your computer
Right now, the repo on GitHub looks a little different than what you have on your local machine. For example, the commit you made in your branch and merged into the master branch doesn't exist in the master branch on your local machine.
In order to get the most recent changes that you or others have merged on GitHub, use the git pull origin master
command (when working on the master branch).
This shows you all the files that have changed and how they've changed.
Now we can use the git log
command again to see all new commits.
(You may need to switch branches back to the master branch. You can do that using the git checkout master
command.)
Step 11: Bask in your git glory
You've successfully made a PR and merged your code to the master branch. Congratulations! If you'd like to dive a little deeper, check out the files in this Git101 folder for even more tips and tricks on using git and GitHub.
I also recommend finding some time to work with your team on simulating a smaller group project like we did here. Have your team make a new folder with your team name, and add some files with text to it. Then, try pushing those changes to this remote repo. That way, your team can start making changes to files they didn't originally create and practice using the PR feature. And, use the git blame and git history tools on GitHub to get familiar with tracking which changes have been made in a file and who made those changes.
The more you use git, the more comfortable you'll... git with it. (I couldn't resist.)
I’ve decided to learn a little bit more about how to use GitHub because I want to start writing some automation code for the UCS B-Series configuration. I did some research on which editor to use (I’ve only used vi, that was the last time I coded) and I found that Atom (www.atom.io) is really sleek and that it integrates with programming languages and you can git directly from it. I will try to present my findings in the next few paragraphs.
In Summary:
- Go to http://www.github.com and create an account by clicking the ‘Sign up‘ button on the top right.
This slideshow requires JavaScript.
- Create a Repository (project): You can Read the guide or Start a project directly:
- Copy the Clone/Download URL
- git clone the URL (If you don’t have git installed, check this post from Chris Chernoff explaining how to go about it).
With the previous section, I demonstrated how to download or clone the project or repository into your computer. Now let’s move on to the Atom editor:
- Download the Atom editor from www.Atom.io, then install and start the application.
- Open the Atom editor and under the File menu click on “Add Project Folder”, Then select the project folder that came down from the git clone.
- In the Atom editor, write your python code and save the file to the repository folder using a .py extension. In this example, we will use the filename “ucs-config.py“
- If the atom-runner package is installed, you could execute the code directly from Atom pressing CTRL-R on your keyboard.
- Go back to the command line (Terminal) and execute the following commands:
How To Clone From Github
- Execute “git add ucs-config.py” and “git status” again
- Execute “git commit -m “message” “
- At this point, the new file is committed to the project in my laptop, but not synced up.
- Execute “git push“
- If this was your first time pushing a file, execute “git config –global push.default simple” to get rid of the legacy message.
- Everyone else who wants to have the latest revision of your project must execute a “git pull” if they already cloned the project or the “git clone URL” command to download the whole project.
- All these git commands work when you are in the repository path/folder of your laptop, use the “pwd” command to check your current path.
- Execute git with no arguments to display the Help:
The preceding steps came from these two videos:
Two things I learned after watching those videos:
- Add the terminal-plus package to Atom to open a terminal on you current repository and execute the git commands directly from the Atom editor.
- As an alternative to the command line add the git-plus package to use git directly from the Atom editor, after it is installed press “Command+Shift+P” to add, commit and push. To learn how to use git from atom check this video:
As always, excuse my grammar and lack of details, I mostly write these blogs to be able to remember the things I use once a month.