Showing posts with label git for beginners. Show all posts
Showing posts with label git for beginners. Show all posts

Monday, 26 January 2026

Git: Cleaning up

Sometimes, our IDE can make a mess of things (mostly because we did something wrong, though).

And in order to fix it, sometimes we have to get rid of all unversioned files, so that we have a clean git repository and then create a new Project in our IDE.

This helps as it forces the IDE to recreate its config files from scratch.

This blog is just a small note on how to clean your git repository of all unversioned files.

A dry run would look like this:

git clean -n -x -d

A proper clean would look like this:

git clean -d -x

Options:

-n
dry run
-d
also recursively delete unversioned directories.
-x
ignore .git-ignore rules (convenient for cleaning up buildproducts and IDE config files)

Output of a dry run would look something like this:

% git clean -n -x -d
Would remove .DS_Store
Would remove .idea/
Would remove mrbear-parent/mrbear-app/target/
Would remove mrbear-stubs/.gradle/
Would remove mrbear-stubs/.kotlin/
Would remove mrbear-stubs/build/

See also "git restore" or "git reset".

References

git-clean - Remove untracked files from the working tree
https://git-scm.com/docs/git-clean

Sunday, 11 January 2026

Trying out the Github Command Line

So it's possible and quite convenient to use hardcode git commands to checkout Github repositories. It works fine, once you get the SSH keys sorted out and all that.

I tend to use IntelliJ to checkout new github repositories, as it is soo much easier to point and click.

This always happens to me, when I find myself having to do things I don't do often. Because once you have a repo checked out, that's basically the end of all the complicated stuff and you just get to work.

Checking out using Git

I still prefer using Git to checkout a new repository for now4.

The way this works is just do "git clone git@github.com:username/repo.git". You can find this url in the repository on the GitHub website.

However, an ssh key must be available on your GitHub account for the computer/client you're using.

First generate such an ssh key on your computer, using reference [5].

For example: "ssh-keygen -t ed25519 -C "mrbear@mrbear.com"".

Add your ssh key to the ssh-useragent, using "ssh-add ~/.ssh/id_ed25519".

You should see:

$ ssh-add ~/.ssh/id_ed25519
Enter passphrase for /home/mmrbear/.ssh/id_ed25519:
Identity added: /home/mrbear/.ssh/id_ed25519 (mrbear@mrbear.com)

Then you can add the generated key to your Github account using the website, using reference [6].

After that, you can finally just clone your repo as mentioned above.

Installation

GitHub has a command line interface nowadays1. It's easy to install2.

I'm using Fedora, so all it takes on my end is to "sudo dnf install gh".

There used to be a tool called "hub", apparently, which is basically a git wrapper for GitHub. It was an unofficial tool. You can find the differences discusses in [3].

Using the CLI

Logging in using "gh auth login".

$ gh auth login
? Where do you use GitHub? GitHub.com
? What is your preferred protocol for Git operations on this host? SSH
? Upload your SSH public key to your GitHub account? Skip
? How would you like to authenticate GitHub CLI? Login with a web browser

! First copy your one-time code: DFGF-JFDB
Press Enter to open https://github.com/login/device in your browser...
✓ Authentication complete.
- gh config set -h github.com git_protocol ssh
✓ Configured git protocol
✓ Logged in as mrbear

Then the command "gh issue list" shows me open issues.

Works! Quite nice!

References

[1] GitHub - Take GitHub to the command line
https://cli.github.com/
[2] Installing gh on Linux and BSD
https://github.com/cli/cli/blob/trunk/docs/install_linux.md
[3] GitHub - GitHub CLI & hub
https://github.com/cli/cli/blob/trunk/docs/gh-vs-hub.md
[4] GitHub - Cloning a repository
https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository
[5] GitHub - Generating a new SSH key and adding it to the ssh-agent
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
[6] GitHub - Adding a new SSH key to your account
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

Friday, 12 December 2025

Git: Deleting old local branches

Just looking at old local branches. They tend to proliferate, especially if you have a release cadence.

To list the local branches (the default), or list the branches according to a pattern, see the two examples directly below.

git branch
git branch --list "V2022*"

Small explanation of the output of the command:

  • existing branches are listed
  • the current branch will be highlighted in green and marked with an asterisk
  • any branches checked out in linked worktrees will be highlighted in cyan and marked with a plus sign

I'm usually only interested in local branches, but "-r" shows remote-tracking branches and "-a" shows both remote and local branches, if you're interested.

