While knowing how to use Git on the command line will always be useful since the full power of Git and its customizations and flexibilty is designed for use with the command line, Github also provides Github Desktop as an graphical interface to do basic git commands; you can do all of the basic functions of Git using this desktop app. Feel free to use this as an alternative to Git on the command line if you prefer.
Branches allow you to keep track of multiple versions of your work simultaneously, and you can easily switch between versions and merge branches together once you've finished working on a section and want it to join the rest of your code. Here are some cases when it may be a good idea to branch:
A detailed tutorial on Git Branching can be found here. You can also find instructions on how to handle merge conflicts when joining branches together.
A standard workflow when starting on a new project and contributing code looks like this:
Command | Description |
---|---|
SETUP: FIRST TIME ONLY: git clone <url> <directory_name> |
Clone the repo. This copies of all the project files in its current state on Github to your local computer. |
1. git pull origin master |
update the state of your files to match the most current version on GitHub |
2. git checkout -b <new_branch_name> |
create new branch that you'll be working on and go to it |
3. Make some file changes | work on your feature/implementation |
4. git add -p |
add changes to stage for commit, going through changes line by line |
5. git commit -m <commit message> |
commit files with a message |
6. git push -u origin <branch_name> |
push branch to remote and set to track (-u only needed if this is first push) |
7. Repeat step 4-5. | work and commit often |
8. git push |
push work to remote branch for others to view |
9. Follow the link given from the git push command to submit a pull request (PR) on GitHub online |
PR merges in work from your branch into master |
(10.) Your changes and PR get approved, your reviewer deletes your remote branch upon merging | |
11. git fetch --all --prune |
clean up your local git by untracking deleted remote branches |
Other helpful commands are listed below.
Command | Description |
---|---|
git clone <url> <directory_name> |
clone a repository, only needs to be done the first time |
git pull origin master |
pull from master before making any changes |
git branch |
check what branch you are on |
git branch -a |
check what branch you are on + all remote branches |
git checkout -b <new_branch_name> |
create new branch and go to it (only necessary when you create a new branch) |
git checkout <branch name> |
switch to branch |
git add <file name> |
add file to stage for commit |
git add -p |
adds changes to commit, showing you changes one by one |
git commit -m <commit message> |
commit file with a message |
git push -u origin <branch_name> |
push branch to remote and set to track (-u only works if this is first push) |
git branch --set-upstream-to origin <branch_name> |
set upstream to origin/<branch_name> (use if you forgot -u on first push) |
git push origin <branch_name> |
push work to branch |
git checkout <branch_name> \ git merge master |
switch to branch and merge changes from master into <branch_name> (two commands) |
git merge <branch_name> master |
switch to branch and merge changes from master into <branch_name> (one command) |
git checkout --track origin/<branch_name> |
pulls a remote branch and creates a local branch to track it (use when trying to pull someone else’s branch onto your local computer) |
git push --delete <remote_name> <branch_name> |
delete remote branch |
git branch -d <branch_name> |
deletes local branch, -D to force |
git fetch --all --prune |
untrack deleted remote branches |