Part 1: Installing VMware and CentOS
Part 2: Expanding CentOS's Minimal Install
Part 3: Installing Python 3.6
Part 4: Linking PyCharm to Github via SSH
This is Part 5: Running through Git-It again, this time with PyCharm
Run through Git-it again, but with PyCharm
At this point we're just about ready to start working in PyCharm. All that remains is to work through Git-it again, but using only PyCharm, to discover any differences along the way (or to become more familiar with PyCharm). Each numbered step below corresponds to a step in the Git-it sequence.
Before you begin: Remove hello-world and patchwork
In Github, remove hello-world and patchwork by going to https://github.com/my_user_name/repo_name/settings (like https://github.com/gbarwis/hello-world/settings, for example) and scrolling down to the bottom of the page to the delete this repository feature.
In a terminal, remove both the hello-world and patchwork repositories from your local host via:
$ rm -rf ~/hello-world/ ~/patchwork/
1. Get Git
For this step, since your operating system already has
git
installed, all that needs to be done is to configure the git
global environment variables. As far as I can tell, this cannot be done through PyCharm.In a terminal, add global
git
options via:$ git config --global user.name "My Name"
$ git config --global user.email "my@email"
2. Repository
Create a new project folder
The first task - create a new project folder - seems pretty straightforward. Click on Create New Project ...
... then name it hello-world and click Create. I'm going to leave mine in the PycharmProjects directory to which it defaults, in order to keep my home directory cleaner for a time when I may have multiple projects going.
Initialize the folder as a git repository
The second task - initialize is as a git repository - also turns out to be pretty quick:
- Make sure the new hello-world project is selected on the left.
- Since
git
is Version Control Software, click on the VCS menu. - Since the spirit of
git init
is to enablegit
to track that directory, click on Enable Version Control Integration.
- Select Git and click on OK.
Finally, Git-it suggests that we use
git status
to confirm that the directory is set up as a repository. In PyCharm, the equivalent is to use the Version Control Tool Window. You can bring this up by pressing Alt+9
on your keyboard, or by clicking in the menu on View / Tool Windows / Version Control.But wait - what's this? I just created this project folder, I haven't created a single file to put in it yet, but somehow there are four "Unversioned Files" listed here. Where did they come from?
As it turns out, the
.idea
directory (where all these new files have appeared) is used to store IDE-specific settings (remember that PyCharm is an IDE, or Integrated Development Environment). These include general information about the project, as well as specific configuration information about your own local IDE environment.The first thing you'll need to consider, then, is whether you want to version-control these files at all. If you're planning to share your project with other developers who may be using entirely different IDEs, then you may not want to share anything in the
.idea
folder. If, on the other hand, you expect that the project will be worked on only using PyCharm, then it's worth keeping these files in version control. This is actually a per-project decision that you can make, which is pretty cool.For me, with most of the projects I'll be working on initially, there will only be two people working on the code - me at home, and me at work. Both environments will have the same setup, so I'll want to share most of the files in this
.idea
folder. Over a longer timeline, though, I'll probably not want to share it at all.Here's what JetBrains has to say about this
.idea
folder, and what to include in version control:All the settings files in theConfigure version control project exclusions.idea
directory should be put under version control except theworkspace.xml
, which stores your local preferences. Theworkspace.xml
file should be marked as ignored by VCS.
To exclude files or directories from version control (at least with
git
), you use a file called .gitignore
. This file sits in the root of your project folder, and tells git
(and, by extension, Github) which files not to track in version control. For notes on specific syntax within .gitignore, see https://git-scm.com/docs/gitignore.To exclude the
workspace.xml
file, then, we'll want a line in our .gitignore
file that looks like .idea/**/workspace.xml
.You can create or edit this file directly in PyCharm (don't forget a carriage return at the end of your first line) or you can go to a terminal window, change into the project folder, and:
$ echo .idea/**/workspace.xml >> .gitignore
For what it's worth, here's the .gitignore
file that I'm using with my PyCharm projects:# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
### PyCharm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
Add the .gitignore plugin
After the
.gitignore
file is created, PyCharm will recognize its presence and prompt for the installation of a plugin. More details on that plugin can be found here on Github, but let's go ahead and install it. Installation is simple, but be sure to restart PyCharm when prompted, after the plugin has finished installing.After PyCharm has restarted you'll be asked if you would like to add unversioned files to the
.gitignore
file. Click Cancel here (we have unversioned files because we haven't committed anything yet).3. Commit To It
The goals of this section of Git-it are to add a file, check status, add files to git and commit changes.
Create a new file
I wonder if the workflow is different, in PyCharm, for files added through PyCharm itself versus files added manually in the terminal? I used PyCharm last time (via File / New / File), and that was simple and intuitive. This time, I'll create the file from the command line, to see what difference it makes within PyCharm.
$ cd ~/PycharmProjects/hello-world/ ## Just making sure I'm in the right directory
$ echo 'Peter Sykes' > readme.txt
Check StatusGreat. As soon as I've created this file, when I look back in PyCharm, I can immediately see that the new file is in place and that its contents are current. I can also see that, just like a few of the files in the
.idea
folder, this new file exists only locally, and isn't currently scheduled to be added to the repository - because their filenames are brown.There is a list of what all the various file status colors and highlights mean on the Jetbrains site at https://www.jetbrains.com/help/pycharm/2016.3/file-status-highlights.html. Here are the three that you'll be encountering right away, though:
- Black means that the file is unchanged
- Green means that the file is scheduled for addition to the repository
- Blue means that the file has changed since the last synchronization
- Brown means that the file exists locally only, and isn't scheduled for addition to the repository
Add and Commit Changes
In fact, let's go ahead and add all of the new files. In the Version Control window, select all of the brown files (your
readme.txt
, plus the files in the .idea
folder), right-click on one of the selected files, and click Add to VCS.Now let's commit the changes to the repository. Like most things in PyCharm there is more than one way to do this - menus, context menus, and so forth; I leave it to you to find the one that you like the best. For now, though, in the Version Control window select the Default header that lists all of the newly added files beneath it, and then click on the Commit icon to the left.
In the window that follows, you'll see a list of all files being committed; you can select each file, and see what's changed about that file in the bottom window. For now, in that window, each file's entire contents will show, and be green, because they're all brand new.
Enter your commit message in the appropriate box, then click on Commit. The commit message here is the equivalent of what you'd enter after the
-m
argument with the command line, as in git commit -m "here's my commit message"
.Make More Changes
In order to get one more glimpse of the differences in behavior, let's change this text file in two ways - from the command line, and from within Pycharm.
$ echo 'Michelangelo Antonioni' >> readme.txt
Go back into Pycharm. Notice that the file is now blue? That's because it has changed. Double-click on the filename in the Project window so that you can edit it, and add a third line of text to file.The next step in Git-it is to view the difference between the file now and how it was at your last commit. Again there are several ways to do this, but for now, just right-click on the filename in the Version Control window and click on Show Diff. This is a handy tool - play around with it a bit, as you're going to find yourself using it fairly often.
Finally, the last thing to do in Git-it is to use what you've learned to add and commit this latest change - so go ahead.
4. GitHubbin
Now it's time to link this all to Github. For this particular section of Git-it, you're going to need to bring up a terminal and add your Github username to
git
, because that's what Git-it will check before moving on to the next section.$ git config --global user.username your_github_username
5. Remote Control
When we did this the first time around, there were three steps to get a local repository on Github (1) create a remote repo on Github, (2) connect local repo to remote repo, and (3) Push the files from local to remote. Using PyCharm, we're going to approach this a bit differently.
Select the project itself in the Project pane. Then, in the top menu, select VCS / Import into Version Control / Share Project on GitHub.
In the next window, New repository name is the name it will have on Github. Confirm these entries and click Share.
Head on over to your profile on Github in a browser, and you'll see that everything is there.
This works well for getting a project up onto Github for the first time. For any subsequent updates to the same project, though, remember that commit saves your changes to the local git repository, and push sends that local repository information up to Github - so in order to push, you must first commit.
Let's add one more line to our
readme.txt
file, and then get it into Github. Below, you can see that readme.txt
has changed since the last synchronization (it's blue). Click on either of the two VCS buttons or press Ctrl+K
to commit those changes to your local git
repository.At the commit dialog, enter your Commit Message. At this point, you have two choices. You can click on the Commit button, and then push to Github later - or, you can hover over the commit button for a few seconds, and select Commit and Push to do it all at once.
Remember, if you only commit, but do not push, you will need to push to Github later - either by pressing
Ctrl+Shift+K
, or by going to VCS / Git / Push on the menu.6. Forks and Clones
For this section, begin by forking the jlord/patchwork project as discussed in Git-it.
From within PyCharm you'll clone this Github repository to your local machine. At the PyCharm main screen, click on Check out from Version Control (if you're already in a project, find this on the VCS menu). Remember that you're using SSH, so you'll need to use
git@github.com:
instead of https://github.com/
to begin your Git Repository URL. Click on Clone, and then when prompted to open the new directory, click on Yes.Note that PyCharm will automatically add an
.idea
directory to your project. This time, let's use something other than .gitignore
to ignore that entire directory - and to ignore .gitignore itself.Because this is a fork of somebody else's repository, we do not want to update a .gitignore file that would impact thousands of other users. Instead, we're going to use a file at .git/info/exclude to achieve an effect similar to .gitignore - except in a way that will not affect the upstream project. (In fact, if you issued a pull request at the end of this to get your branch merged back in, it would give you an error if you'd updated the .gitignore file).
In a terminal, go to the directory for your
patchwork
repository, and then:$ echo '.gitignore' << .git/info/exclude
$ echo '.idea/' << .git/info/exclude
The last step in this section of Git-it is to connect to the original repository - the one from which you forked. To do this, first click on VCS / Git / Remotes...
...and then add the remote that Git-it is prompting you to add. Remember that the URL naming convention is different, because you're using SSH.
7. Branches Aren't Just For Birds
In this section of Git-it, you're working with branches. Create the branch by going to VCS / Git / Branches...
Click on New Branch in the window that pops up, then name the branch as indicated in Git-it (add-<your_github_username>) and click OK.
The next thing that Git-it suggests is that you get into that branch to work on it through a checkout. However, when you created that new branch in PyCharm, it did that for you. You can confirm this in a terminal with
git status | grep 'branch'
or in PyCharm by again going to VCS / Git / Branches... and looking at the bottom of the pop-up window (where it will tell you your current branch).This happened because PyCharm didn't use
git branch <branch>
to create the branch - instead, it used git -b checkout <branch>
which tries to checkout an existing branch, and creates a new one if it doesn't exist.Next, Git-it wants you to add a new file to the contributors folder. This is straightforward - right-click on the folder in the PyCharm project pane, and then select New / File from the context menu that appears.
Follow the Git-it instructions on what this file should be named. Shortly, you will be asked if you want to add this new file to git. Check the box for Remember, don't ask again and click on Yes. This will save you the separate step of issuing a
git add
after each new file you create within PyCharm.Once you've written your Github username in this file, you won't need to add it to Git (because you did that in the previous step).
Finally, let's commit the change, and push it to Github - all in one step. No screenshots this time, as we've done this before. After it's complete, you can check it out on Github - look at the repository and change the branch, and you'll see the new file (and its contents).
8. It's A Small World
This next section of Git-it is about adding collaborators to your Github repository.
There's nothing to do in PyCharm here, so follow the instructions in Git-it to add a collaborator to your patchwork fork in Github, and then go on to the next section.
9. Pull Never Out Of Date
For this part of Git-it we're going to pull any changes that may have been made to the remote branch, and apply them to our local branch. Go to VCS / Git / Pull...
...and look at the dialog box that appears. The Current Branch should still be the branch you created in step 7. The next two sections map against the Git-it instructions;
<remotename>
reflects your choice in the Remote drop-down box, and <remotebranch>
is what you select in the Branches to merge section. Make your choices, and click Pull.Have a look in PyCharm at your
add-<your_github_username>
file - reporobot changed it while you weren't looking.10. Requesting You Pull Please
This next Git-it section is once again about work that you're doing in Github, not Pycharm, so there's no real difference - with one potential exception along the way. I'm unsure yet whether this is related to PyCharm or Github or Git-it's instructions, but if you get to the part where you're in the jlord/patchwork repository and Git-it says "Click the green 'New pull request' button' and there isn't one, then this should get you to the pull request dialog:
- Click on the grey 'New pull request' button
- Click the link to compare across forks
- For head fork, select your own fork of this repository; for compare, select the add-<username> branch that you created.
11. Merge Tada
In this last section of Git-it, you'll "put things back" - you're done working on your branch, so you'll merge everything back together, get rid of the branch, and pull the latest version from upstream.
First, move into the branch you want to merge into - in this case, the branch 'gh-pages' - by going to VCS / Git / Branches and selecting that local branch followed by Checkout.
Next, merge in that branch - by going to VCS / Git / Branches and selecting your feature branch that begins with 'add-' and clicking on Merge.
Tidy up by deleting your feature branch. Now that it's been merged you don't really need it around. Go to VCS / Git / Branches and delete the local branch that begins with 'add-' - and then delete the same branch from your remote on Github.
Finally, pull in the upstream changes so you are up to date (and have the latest version with your name on the website). Go to VCS / Git / Pull and change your Remote to upstream. You may need to click on the refresh button (to get branch names from the remote repository) - then select the upstream gh-pages branch and click on Pull.
Congratulations, you've finished the Git-it tutorial using PyCharm.
Part 1: Installing VMware and CentOS
Part 2: Expanding CentOS's Minimal Install
Part 3: Installing Python 3.6
Part 4: Linking PyCharm to Github via SSH
That was Part 5: Running through Git-It again, this time with PyCharm
No comments:
Post a Comment