Recently, I share with colleagues my knowledge of Git/GitHub. I gave a presentation in a team meeting, which covers the authentication and the basic functionalities of GitHub.
The authentication setting of GitHub in Git is not as easy as that in the GitHub Desktop App. Most beginners are overwhelmed by large numbers of relevant posts. Therefore, I list the only necessary official references that you can follow step by step to set up the authentication. Moreover, from my perspectives, I list the basic functionalities of GitHub as well as the corresponding daily Git commands. Lastly, I share some rules of thumb for collaboration in GitHub.
Authentication
You must authenticate before you can access certain resources on GitHub. When you authenticate to GitHub, you supply or confirm credentials that are unique to you to prove that you are exactly who you declare to be. The most two popular ways to authenticate to GitHub are
- A Combination of Username and Personal Access Token, and
- The SSH Protocol
Username and Token
Creating a personal access token: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-token
Using a token on the command line: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token#using-a-token-on-the-command-line
SSH
- Checking for existing SSH keys: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/checking-for-existing-ssh-keys
- Generating a new SSH key and adding it to the ssh-agent: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
- Adding a new SSH key to your GitHub account: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit.
Basic Functionalities
Adding an existing project to GitHub
https://docs.github.com/en/github/importing-your-projects-to-github/importing-source-code-to-github/adding-an-existing-project-to-github-using-the-command-line
Cloning a repository
https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository-from-github/cloning-a-repository
Branch: Checkout/Branch
- Create a new branch using
git branch <branch_name>
. - Switch to the new branch using
git checkout <branch_name>
.
Execute two command lines in one:
git checkout -b <branch_name>
Adding a file to a repository
https://docs.github.com/en/github/managing-files-in-a-repository/managing-files-using-the-command-line/adding-a-file-to-a-repository-using-the-command-line
Remove files from the working directory
- Remove files (e.g. trash.txt) from local directory manually.
git rm trash.txt
in Git Bash.- Commit the change using
git commit -m "Remove trash.txt"
.
Pull and Push
- Check the target remote repository on GitHub using
git remote -v
. - Pull the latest changes from GitHub with
git pull
. - Push your work to GitHub with
git push origin <branch_name>"
whereorigin
is referred to as the target remote repository.
Merge
Merge code changes in two different branches (target branch and to be merged branch)
- Checkout the target branch using
git checkout <target_bread>
. - Merge the changes from to-be-merged-branch into the target branch
using
git merge <to_be_merged_branch>
.
Revert and Reset
git revert <commit_ID>
will undo the changes in a
particular commit.
git reset
git commit --amend
Best Practices for Collaboration
- Always synchronize your branches before starting any work on your own.
- Make changes that are self-contained.
- Avoid having very large changes that modify a lot of different things. (Try to make changes as small as possible as long as they're self-contained.)
- When working on a big change, it makes sense to have a separate feature branch.
- Regularly merge changes made on the master branch back onto the feature branch.
- Have the latest version of the project in the master branch, and the stable version of the project on a separate branch.
- You shouldn't rebase changes that have been pushed to remote repos.
- Write commit messages.
- Only edit the same branch when necessary after submitting a pull request.
View / Make Comments