Skip to content

Instantly share code, notes, and snippets.

@troyharvey
Last active June 28, 2022 22:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save troyharvey/1dc6acae0ec29172caec9e54634587a1 to your computer and use it in GitHub Desktop.
Save troyharvey/1dc6acae0ec29172caec9e54634587a1 to your computer and use it in GitHub Desktop.
A brief description of how I like to use git

My git Workflow

This is just one way of using git/GitHub. But I have been using this same basic flow for 10 years and I rarely have trouble.

  1. Checkout the main branch

     git checkout main
    
  2. Pull the most recent version of main from GitHub

     git pull origin main
    
  3. Checkout a new branch to begin my work

     git checkout -b th/some-new-work-i-am-starting
    
  4. Add the new work to the staging area

     git add .
    
  5. Check the work before committing it to my new branch

     git diff --staged
    
  6. Commit the work to the branch

     git commit --message="A brief message explaining what I'm changing"
    
  7. Push the branch to GitHub

     git push origin th/some-new-work-i-am-starting
    
  8. Open a draft Pull Request on GitHub for the new branch so my team can see what I'm working on

  9. Flag the Pull Request as "Ready for Review"

When my branch cannot be merged into main

Sometimes you will need to deal with merge conflicts. Here's how I do that.

  1. Checkout the branch that cannot be merged with main.

     git checkout th/some-branch-with-a-problem
    
  2. Fetch the latest changes to the main branch.

     git fetch
    
  3. Rebase the branch with the issue.

     git rebase origin/main
    
  4. Usually, git tells you that the rebase is incomplete because of a conflict. Resolve conflicts by opening each file that is flagged with CONFLICT in the error message from git.

  5. One by one, find the sections of the file where the conflict occurred and edit the file - removing the old changes and keeping the correct changes.

  6. After all the files with conflicts are repaired, add the files to the staging area to tell git that the conflicts are resolved.

     git add .
    
  7. Now continue the rebase process.

     git rebase --continue
    
  8. If the rebase completes without any CONFLICT messages, push the rebased branch up to GitHub and the merge conflict on GitHub should disappear.

     git push origin th/some-branch-with-a-problem -f
    

Stashing... error: Your local changes to the following files would be overwritten by checkout

Sometimes git will throw this error when you are trying to pull (or checkout) a branch but you have made some changes to files locally without committing those changes. git stash is useful here.

  1. When you see an error like this...

     git checkout main
    
     	error: Your local changes to the following files would be overwritten by checkout:
     	README.md
     	Please, commit your changes or stash them before you can switch branches.
     	Aborting
    
  2. Add the files you've changed to the staging area

     git add .
    
  3. Stash the changes

     git stash
    
  4. Try the checkout command again

     git checkout main
    
  5. Bring the stashed changes back

     git stash apply
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment