Skip to content

Instantly share code, notes, and snippets.

@RickCogley
Last active November 3, 2024 23:14
Show Gist options
  • Save RickCogley/c333df731696cb61c6a6886f63acb95c to your computer and use it in GitHub Desktop.
Save RickCogley/c333df731696cb61c6a6886f63acb95c to your computer and use it in GitHub Desktop.
Tips for git with cms

I have a situation where I have scripts pushing changes to a github repository on feature branches. What settings should I use to make the process as smooth as possible?

To ensure a smooth process for scripts pushing changes to a GitHub repository on feature branches, consider the following settings and best practices:

  1. Branch Protection Rules:

    • Enable branch protection rules to prevent direct pushes to the main branch. This ensures that all changes go through a pull request (PR) process.
    • Require status checks to pass before merging. This can include automated tests or other CI/CD checks.
  2. Commit Messages:

  3. Frequent Commits:

  4. Rebase Instead of Merge:

  5. Review Changes Before Pushing:

  6. Push Regularly:

  7. Use GitHub Actions:

  8. SSH Keys and Personal Access Tokens:

Implementing these practices will help maintain a clean and efficient workflow, making collaboration easier and more reliable. If you have any specific requirements or need further assistance, feel free to ask!

Source: Conversation with Copilot, 11/4/2024 (1) Best practices while pushing code to GitHub - DEV Community. https://dev.to/keshav___dev/best-practices-while-pushing-code-to-github-5a7e. (2) Most effective ways to push within GitHub Actions - Johtizen. https://joht.github.io/johtizen/build/2022/01/20/github-actions-push-into-repository.html. (3) How to Push Changes to a Remote Repository with Git Push - Squash. https://www.squash.io/how-to-push-changes-to-remote-repo-with-git-push/. (4) #7 Important Settings ​For GitHub Repository | Step By Step Details. https://www.youtube.com/watch?v=lc3yulAaP_g. (5) How to Create a new branch and push in existing git repository | BlueTek. https://www.youtube.com/watch?v=cF2d--zaAGY. (6) Creating Your First GitHub Repository and Pushing Code. https://www.youtube.com/watch?v=f26KI43FK58. (7) undefined. https://codingbeast.org/creating-a-new-github-repository-add-your-project/.


can I force the repository to always rebase?

Yes, you can set up your repository to always rebase when pulling changes. Here are a few steps to achieve this:

  1. Configure Git to Rebase by Default:

    • You can set Git to always rebase when pulling changes by configuring your global or local Git settings. Run the following command to set this globally:
      git config --global pull.rebase true
    • To set this for a specific repository, navigate to the repository and run:
      git config pull.rebase true
  2. Use Aliases for Convenience:

    • Create Git aliases to simplify the rebase process. Add the following to your .gitconfig file:
      [alias]
        pullr = pull --rebase
    • Now, you can use git pullr to pull with rebase.
  3. Rebase in CI/CD Pipelines:

    • If you use CI/CD pipelines, ensure that your scripts include rebase commands. For example, in a GitHub Actions workflow, you can add:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      
      - name: Rebase branch
        run: git pull --rebase origin main
  4. Force Push After Rebase:

    • After rebasing, you often need to force push to update the remote branch. Use:
      git push --force

By configuring these settings, you can streamline your workflow to always rebase, keeping your commit history clean and linear¹(https://graphite.dev/guides/git-rebase-force)²(https://blog.verslu.is/git/git-rebase/)³(https://websitehurdles.com/force-git-rebase/). If you have any specific scenarios or further questions, feel free to ask!

Source: Conversation with Copilot, 11/4/2024 (1) Force pushing after a Git rebase - Graphite.dev. https://graphite.dev/guides/git-rebase-force. (2) Git Rebase: Don't be Afraid of the Force (Push). https://blog.verslu.is/git/git-rebase/. (3) How to force a git rebase: Scenarios and Possible Risks. https://websitehurdles.com/force-git-rebase/. (4) Updating and rebasing branches - Renovate Bot. https://docs.renovatebot.com/updating-rebasing/. (5) undefined. https://commons.wikimedia.org/wiki/File:Mergevsrebase.png.


how can I setup in GitHub a user for the CMS to use but only allow the user to make a PR on main branch, so another user has to approve it.

To set up a GitHub user for your CMS that can only create pull requests (PRs) on the main branch, with another user required to approve them, follow these steps:

  1. Create a New GitHub User:

    • Create a new GitHub account for the CMS if you haven't already.
  2. Add the User to Your Repository:

    • Go to your repository on GitHub.
    • Navigate to Settings > Manage access.
    • Click Invite a collaborator and add the new user with Write access.
  3. Set Up Branch Protection Rules:

    • Go to Settings > Branches.
    • Under Branch protection rules, click Add rule.
    • Specify the branch name (e.g., main).
    • Enable Require pull request reviews before merging.
    • Optionally, specify the number of required reviewers.
  4. Restrict Direct Pushes to the Main Branch:

    • In the same branch protection rule, enable Restrict who can push to matching branches.
    • Add the users or teams who are allowed to push directly to the main branch (excluding the CMS user).
  5. Require Code Owner Reviews:

    • Create a CODEOWNERS file in your repository (usually in the .github directory).
    • Specify the users or teams who must review changes. For example:
      * @team-leads
      
    • This ensures that any changes to the main branch must be approved by a code owner.
  6. Automate PR Creation:

    • Ensure your CMS is configured to create PRs instead of pushing directly to the main branch. This can usually be done by setting the target branch for changes.

By following these steps, the CMS user will only be able to create PRs on the main branch, and another user will need to approve these PRs before they can be merged¹(https://github.com/orgs/community/discussions/23765)²(https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request)³(https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule). This setup helps maintain code quality and ensures that all changes are reviewed.

If you need more detailed guidance on any of these steps, feel free to ask!

Source: Conversation with Copilot, 11/4/2024 (1) Allow only specific people or team to approve a PR - GitHub. https://github.com/orgs/community/discussions/23765. (2) Creating a pull request - GitHub Docs. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request. (3) Managing a branch protection rule - GitHub Docs. https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule.

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