Github Quick Tricks— Dev
2 min read
Remove a file from a pushed commit
Sometimes you may accidentally commit changes to a file and push it to GitHub. Because this file wasn't supposed to be modified and committed, you may be asked to remove the file from your commit or pull request.
- Go to your git directory and
cdto the location of the file you want to remove from your commit
git checkout origin/develop package-lock.json
package-lock.jsonis the name of the file you want to revert changes to
origin/developis the name of the remote branch on github you want your file to revert back to
- By doing this, your file no longer has any differences with the branch you are looking to merge onto
git commit -m "Removes package-lock.json"
Reset to a branch from remote
Sometimes you just want to reset your working branch back to how things were when they were last pushed up to GitHub
1git reset --hard origin/NAME-OF-BRANCH
Rebasing the base branch
Let's say you are working on a new feature
A in branch
1develop: --1--2--4--6--72featA: \--3--5
But then, you need to begin working on feature
B which builds on top of feature
A, before feature
A has been merged into develop.
So, you create a new branch
featB based on
1develop: --1--2--4--6--72featA: \--3--53featB: \--8
featA has new commits, you can simply rebase them onto
While you are on the
featB branch run
1git rebase featA
1develop: --1--2--4--6--72featA: \--3--5--93featB: \--8
featA is complete and gets squashed and merged into
1develop: --1--2--4--6--7--102featA: \--3--5--9----/3featB: \--8
featA gets squashed and merged, it is often safely deleted.
1develop: --1--2--4--6--7--102featA:3featB: \--3--5--9--8
Now, you'll want to rebase
featB and make it based off of
develop instead of
featA since all that work has been merged in.
Notice how the commits with hash
featB are redundant since they have already been squashed and merged into commit with hash
10. This means we can safely drop commits
9 as long as
featB is based off of commit
10 (or later) on the develop branch. The new work is on commit
8 and later.
featB branch, run the below command. (Replace
3 with the appropriate commit hash for your branch).
1git rebase -i 3
You'll get something like this:
1pick 3 commit message2pick 5 commit message3pick 9 commit message4pick 8 commit message
Edit and save the file to something like this:
1d 3 commit message2d 5 commit message3d 9 commit message4b5pick 8 commit message
d command is short for
drop. This means we will drop that commit and lose it. Since commits
9 are merged and squashed into commit
10 (on develop), this is fine.
b command is short for
break. This pauses the interactive rebase at this step.
When the interactive rebase stops at the
break, we will merge in our work from the develop branch.
1git merge origin/develop
There shouldn't be any merge conflicts at this point because we haven't applied any new work on top of this detached branch.
Then, continue our rebase with the new work we did on
1git rebase --continue
featB branch should be rebased off of
1develop: --1--2--4--6--7--102featB: \--8
Check if everything is working!! Then, force push this up to your remote repository.
1git push --force
If you mess up trying to rebase
featB follow these steps:
- Checkout to another branch. Ex
git checkout develop
- Delete your local
git branch -D featB
- Pull down the remote
git checkout -b origin/featB