Git & GitHub Workflow

Beginning a new project

  1. git init
  2. git add -A
  3. git commit -m "My Message"
  4. Create GitHub repo
  5. Make sure ssh is checked and copy commands from GitHub to add remote repo to local project
  1. mkdir my-directory-name
  2. cd my-directory-name
  3. touch my-filename
  4. git init
  5. git add -A
  6. git commit -m "My Message"
  7. Create GitHub repo
  8. Make sure ssh is checked and copy commands from GitHub to add remote repo to local project
  1. Fork project
  2. Choose only main branch or additional topic branches to be forked
  3. Copy ssh url from my repo
  4. Navigate in the command line to where directory will be added
  5. git clone ssh url from my repo
  6. cd my-project-name
  1. cd my-directory-name
  2. touch my-filename
  3. git init
  4. git add -A
  5. git commit -m "My Message"
  6. Create GitHub repo
  7. Make sure ssh is checked and copy commands from GitHub to add remote repo to local project

Existing projects

  1. git add -A
  2. git commit -m "My Message"
  3. git push origin my-branch-name
  1. Add and commit all changes before switching branches
    • git add -A
    • git commit -m "My Message"
  2. git checkout -b my-topic-branch-name
  1. Add and commit all changes before switching branches
    • git add -A
    • git commit -m "My Message"
  2. git checkout main
  3. git merge my-topic-branch-name -m "My Message"

Working with a team

  1. Fork project
  2. Choose only main branch or additional topic branches to be forked
  3. Copy ssh url from my repo
  4. Navigate in the command line to where directory will be added
  5. git clone ssh url from my repo
  6. cd my-project-name
  7. Create a topic branch
    • git checkout -b my-topic-branch-name
  8. Work on the topic branch
  9. git add -A
  10. git commit -m "My Message"
  11. Merge work into main branch
    • git checkout main
    • git merge my-topic-branch-name -m "My Message"
  1. Connect to the upstream repo
    • git remote add upstream ssh url from upstream repo
  2. Fetch from upsteam branch
    • git fetch upstream
  3. Checkout main branch
    • git checkout main
  4. Merge upstream main branch into local main branch
    • git merge upstream/main
  5. Checkout topic branch
    • git checkout my-topic-branch
  6. Merge main branch into topic branch
    • git merge main -m "My Message"
  1. git fetch upstream
  2. git checkout main
  3. git merge upstream/main
  4. git checkout my-topic-branch
  5. git merge main -m "My Message"
  6. Work on the topic branch
  7. git add -A
  8. git commit -m "My Message"
  9. Merge work into main branch
    • git checkout main
    • git merge my-topic-branch-name -m "My Message"
  1. All code has been added, committed, and merged into the main branch
  2. Push main branch to GitHub remote repo
    • git push origin main
  3. Issue pull request
    • Click the "Pull Request" button
    • Click the "Create Pull Request" button
    • Click the "New Pull Request" button
    • Choose which branch you want to compare
    • Check the git diff to be sure the changes are what you want
    • Click the "Create Pull Request" button

Troubleshooting

  1. What directory am I in?
    • pwd
  2. Have my changes been added and committed?
    • git status
  3. What remote repos have been added to my directory?
    • git remote -v
  1. Read the error message
  2. Check for a typo
  3. Google the error message
  4. Be very careful if the command is to remove files or folders. Something removed in the command line is gone-gone! 😰
  1. Resolve merge conflicts in the text editor
  2. Remove all conflict markers
  3. git add -A
  4. git commit -m "My Message"
  1. If you're working on a topic branch, you can add and commit the changes, switch to the main branch without merging, and then delete the topic branch. 😄
  2. If you need to remove one or more commits, you can use git reset. Start by listing the commits so you can see the commit hash. Then reset the Head to the last commit you want to keep
    • git log -3
    • git reset --hard commit hash you want to keep
    • git push origin my-branch-name --force
    • Be very careful with git reset and git push --force. These commands will permanently remove code from the branch history. 😰 For safety, consider creating a backup branch before using these commands.
  1. There are times you want to save your work without committing it. Stashing your code helps to keep your commit history clean and relevant.
    • git stash
  2. Create a separate branch with you stashed code. Also clears your stash list.
    • git stash branch my-new-branch-name

Resources