Removing branches

git branch -D `git branch --list "V2022*"`

Bear in mind that branches that are used by a worktree cannot be deleted. Remove the worktree first.

Actually, I really like that behaviour.

Checking old branches

Apparently you can sort the list of branches based on last comitterdate.

git branch --sort=-committerdate # DESC

In the example above, you'll see branches that have been committed to recently at the top.

Finding a commit

This is nice, you can find which branches contain a certain commit quickly.

git branch --list --contains 089aafb331a08d19ed805fff6fea3846776980a0

Unfortunately, we're currently using git as a local repo, and svn remote, so there's a disconnect between commit hashes.

References

Git - git branch documentation
https://git-scm.com/docs/git-branch
StackOverflow - Can you delete multiple branches in one command with Git?
https://stackoverflow.com/questions/3670355/can-you-delete-multiple-branches-in-one-command-with-git

Saturday, 21 June 2025

Git Large File Storage (LFS)

One of my hobby projects is using big files. Github complains about it:

remote: warning: File mrbear/Terrain_PureNature.asset is 58.17 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB
remote: warning: GH001: Large files detected. You may want to try Git Large File Storage1 - https://git-lfs.github.com.

Luckily this is only a warning. The real error happens when trying to push 2GB or bigger files onto the github2.

So, after installing it on my system...

brew install git-lfs

And telling git I wish to use it:

git lfs install

I had to tell it which files were the primary culprits (apparently we do not want ALL files to use LFS).

git lfs track ".png"
git lfs track ".asset"
git lfs track ".fbx"
git lfs track ".unity"

Let's not forget to add those new settings to the repo.

git add Assets/.gitattributes

Apparently this will only work on new files. Existing files in the repo are not changed.

But your could try the command git-lfs-migrate (which will change your repos history) for existing files3.

Addendum

I think it can be argued that, if you need to use LFS a lot for your git repo, git might actually not be the right tool for you.

But I'll leave that argument to smarter people.

References

[1] Git Large File Storage (LFS)
https://git-lfs.github.com
[2] GitHub - About Git Large File Storage
https://docs.github.com/en/repositories/working-with-files/managing-large-files/about-git-large-file-storage
[3] Github Blog - Git LFS 2.2.0 released
https://github.blog/open-source/git/git-lfs-2-2-0-released/

Tuesday, 22 October 2024

Git: Adding your own commands

So I do have a habit of adding my own commands, simply as an alias, simply in the startup scripts of my *nix account and it works fine.

But apparently it also can be done inside git.

So in the example in [1], the new command is "git eradicate".

It was added to the ~/.git-config file as:

eradicate = "!git reset --hard; git clean -fdx"

"-fdx" will also remove files that are mentioned in the .gitignore file.

As there are settings in there, this is perhaps not what you want.

According to the manual in [2] regarding git clean, the option "-x" has the following interesting effect:

"Don’t use the standard ignore rules (see gitignore[5]), but still use the ignore rules given with -e options from the command line. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git restore or git reset) to create a pristine working directory to test a clean build."

In that case, perhaps the one below is safer:

eradicate = "!git reset --hard; git clean -fd"

References

[1] Medium.com - SE Radio, Git Eradicate, Progress On JMS
https://medium.com/nipafx-news/jpms-support-for-module-versions-a-research-log-2c96f5d0c1ee
[2] Git - --distributed-is-the-new-centralized - Git Clean
https://git-scm.com/docs/git-clean
explainshell.com
https://explainshell.com/explain

Thursday, 2 November 2023

How to Change a Git Remote

I needed to change a remote, as the ip address of the remote server had changed.

$ git remote -v
origin ssh://mrbear@192.168.2.1:/home/mrbear/store (fetch)
origin ssh://mrbear@192.168.2.1:/home/mrbear/store (push)

So to change it:

git remote set-url origin ssh://mrbear@192.168.2.7:/home/mrbear/store

And it becomes:

$ git remote -v
origin ssh://mrbear@192.168.2.7:/home/mrbear/store (fetch)
origin ssh://mrbear@192.168.2.7:/home/mrbear/store (push)

References

CareerKarma - How to Change a Git Remote
https://careerkarma.com/blog/git-change-remote/

Thursday, 1 December 2022

Git 2.23 Adds Switch and Restore Commands

Just a little blurb from me on how to deal with branches with the "switch" command. I was still used to the "checkout" command.

