Install Git
sudo apt-get install gitCheck git version
git versionNOTE: Refer to git download on other platform.
Setup Git with Github
Create github account.
Setup username and email.
git config --global user.name "github_username"git config --global user.email "github_email"Check configuration
git config --listEdit configuration
git config --edit --globalAdd SSH key to github
Check if you already have a local SSH key. If yes, ~/.ssh/id_rsa should exist.
ls -al ~/.sshIf you don't have a local SSH key, generate one.
ssh-keygen -t rsa -b 4096 -C "any identifier: email, etc."NOTE: I would recommend to use a passphrase
Add SSH key to ssh-agent
ssh-add ~/.ssh/id_rsaList all ssh-agent keys.
ssh-add -lCopy content of ~/.ssh/id_rsa.pub
sudo apt-get install xclipxclip -sel clip < ~/.ssh/id_rsa.pubPaste SSH key to Github. Visit Github, click to top right user profile icon, then Settings -> SSH and GPG keys -> New SSH Keys.
NOTE: Refer to Connecting to GitHub with SSH.
Create New Github Repository
Visit Github, click on the top right + icon (left of user profile icon) and select New Repository. Fill in the following:
- Name: e.g.
git-test - Description: optional
- Public/Private
- Initialize this repository with a README (checked this if you haven't created the project files on your computer yet)
- Add .gitignore: e.g. select
pythonif you are doing a python project - Add license: if this is a public project, 3 common choices are
Apache License 2.0,GNU General Public License v3.0,MIT License. For private project, you can selectNone.
NOTE: Refer to Which License for Sample Code for license reference.
Checkout a Github Repository
Download/Clone the Github repository to a local directory.
Run the following in your workspace directory.
git clone [email protected]:USERNAME/git-test.gitNOTE: The above will create git-test directory in the current directory.
NOTE: You can rename git-test directory with git clone [email protected]:USERNAME/git-test.git NEW_DIR_NAME.
NOTE: If you prefer to create a local git repository without cloning existing remote repository, use git init.
Add, Commit & Push
- Add: start tracking a file
- Commit: commit to local repository (revision log created)
- Push: push changes to remote repository (e.g. Github)
Check current git status
git statusAdd new file
git add test.pyAdd all files (new, modified, deleted).
git add -ANOTE: git add --all or git add . has the same effect.
Commit all tracked files.
git commit -m "Commit #1"NOTE: Comment/Message for commit are mandatory. You can use git commit -m. (. as message) or git commit -am'save' (save as message).
NOTE: Some might use git commit -am 'message' to combine add and commit, but this only affect files that have been modified and deleted, but doesn't affect new files.
Push all committed changes to server.
git push origin masterBranch
Sometimes you want to work on a big feature which might take more than one day, yet you still want access to the original source (before modification) still be accessible and stable.
Create a new branch and switch to the branch.
git checkout -b sprint-01Show available branch and current branch.
git branch master
* sprint-01Push branch to remote server (e.g. Github)
git push origin sprint-01Switch branch
git checkout masterDelete branch
git branch -d sprint-01Merge
Once your work on the branch is completed and tested, you can merge it with the master.
Assuming you are working in the branch (e.g. sprint-01). You can check your current branch
git branchCheck diff with master (assuming you are still in sprint-01 branch).
git diff masterMerge current branch with master
git checkout master # Switch to `master` branchgit pull # Optional: get latest update from remote repositorygit merge sprint-01 # Merge `master` with `sprint-01`git push origin master # Update merged `master` to remote repositoryTag
Tag is usually used for release. It is like a save point, where we can refer back to this point for deployment or testing.
Create tag.
git tag -a 0.1 -m "Message for 0.1"Update tag to remote repository.
git push origin 0.1Show created tags.
git tagDelete tag
git tag -d 0.1 Conclustion
This is a very basic guide suited for single user.
Git is quite simple to get started, but hard to master especially when there are more users in a single project.
References: