Tips on Version Control with Git
Created on: 11 Jul 21 05:26 +0700 by Son Nguyen Hoang in English
Tips on Git Version Control
While 99% of commands I used on Git is simple fecth, pull, add, commit and push, Git version control is much more powerful than what I think it is.
Many scenarios happends requires extensive knowledge on Git to make safe, proper action.
In the other hand, I am simply too lazy to command git help
everytime a bug occurs.
Therefore, I created this article to summarize some command and tips I think to be valuable and worthy to note down.
This article is under ongoing development hence the content may be updated and changed regularly
- Remove large files from Git comit
- Sometime you accidently commit a huge file and do not regconize it until you push it to the remote.
- Also, you don’t want to delete lastest commit as you already did so much changes already.
Solution
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
*Replace path_to_file with the path linked to the file you want to delete
- Remove commited file from Git
Scenario
- You commit & push a file (named X)
- You accidently changed your mind, that file is supposed to be in .gitignored (untracked file)
- You add the file to .gitignore, but now the file still exists on remote
- You feel extremely annoyed because of it. So, how to remote accidentally added file from Git?
Solution
- Run
git ls-files
to check the file you want to remove (X) - Run
git rm --cached <filename>
to remove the files. In this example, it could begit rm --cached fileX
. You can also apply some regex, such asgit rm --cached *.sln *.obj
to mask multiple files at the same time. - Run
git status
to confirm file had been deleted. - Commit & Push as usual. Don’t forget to check remote to see if it still stay there or not.
- Switch Remote Origin when there are diverges
Scenario
- Due to an unknown reason, the boss create a new Git repo (B) based on the original one (A). The two has different remote origin.
- However, by the time the new repo (B) be available for developer to push commit to, the new commit to (A) had been made. This creates diverges between A and B.
- Worse, the new repo B could be initialized by copy & paste. Which means that this and the former one had seperate, unrelated history.
So, how to migrate new changes from A to B safely?
Solution
- Clone B from the new remote.
- Literally copy & paste the souce code from A to B. Git will take a minute or two to refresh the index. Then, it will give us a list of changes in local as usual (in B).
- Discard the unwanted files and commit & push as normal.
- Update Local to Match with Remote
Solution:
git fetch origin
git reset origin --hard
- Fatal Error: The remote end hung up unexpectedly
Explanation and Solution
Sometime, this problem is caused because you push too much data to the remote. Usually, this happens when you push commits using HTTPS.
Consider to switch to SSH to avoid this problem.
- Delete the last “X” commit
Solution:
git reset --hard HEAD~[x] # x: number of last commit you want to delete
# To purge the commit on origin, you use the below command:
git push origin hard --force
- Delete All files/folder that accidently commited but newly-added in .gitignore
- You added/commited unwanted folder (Folder A)
- You push the commit
- You realized what you have done. You modify the .gitignore but it is too late!
- The wrong file is in remote now?
So how to remove every files/folders that de
Solution
Run
git rm -r --cached .