# show all branches
git branch            
  
# create local branch from integration, and switch
git switch -c integration origin/integration
 
# switch back to the master
git switch master  
 
# switch to integration
git switch integration
  
# remove old local branch v0.0.1
git branch -d v0.0.1

References

InfoQ - Git 2.23 Adds Switch and Restore Commands
https://www.infoq.com/news/2019/08/git-2-23-switch-restore/

Friday, 26 March 2021

Changing Git Remote Urls

I recently switched network settings, and now my IPs have changed, and my remotes no longer match.

A quick google1 for the right syntax is all I needed.

mrbear@labtop mygitrepository % git remote -v
origin ssh://mrbear@10.0.0.1:/home/mrbear/mygitrepository (fetch)
origin ssh://mrbear@10.0.0.1:/home/mrbear/mygitrepository (push)

So my setup changed ips from 10.0.0.0 network to 192.168.2.0 network.

% git remote set-url origin ssh://mrbear@192.168.2.1:/home/mrbear/mygitrepository
mrbear@labtop mygitrepository % git remote -v
origin ssh://mrbear@192.168.2.1:/home/mrbear/mygitrepository (fetch)
origin ssh://mrbear@192.168.2.1:/home/mrbear/mygitrepository (push)

Verification is always a good thing.

% git pull
mrbear@192.168.2.1's password:
remote: Enumerating objects: 19, done.
remote: Counting objects: 100% (19/19), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 16 (delta 8), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (16/16), 93.48 KiB | 3.89 MiB/s, done.
From ssh://192.168.2.1:/home/mrbear/mygitrepository
7f41ba8..e7de4cf master -> origin/master
Updating 7f41ba8..e7de4cf
Fast-forward

References

[1] Managing remote repositories
https://docs.github.com/en/github/getting-started-with-github/managing-remote-repositories

Thursday, 31 January 2019

Git Branches

Just a little something for me to remember how to create branches and how to switch over to them.

There are plenty of resources available explaining how to do this, so I just thought I'd write some references down here.

$ git branch testing
creates a new branch called testing. Notice that this does not automatically make your new branch the current HEAD.
$ git log --oneline --decorate
seeing the branches in your log (as well as tags and the origins. Nice.)
54718edb (HEAD -> master, tag: v2.0.0, origin/master, origin/HEAD, v2.0.1) /* notes here. */
4c8c3bb9 /* moved the entire karchanangular into karchanpersonal. It compiles into the webapp directory. */
bb4bb432 /* upgrade from angular 4 to angular 7 */
12b528ed /* removed old directories that are no longer in use. Clean up. */
ead93fc3 /* changed images table, now has an autogenerated ID, plus an index on the pair owner and url. */
$ git checkout testing
switching to a branch
$ git checkout -b testing
shorthand for both creating a new branch called testing and checking it out immediately.
$ git checkout master ; git merge testing
merges the changes in the testing branch over into the master.
$ git checkout testing ; git merge master
merge the changes from your master into your testing branch.
$ git branch -d testing
delete a branch
$ git status
also shows you what branch you are on.
$ git show-branch
shows you all existing branches, and the commit that goes with that point in which it was branched.

References

GitBook - Git Branching
https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell
Git - Branch Manpage
https://git-scm.com/docs/git-branch
TutorialsPoint - Git Managing Branches
https://www.tutorialspoint.com/git/git_managing_branches.htm

Thursday, 6 April 2017

Try Git

To anyone who is absolutely new to the exciting new world of Git1.

There seems to be a little website where you can try Git2, working in a (very very) limited sandbox environment.

What is Git?

If you wish to know what Git is, there are loads of interesting articles on teh interwebs that explain it very well.

But I did find the following explanation in the README provided with the source tar-ball:
The name "git" was given by Linus Torvalds when he wrote the very
first version. He described the tool as "the stupid content tracker"
and the name as (depending on your mood):

 - random three-letter combination that is pronounceable, and not
   actually used by any common UNIX command.  The fact that it is a
   mispronunciation of "get" may or may not be relevant.
 - stupid. contemptible and despicable. simple. Take your pick from the
   dictionary of slang.
 - "global information tracker": you're in a good mood, and it actually
   works for you. Angels sing, and a light suddenly fills the room.
 - "goddamn idiotic truckload of sh*t": when it breaks

References

[1] Git --distributed-is-the-new-centralized
https://git-scm.com/
[2] Try Git
https://try.github.io/