This document is a minimum tutorial for using git. It covers the most practical and fundamental usages of git, especially topics related to repositories and branches.
There are several things that need to be done in advance:
-
Create an account at GitHub
-
Get a computer, Mac/Linux/Windows
You can set up your email address and username locally or globally, which means setting them up for a single repository or for every repository on your computer.
-
Open Terminal.
-
Change the current working directory to the local repository where you want to configure the name that is associated with your Git commits.
-
Set your email address and username:
git config user.email "[email protected]"
git config user.name "John Snow"
- Confirm that you have set the Git username correctly:
git config user.email
git config user.name
-
Open Terminal.
-
Set your email address and username:
git config --global user.email "[email protected]"
git config --global user.name "John Snow"
- Confirm that you have set the Git username correctly:
git config --global user.email
git config --global user.name
A repository can be created in several ways:
You can create a repository on GitHub following the simple creation instructions. Do not forget initializing your repository with a README.
More than often, you have developed some parts of a project before you realize that you want to put it on GitHub. Is it a little late? Don't worry. You can put your files on GitHub anytime you want.
Suppose you have a folder like this
/hello
say_hello.py
Following the instructions below, you can create the repository you need.
-
Open terminal and change into the working directory
-
Initilize the local directory as a Git repository
$ git init
- Add the files to your new local repository
$ git add .
- Commit the files that you've staged in your local repository
$ git commit -m "Repository Created"
-
Create an empty repository on GitHub and copy the remote repository URL
-
In Terminal, add the URL for the remote repository where your local repository will be pushed
$ git remote add origin [paste copied URL here]
- Push the changes in your local repository to GitHub
$ git push -u origin master
When you connect to a GitHub repository from Git, you'll need to authenticate with GitHub using either HTTPS or SSH. Click this link if you are not farmiliar with these two kinds of connecting methods.
If you clone with HTTPS, you can cache your GitHub password in Git using a credential helper.
- Find out if Git and the osxkeychain helper are already installed:
git credential-osxkeychain
- Tell Git to use osxkeychain helper using the global credential.helper config:
git config --global credential.helper osxkeychain
The next time you clone an HTTPS URL that requires a password, you'll be prompted for your username and password, and to grant access to the OSX keychain. After you've done this, the username and password are stored in your keychain and you won't be required to type them in to Git again.
If you clone with SSH, you must generate SSH keys on each computer you use to push or pull from GitHub.
-
Open terminal
You can create or delete branches directly on GitHub, simply following the instructions.
- Create the branch on your local machine and switch in this branch
$ git checkout -b [new_branch_name]
- Push the branch on github :
$ git push origin [new_branch_name]
- Delete a branch on your local filesystem (checkout first):
$ git branch -d [branch_name]
- Delete the branch on github :
$ git push origin :[branch_name]
-
Create a repository from local files
-
Remove local files
-
Clone the repository using https
-
Set up your email and username locally
-
Authenticate the cloned repository using osxkeychain
-
Change some codes and push them to remote repository
-
Now that git has remembered your name and password, make another change and push it to remote repository
-
Create a new branch using terminal
-
Change some codes and push the change to remote repository using the new branch
-
Merge the change into master branch using the frontend of Github
-
Delete the created branch using terminal
Congratulations!!! You have succeeded in managing Git!
- Hong-Bo Deng - Initial work - Hong-Bo