git – tips and tricks

Overview

This is a set of tips and tricks that I commonly use when working day to day with git.

General Tips

File Path length

Keep your file paths short, as sometimes you will hit a file path limit.
I keep my repos as close to the root of the drive as possible.

c:\code
or if you work for multiple clients or projects you might have a subfodler
c:\code\{project}

Setup

Username and email

When building a new computer you will need to setup your username and email.

If working for one company you can use a -global switch to

git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

To set it on a single repo enter the repo and use

cd /path/to/your/repository
git config user.name "Your Name"
git config user.email "youremail@yourdomain.com"

To see your configuration

git config --list

Long filenames

Sometimes with folder structures that are too deep inside your repo you will hit a path limit. Setting this on can help extend the length to 32767 long or 2^15

In a window as administrator (powershell or git bash), run the following command to enable long file paths.

git config --system core.longpaths true

Git Commands

Clone

cd into the root of the folder where you keep your code

New branch – first commit

These are the commands you may use to create a new branch.. once worked on add you changes, commit and the push for the first time.

git checkout -b feature/1234
... do some work and make changes
... feature/1234 where 1234 might be a reference to the task # you are working on

git add .
git commit -m "first commit"
git push -u origin feature/1234

Pull a specific commit

git checkout {commit hash}

Rename a folder

Sometimes you want git to treat a folder rename as a move not a delete and add so this is the command

git mv {old name} {new name}

Discard local uncommitted changes

git checkout .

Fix untracked files

Sometimes people start with a bad .gitignore and so all the bin and obj files and .vs folders are committed to source control, which is bad and you want to get them out of the remote repo

So you adjust the .gitignore but now all the files that you are no longer tracking are still in the remote rep.

To clear the remote repo and re-align the remote repo with the latest .git ignore do the following:

git rm -r --cached .
git status
git add .
git commit -m "fixed untracked files"
git push

The git status is just so you can see what it has done.

Tag a branch

Here is an example where you might tag a release by sprint name

git tag -a 2025-01-R1 -m "2025-01 - release-1"
git push origin 2025-01-R1

to remove that tag

git tag -d 2025-01-R1
git push --delete origin 2025-01-R1

Cancel Merge

git merge --abort

that didn’t work for me on an occasion so I used the following to cancel the merge

git reset
git checkout .

The first command finds and clears all the files no longer being tracked, you then add all the changes, commit and then push.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.