Git shortcuts

LiveRunGrow
1 min readApr 27, 2020

--

Case 1: Editing commit message that has been pushed to a remote repository

git commit --amend -m "NEW MESSAGE COMMIT"
git push --force-with-lease origin <branch>

Case 2: Discard all local changes and rebase to whatever that is on the remote repository

git fetch origin
git reset --hard origin/<branchname>

Case 3: Discard changes and change branch. When the computer asks you to do a git stash

git checkout .

Case 4: Add a new entry to .gitignore and remove the file from pushed commit.

git rm -r --cached . 
git add .
git commit -am "Remove ignored files"

Case 5: Remove files already in committed

git reset --soft HEAD~1

Case 5: Pull new changes from forked repository

1. Clone your fork:

git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream

3. Updating your fork from original repo to keep up with their changes:

git pull upstream master_branch_name

--

--

LiveRunGrow

𓆉︎ 𝙳𝚛𝚎𝚊𝚖𝚎𝚛 🪴𝙲𝚛𝚎𝚊𝚝𝚘𝚛 👩‍💻𝚂𝚘𝚏𝚝𝚠𝚊𝚛𝚎 𝚎𝚗𝚐𝚒𝚗𝚎𝚎𝚛 ☻ I write & reflect weekly about software engineering, my life and books. Ŧ๏ɭɭ๏ฬ ๓є!