21 November, 2023

git Notes

Setup after install

After a fresh git install we have to set user-variables like this:

git config --global user.name "Phyrum Tea"
git config --global user.email phyrum@tea.ch

To verify variables:

git config --list --global

Git Repository

You don't need GitHub, Azure or a server to have your own repositories. A local drive or an USB stick can host your private git repositories. Here are the steps to create a remote bare repo. This can be on an USB stick like D:\git on Windows or a home folder on a Mac /Users/ptea

cd /Users/ptea
mkdir git
git init --bare --shared wisecards.git

You either start from a project folder that is not a git repository yet or from a cloned one. Here are the steps to convert current project to git project:

cd wisecards
vi .gitignore // to ignore bin/ and gen/ folders.
git init
git add .
git commit -m ‘Initial commit’

Now add contents to the remote bare repository:

git remote add origin /Users/ptea/git/wisecards.git
git push -u origin master

The repository can now be cloned from the same machine or from other machines when accessible as a network drive:

git clone /Users/ptea/git/wisecards.git

# or

git clone file:///Volumes/ptea/git/wisecards.git

Here is how to change the remote repository. Check the current value, show origin url:

git remote -v

Change origin

git remote set-url origin file:///C:/Users/ptea/git/Wisecards.git

Git Commands

remove or delete multiple filesf:

git add -u

after editing some files

git add .
git commit -m 'added new benchmarks'
git push

shortcut for add and commit.

git commit -a -m 'added new benchmarks'
git push

If I have some changes that I want to add to the last commit. --force is only needed when the last commit was already pushed to the remote repo.

git commit --amend --no-edit
git push --force

Start new branch alpha15

git branch alpha15
git checkout alpha15

Shorthand for new branch and checkout

git checkout -b develop

Make branch alpha15 public

git push origin alpha15

Continue work on master

git checkout master
git tag 1.5.0.3a

replace tag git tag -f 1.5.0.3a

git push origin -f 1.5.0.3a
git push origin 1.5.0.3a

Update after clone

cd wisecards
git pull

git checkout alpha15

move master to develop

git checkout master
git merge develop

git checkout alpha15
git merge master

publish the 2 merges

git push origin master
git push origin alpha15

Delete unused branch, branches which are merged in master:

# list merged branches for cleanup/delete
git branch --merged master
# delete branch, multiple branches can be listed after -d flag
git branch -d alpha15
git push origin :alpha15

show history

git log --pretty=oneline

Made a mistake and want to revert commits to the origin/master?

git reset --hard origin/master

Rebase

I don't like using rebase but if the project's policy requires it to fake the repo-history:

git checkout master
git pull
git checkout alpha15
# git branch --set-upstream-to origin/alpha15
git rebase master
git push --force

rebase and squash last 2 commits

git rebase -i HEAD~2

Squash and Pick

git rebase -i HEAD~5

Commit in wrong Branch

Sometimes mistakes happen. You checkout out a branch and made changes and commit without making a new branch. Here are the commands to undo and start a new branch:

git reset HEAD~1
git checkout -b alpha15

Commit amend to existing commit

Use case, I have changes that must go to older commit which is not the previous.

# Store current changes to the stash
git stash

# Lookup the commit sha
git log --pretty=oneline

# Start a rebase add ^ at the end 
git rebase -i '6baf243b14cdf501ce79a35b91ef4377ee34674d^'

# An editor opens, change the pick to edit, save and close file.

git stash pop

git commit --all --amend --no-edit

git rebase --continue

Ignore Files

In .gitignore are files and folders listed that should not be in the repository. But when you want to make chages in some file that should be kept locally neither .gitignore nor .git/info/exclude will work.

git update-index --assume-unchanged src/Release.config

Undo the decision:

git update-index ---no-assume-unchanged src/Release.config

Using git over ssh

git clone ssh://phyrum@tea.ch:/media/Samba/Git/Test.git samba