Skip to content

Instantly share code, notes, and snippets.

@ByteJoseph
Created November 14, 2024 03:13
Show Gist options
  • Save ByteJoseph/f4e6a8ece1d58650eee7c85af08e34e2 to your computer and use it in GitHub Desktop.
Save ByteJoseph/f4e6a8ece1d58650eee7c85af08e34e2 to your computer and use it in GitHub Desktop.
Quick GitHub Commands

Quick Bash Commands for Git Troubleshooting

1. Undo the Last Commit (Keep Changes)

git reset --soft HEAD~1

Explanation: Reverts the most recent commit but keeps your changes in the working directory, letting you make adjustments before re-committing.

2. Discard Local Changes (Not Committed)

git checkout -- <file>

Explanation: Reverts uncommitted changes in the specified file back to the last committed state. Replace <file> with . to discard all changes.

3. Delete a Local Branch

git branch -d branch_name

Explanation: Deletes the local branch branch_name if it has been merged. Use -D to force delete.

4. Recover a Deleted Branch

git reflog

git checkout -b branch_name <commit_hash>

Explanation: Finds the last commit on the deleted branch with reflog, then recreates the branch using the commit hash.

5. Amend the Last Commit (Message or Content)

git commit --amend

Explanation: Opens the last commit in your editor to change the commit message or add new changes to it.

6. See All Local Changes

git diff

Explanation: Displays differences between your working directory and the last commit, showing what has been changed.

7. Remove a File from Git (Keep Locally)

git rm --cached <file>

Explanation: Stops tracking <file> in Git, but keeps it in your local directory.

8. Force Pull to Overwrite Local Changes

git fetch origin

git reset --hard origin/main

Explanation: Syncs your local branch with the remote version, discarding all local changes.

9. Merge Only Specific Files from Another Branch

git checkout branch_name -- <file>

Explanation: Pulls only the specified file from branch_name into your current branch.

10. Show All Commits by a Specific Author

git log --author="Author Name"

Explanation: Filters commit history to show only commits by the specified author.

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