Categorised in: Web Development
I constantly find myself Googling similar questions about git on the command line even though I’ve been using it for years.
This post will be a write-up of some common questions along with their answers when I next search for them. I’ll also have a look at what everyone else seems to be searching for and drop some of the answers to those questions in here too.
99% of the time I use the command line version of git (mainly out of habit) so these answers will be specific to that.
How to revert the last git commit on a git repository
The first thing you need to do on this one is figure out the hash of the commit you want to revert.
You can do this in GitHub or from the command line we can see the recent commits by using
and you’ll see something like this.
You can see the hash in the yellow there after the word commit. The commits here are in descending order.
To revert that commit we need run the revert command and get the changes committed and pushed the repo.
git revert [your-commit-hash] git commit -m "Revert the last commit" git push origin main
Behind the scenes, this creates a new commit that will undo the changes in that commit
How to change master to main in git
A few years back there was a movement by tech companies to remove divisive language from a lot of their software. GitHub was one of those companies and the word master was removed in favour of main.
For existing repositories, there was a need to change that manually.
Create a new branch named
main and check it out of git.
git branch main git checkout main
We then need to merge the old master branch into the new main branch and then update what should be the default branch
git merge master git remote set-head origin main
Now we can delete the old master branch
git branch -d master
Finally we need to push the changes up with
git push origin main --set-upstream
If you have any hosting or CI tools that were pointing to or monitoring changes on that specific branch, you’ll need to update those too.
Finally, don’t forget to tell any other contributors on your repo. They’ll need to fetch and pull all of the new repo changes.
How to untrack files in git
There are two ways you can do this.
Firstly you can add the file location or file pattern to your
# ignore a specific file /folder/sub-folder/filename.txt # ignore all text files *.txt
If you want to completely remove the file from the git index you need to run a few commands.
git rm --cached folder/sub-folder/filename.txt git commit -m "Removing filename.txt" git push origin [your-branch-name]
This will still keep the file within your local file though.
You can either move this to another folder or delete this if it is not needed.
How to create a folder in a git repository
For this, I’m assuming that you want to commit an empty folder in git. Git doesn't seem to allow you to add empty folders into repositories.
The way to get around this is to create a hidden file within the folder called
.gitkeep, notice there is a period at the start.
You can either do that from within your development environment or in the command line by changing the directory to the folder you want to keep and running
Then it's a case of committing those changes to the repo.