Skip to content

Instantly share code, notes, and snippets.

@jmscarnatto
Last active August 4, 2025 00:02
Show Gist options
  • Save jmscarnatto/c4be90c7cda87664543984d995b47f61 to your computer and use it in GitHub Desktop.
Save jmscarnatto/c4be90c7cda87664543984d995b47f61 to your computer and use it in GitHub Desktop.
Git most frequently used commands

Really frequent mistake!! - Push when the remote has changed

git reset --soft HEAD~1  # this undoes the last commit and preserves last changes
git pull
git commit -m 'my erased last commit comments'
git push

Prompt for PR description creation

Act as an agent that generates raw markdown pull request descriptions following the structure below.  
Compare the current branch with a reference branch, analyze all differences, and generate a clear, professional, and technical summary.  

The output must be in raw markdown, enclosed in triple backticks (```), ready to copy and paste.

### Required structure:
1. **Overview**  
   - High-level summary of the purpose of this PR.  
   - Explain the feature, fix, or improvement in 2–3 sentences.  

2. **Changes**  
   - Group file changes into categories:  
     - ✨ **Added**: new files or major additions.  
     - 🔧 **Modified**: updated or refactored files.  
     - 🗑️ **Removed**: deleted files.  
   - Under each category, list the files changed and add short inline explanations if needed.

3. **How it works**  
   - Step-by-step explanation of the core logic or workflow introduced by this PR.  
   - Include how components interact and important flows.

4. **Deployment Notes**  
   - Mention configuration changes, env vars, migrations, or background jobs needed.  

5. **Impact**  
   - Explain how this change affects the system, workflows, or end-users.  

### Constraints:
- The tone must be professional and suitable for a technical audience.
- Summarize code differences accurately.
- Do not render markdown formatting; output raw markdown within triple backticks.

Now, analyze the diff between the current branch and reference branch {{reference_branch}}, and generate the PR description.

Squash commits with remote pushes

Scenario: You have pushed several commits to the staging environment and now want to squash them all into a single commit. Once you are satisfied with your branch and QA:

git log and copy the hash of the commit just before your first partial commit.
git rebase -i <the-hash>.

In the interactive rebase editor, replace pick with r for the top commit and s for the rest. Follow the instructions, removing any unnecessary information. Once finished, push your changes with

git push --force-with-lease

Merge parent without changing the branch

git fetch origin <parent>
git merge origin/<parent>

Conflicts! - Resolve accepting current changes (from remote)

git checkout <parent-branch-name> <conflicted-file-path>

Conflicts! - RUBY ON RAILS - Schema.rb - Resolve bringing the master version

rails db:rollback  #until it is matched with master
git checkout --theirs db/schema.rb
rails db:migrate
git add .... etc

Revert one single file

git log path/to/file
# get the version of the file from the given commit
git checkout <commit> path/to/file
git commit

Checkout (bring) a file or folder from other branch to current

git checkout -p <source-branch> -- <path-to-file-or-folder>
git checkout -p develop -- .circleci

Revert pushed .env - wtf!!

edit .gitignore file to add the line
    .env
git rm -r --cached .env
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
git push --force

Check how a branch|folder|file has grown

git diff HEAD~10 HEAD --stat lib/tasks/seed

Show current branch

git branch --show-current

GIT STASH

git stash
git stash pop
git stash show -p stash@{1}

CLONE SPECIFIC BRANCH

git clone -b <branchname> <remote-repo-url>
i.e.
git clone -b the-branch-name [email protected]:FakeCompany/reponame.git

WORKING IN A WRONG BRANCH

git stash
git checkout targetbranch
git stash apply

FIX COMMIT SHIT (some files need to be redone)

git log       
=> Copy the last good commit hash
git reset <CopiedHasH>
git status
git checkout <files>
=> start over

RETURN TO A PREVIOUS COMMIT AND BACK TO LAST

git log       
=> Copy the desired target commit hash
git checkout <CopiedHasH>
=> do wherever you want
git reflog # to see HEAD movements and copy <hash_to_return>
git reset --hard <hash_to_return>

LIST REMOTES BRANCHES

git branch -r
git checkout -b <new-local-branch> <remote-branch-to-clone>

RENAME BRANCH

git branch -m old_branch_name new_branch_name
git push origin :old-name new-name

RENEW MESSED LOCAL BRANCH / overwirite local branch with fresh remote one

git checkout .
git log       
=> Copy the last good commit hash
git reset --hard <CopiedHasH>
git pull

.. or just
git reset --hard HEAD~n

ERASE WORK IN PROGRESS

git reset --hard    # will reset tracked changes
git clean -fd       # will delete untracked files and folders

ADD+COMMIT IN ONE STEP

git commit -am "My comment"

AMEND

git commit --amend -m "new corrected message"
git commit --amend --no-edit

REBASE # --force flag permission required

git rebase -i <commit>^ #look at ^ symbol
=> set edit, drop or whatever in vim then save&exit :wq
=> do changes in files if any
git add <files>
git commit --amend
git rebase --continue
git push --force

REBASE PR (when you did all your work from a wrong base)

=> standing in <old-pushed-branch> with <wrong-base-branch> as base
git log
> copy the commit/s hash/es with your changes - clipboard <old-branch-commit-hash>
git checkout <right-branch>
git pull
git checkout -b <new-from-right-branch>
git cherry-pick <old-branch-commit-hash>
git push -f origin <new-from-right-branch>:<old-pushed-branch>

UNSTAGE SINGLE FILE

git reset HEAD <file_path> ## restore to working keeping changes

RESET SINGLE FILE

git reset -- my-file.txt   ## unstage this file
git checkout HEAD -- my-file.txt
git checkout @ -- myfile.ext
git checkout [commit ID] -- path/to/file

PICK A SINGLE FILE FROM OHTER BRANCH

git checkout <source-branch> path/filename

STATISTICS (added deleted)

git diff --numstat [remote repo/branch]

DIFF

# New files in a branch
git diff --name-status master..feature-branch-name | grep ^A

# Specific file or folder
git diff <reference-branch> <path/file.name>

# Specific *remotes* file or folder
git diff <remote-reference-branch> <remote-modified-branch> <path/file.name>
git diff origin/master origin/development app/lib/whatever/filename.rb

# Differences in GITHUB
# add `/compare` at the end of repository url

FORCE AN EMPTY COMMIT

git commit --allow-empty -am 'empty commit to force hooks'

USE DIFFERENT KEYS FOR DIFFERENT REPOSITORIES

git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/private-key-filename-for-this-repository -F /dev/null"

OVERRIDE COMMITER AUTHOR

git commit --author='Pepe Mujica <[email protected]>'

.gitignore DOES'NT WORK

git rm -r --cached .
git add .
git commit -m "untrack files contained in the .gitignore file"

Commit in the past

git commit --date='10 day ago' -m 'comment'
git commit --date='2020-03-20' -m 'comment'

REVERT LAST MERGE

git reset --merge HEAD~1

CHERRY-PICK

multiple commits
git cherry-pick hashA^..hashB  //(hashA commit included)
git cherry-pick hashA..hashB  //(hashA commit excluded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment