Common Git Workflows

Quick-reference guide for Git processes I use regularly.

Pushing Changes to a Branch

To push new changes for a specific branch to the remote repository:

git push origin <branch-name>

Combine New Changes with the Last Commit (Amend)

To add new changes to your last commit without creating a separate one:

  1. Stage your changes:

    git add .
    
  2. Amend the last commit:

    git commit --amend
    

<aside> ⭐

Note: If already pushed, force-push with git push --force (careful if others use the branch).

</aside>

Save Git Diff of a Commit to a File

To save the changes introduced by a specific commit to a file:

  1. Run the git diff command for the commit, redirecting output to a file:

    git diff <previous-commit-hash> <commit-hash> > diff_file.patch
    

    Example:

    git diff abc1234 def5678 > commit_def5678_diff.patch
    

<aside> ⭐

Note: Replace <previous-commit-hash> with the parent commit and <commit-hash> with the target commit.

</aside>

Understanding previous-commit-hash and commit-hash in git diff

When using git diff <previous-commit-hash> <commit-hash> > diff_file.patch, the two commit hashes serve distinct purposes: