1. 程式人生 > >GitHub Primer for Dummies

GitHub Primer for Dummies

Unfortunately, clicking create repository is just the first step in this process (spoiler: it doesn’t actually create your repo). The next step involves using your terminal to initialize your Git and push your first commit. Git is not the same thing as GitHub, although they are related. Git is a revision control system that helps manage source code history and edits, while GitHub is a website that hosts Git repositories. In layman’s terms, Git takes a picture of your project at the time of each commit and stores a reference to that exact state. To initialize the Git for your project, use terminal to enter the directory on your computer where it is stored and enter git init

into the command line. Type git add FILENAME to upload your first file. The next step is making your first commit, or revision. Enter git commit -m "your comment here" into the command line. The comment should provide, in short detail, what changes were made so that you can more easily track your revisions. The commit adds changes to the local repository, but does not push the edits to the remote server. The next step is to type into the command line to create the remote server on GitHub that will host your work. Finally, enter git push -u origin master
to push the revisions to the remote server and save your work.

Adding Files to Repository

The process for adding changes to your GitHub repo is similar to the initialization process. You can choose to add all the files in your project directory in one fell swoop, or add each file individually as edits are made. For a multitude of reasons, discovered through trial and error, I highly recommend pushing each file individually. First, it will keep your repository clean and organized, which is useful when providing links to your GitHub profile/repo on LinkedIn, resumes, or job applications. Second, this will allow you to track changes to each file separately, rather than pushing up a vague commit description. Third, it will prevent you from accidentally pushing files that were not meant to be added to your repo. This can be files containing personal information, such as API keys, that can be harmful if posted to a public domain. It will also prevent you from uploading datasets that exceed 100mb, which is the size limit for free accounts. Once a file is added to the repository, it is extremely difficult to remove, even if it has not yet been pushed or committed. Speaking from experience, I have had to delete a repository on numerous occasions after accidentally uploading a file that I didn’t want, so I stress the importance of carefully selecting which files to upload.

Vim interface

To add a new file, enter your project directory via terminal and type git add FILENAME into the command line. To make a commit, there are two options: you can follow the same process as creating a repo and type git commit -m "commit description”, or use Vim, a unix based text editor to process the changes. Vim is a counterintuitive text editor that only responds to the keyboard (no mouse), but provides multiple keyboard shortcuts that can be reconfigured, and the option to create new, personalized shortcuts. To enter the Vim text editor, type git commit into the command line and press enter. This brings you to the Vim editor; to proceed to writing your commit, type i to enter --INSERT-- mode, and then type in your commit message. Once finished, press esc to exit --INSERT-- mode, and then save and exit Vim by entering :wq to write and quit the text editor. From there, all you need to do is enter git push into the command line to push your changes to GitHub.

Git Ignore

To ignore certain files when pushing to a repo, you can create a .gitignore file that specifies intentionally untracked files to ignore. To create the file, click on the new file button on your repository homepage and name the file .gitignore, or use one of the sample templates provided. There are multiple ways to specify a file or folder to ignore. The first way is to simple write the name of the file in the .gitignore file. For example, if you have a file called AWS-API-KEY-DO-NOT-STEAL.py, you can write the name of that file, with the extension, in the .gitignore file.

Creating a .gitignore file on GitHub

To ignore all filenames with a certain extension, say .txt files, type *.txt into the .gitignore file. Lastly, you can ignore an entire folder by typing folder_name/ in the file. Once you have added all of the files you want to be ignored to the .gitignore file, save it and put it in the root folder of your project. Now, if you try to add and push those files to the repository, they will be ignored and not included in the repository. However, if the files were already added to the repo before being added to the .gitignore file, they will still be visible in the Git repo.

Forks and Branches

If you have used GitHub before, or are familiar with the lingo, you have probably seen the terms Fork, Branch and Merge been tossed around. A fork is essentially a clone or the repository. Forking someone else’s repository will create a new copy under your profile that is completely independent of the original repository. This is useful in the case where the original repository is deleted — your fork will remain, along with the repository and all of its contents. To fork a repository, simply visit the repo page and click the Fork button on the top right of the page. To overwrite a current fork with an updated repository, a user can use the git stash command in the forked directory before forking the revised repo.

A branch provides another way of diverging from the main code line of a repository. Branching a repository adds another level to the repo that remains part of the original repository. Branches are useful for long-term projects or projects with multiple collaborators that have multiple stages of the workflow that are at different stages. For example, if you are building an app, you might have the skateboard and one key feature ready but are still working on two additional features that are not ready to launch. You can create an additional branch, leaving only the finished product in the Master branch, while the two work-in-progress features can remain undeployed in a separate branch. A branch is also useful when working with a team — each member can be working on a different branch, so when they push changes, it does not overwrite files that another team member is working on. This provides an easy way to keep each individual’s work separate until it is ready to be merged and deployed.

Microsoft rolls out automated GitHub support chatbot

Branches can be locally created from your terminal as long as you have a cloned version of the repository saved locally. To see all of the branches in your repo, type git branch into the command line from within your project directory. If no branches have been created, the output should be *master, with the asterisk indicating the branch is currently active. To create a new branch, type git branch <new_branch_name>, and then enter git checkout <new_branch_name> to switch to the new branch so you can work from it. The git checkout command lets the user navigate between different branches of a repository. Committing changes to a branch follows the same process as committing to the Master, just be sure to stay aware of which branch you are working in.