Git - dwisianto/dwisianto GitHub Wiki

Git

Routine

structure

  • _bare : working bare directory
  • _info : ln -s master/readme.md _info
  • _bundle :
  • _archive : zip bundle without history
  • _stash :
  • dot_git
    • echo "gitdir: ./_bare" >> .git

RC

alias gt-bd-new='git bundle create ../bundle/b$(date +"%Y%M_%S_%m_%d") --all'

bundle - bare - master - bundle

time_now=$(date +"%Y_%M_%S_%m_%d")
git clone --mirror _bundle/b_latest _bare
git clone _bare master
cd master; git bundle create ../_bundle/b${time_now} --all

Worktree

cd master
git config -e
git worktree add ../local_dir remote_branch_name
git worktree list
git worktree remove ../local_dir
git worktree prune

Bashrc

#                                                                               
# [] Git                                                                        
# alias gt-br-new="f(){ git checkout -b $1 }; f"                                
#                                                                               
alias gt-="git status"
gt-br-new() {
    echo "git checkout -b $1"
}
gt-all() {
    git add .
    if [ "$1" != "" ] # or better, if [ -n "$1" ]                               
    then
        git commit -m "$1"
    else
        git commit -m update
    fi
    #git push                                                                   
}

Commit

a b c d
git add all uncommited files git add -A
git remove
git rm
git rm -r
git rm -rf
set-upstream
git push --set-upstream origin ie3ts
merge multiple commits : squash

Branch

a b c d
git checkout -b new_branch_name
git checkout branch_name
git switch -c new_branch_name
git switch branch_name
local git branch -a
local git branch ---delete myBranch
git checkout -b iss53
git branch iss53
git checkout iss5

Branch New

Switched to a new branch "iss53"

$ git checkout -b iss53

This is shorthand for:

$ git branch iss53 
$ git checkout iss5

Push to origin

 git push --set-upstream origin y22m08dev

Branch Delete

To delete a local branch in Git, you simply run:

git branch -d <branch-name>

If the branch contains unmerged changes, though, Git will refuse to delete it. If you’re sure you want to do it, you’ll have to force the deletion by replacing the -d parameter with an uppercase D:

git branch -D <branch-name>

You don’t use the git branch command to delete a remote branch. You use git push, even if that sounds weird. Here’s how you do it:

git push --delete <remote name> <branch name>
git push origin --delete myBranchName

It’s like you’re pushing—sending—the order to delete the branch to the remote repository.

Branch Rename

# Start by switching to the local branch which you want to rename:
git checkout <old_name>

# Rename the local branch by typing:
git branch -m <new_name>

# At this point, you have renamed the local branch.
# If you’ve already pushed the <old_name> branch to the [remote repository](https://linuxize.com/post/how-to-add-git-remotes/) , 
perform the next steps to rename the remote branch.

# Push the <new_name> local branch and reset the upstream branch:
git push origin -u <new_name>

# Delete the <old_name> remote branch:
git push origin --delete <old_name>

# That’s it. You have successfully renamed the local and remote Git branch.

Naming Convention

Orphan1

Sometimes you want to create a completely empty git branch in a repository (for example for documentation, static site generation etc). Here is how to accomplish this. Replace empty-branch with the branch name you want to create.

git checkout --orphan empty-branch
git rm -rf .
git commit --allow-empty -m "root commit"
git push origin empty-branch

Orphan2

Git Branch from scratch

November 2021 Update: As of git version 2.27, you can now use git switch --orphan to create an empty branch with no history.

Unlike git checkout --orphan , this branch won't have any files from your current branch (save for those which git doesn't track).

This should be the preferred way to create empty branches with no prior history.

Once you actually have commits on this branch, it can be pushed to github via git push -u origin :

git switch --orphan new_branch
git commit --allow-empty -m "Initial commit on orphan branch"
git push -u origin new_branch

Tag

git status
git fetch --all --tag
git tag -l
git checkout <myTag>
git switch -c <myTagmyBranch>
action command
to list all git tag
add tag git tag -a v2022.11.15 -m "comment here"
push to origin git push --tags
check log git log --one-line

GitHub Configuration

  • Username
    • git config --global --edit
# This is Git's per-user configuration file.
[user]
# Please adapt and uncomment the following lines:
name = dwisianto
email = [email protected]
  • Amend

  • git commit --amend --reset-author

  • cd ~/.ssh

  • ssh-keygen -C "[email protected]"

    • pbcopy < id_rsa.pub
  • to list the current configuration

    • git config --list
  • using main instead of master as the default branch

    • git config --global init.defaultBranch main
  • git clone a project

  • ~/.ssh/config

Host github.com.dm
  User git
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_d20_do_gh_com
#  IdentitiesOnly yes
Host github.com.do
  User git
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_d9_do_gh_io
#Host github.com
# AddKeysToAgent yes
# UseKeychain yes
# IdentityFile ~/.ssh/id_rsa_d9_do_gh_io
Host bitbucket.com
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa_d9_do_gh_io
Host git-codecommit.*.amazonaws.com
 User abcdefgthijiklmnop
 IdentityFile ~/.ssh/id_rsa_d_aws_debeans2

Merge

https://stackoverflow.com/questions/59714347/semi-linear-merge

squash1

https://stackoverflow.com/questions/36437912/how-to-properly-use-git-merge-squash

squash2

https://www.baeldung.com/ops/git-squash-commits

Bare

https://saraford.net/2017/03/03/how-to-create-your-own-local-git-remote-repo-thats-not-hosted-on-a-git-server-bare-option-062/

Bare1

Clean Bare Repo

Short-term solution

In the repository's directory, open up your $GIT_DIR/config file (e.g. .git/config or in my case .bare/config) and add a new line under [remote "origin"]:

fetch = +refs/heads/*:refs/remotes/origin/*

In the event you do not fancy editing manually the configuration file you can also run the following command:

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" 

Below is an excerpt of the configuration file with the remote.origin.fetch property correctly configured:

[remote "origin"]
    url = …
    fetch = +refs/heads/*:refs/remotes/origin/*

Now, any git fetch origin or git worktree add... command will, when necessary, retrieves the remote branches that are missing locally.

Long-term Solution

bare.sh

#!/usr/bin/env bash
set -e

# Examples of call:
# git-clone-bare-for-worktrees [email protected]:name/repo.git
# => Clones to a /repo directory
#
# git-clone-bare-for-worktrees [email protected]:name/repo.git my-repo
# => Clones to a /my-repo directory

url=$1
basename=${url##*/}
name=${2:-${basename%.*}}

mkdir $name
cd "$name"

# Moves all the administrative git files (a.k.a $GIT_DIR) under .bare directory.
#
# Plan is to create worktrees as siblings of this directory.
# Example targeted structure:
# .bare
# main
# new-awesome-feature
# hotfix-bug-12
# ...
git clone --bare "$url" .bare
echo "gitdir: ./.bare" > .git

# Explicitly sets the remote origin fetch so we can fetch remote branches
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

# Gets all branches from origin
git fetch origin

Worktree

list git worktree list
new
add git worktree add ../brach_dir branch_name
git worktree add -b [branch-name] [path] [remote]/[branch-name]
git worktree add -b feature-zzz ../feature origin/feature-zzz
del
prune git worktree prune list

Worktree List

Link

Worktree New

Link

Worktree Add

Linke

Worktree Del

Link

Worktree1

To check out the branch into a new worktree, you can run the command git worktree add [path] [branch]. This will checkout [branch] into the directory at [path]. Make sure that [path] is not an existing directory containing file. Now, you should see a new directory with the contents of [branch] freshly checked out. Make sure to run any project initialization steps, like initializing submodules or installing pods, just like you would if you had just pulled the repository down onto a new computer.

Once you are done with the worktree, you can run git worktree remove [path], and Git will delete the directory, and all of its contents. If you delete the directory yourself, you can also run git worktree prune to remove the linked worktrees in directories that no longer exist.

To see a list of all the current worktrees, run git worktree list. This will print out a list of the current worktrees, the commit, and the branch currently checked out on each worktree. It is important to remember that you cannot checkout the same branch into multiple worktrees.

In my daily workflow, I keep 3 worktrees active on my computer:

One with the latest on Develop One with the branch of my latest work One which I use to checkout Pull Requests, or other developer’s work

git worktree add
git worktree list
git worktree remove
git worktree add -b [branch-name] [path] [remote]/[branch-name]
git worktree add -b feature-zzz ../feature origin/feature-zzz

Worktree2

https://opensource.com/article/21/4/git-worktree

Worktree3

One obvious use is to simultaneously compare the behavior (not source) of different versions - for example different versions of a web site or just a web page.

I tried this out locally.

  • create a directory page1.
  • inside create the directory src and git init it.
  • in src create page1.html with a little content and commit it.
  • $ git branch ver0
  • $ git worktree add ../V0 ver0
  • in src master add more text to page1.html and commit it.
  • $ git branch sty1
  • edit page1.html in the sty1 branch (add some distinctive CSS style) and add commit it.
  • $ git worktree add ../S1 sty1

You can now use a web browser to open and view these 3 versions simultaneously:

  • ..\page1\src\page1.html // whatever git has as current
  • ..\page1\V0\page1.html // the initial version
  • ..\page1\S1\page1.html // the experimentally styled version

Bundle

https://stackoverflow.com/questions/5578270/fully-backup-a-git-repo

git clone --mirror [email protected]/reponame reponame.git
cd reponame.git
git bundle create reponame.bundle --all

After that you have a file called reponame.bundle that can be easily copied around. You can then create a new normal git repository from that using git clone reponame.bundle reponame.

Note that git bundle only copies commits that lead to some reference (branch or tag) in the repository. So tangling commits are not stored to the bundle.

Stash

https://gist.github.com/jioo/eebe4c39d800baf99c0db703ded4842c

Archive

https://mindmajix.com/git-tips-tricks

Synology

  • ~/.rc
  • https://gist.github.com/walkerjeffd/374750c366605cd5123d
  • Set Up User and Folder
    • Create user gituser via Diskstation interface (with File Station and WebDAV privilages)
    • Add new shared folder to hold all repos; which is called git (located at /volume1/d2/s2/g5) with read/write access for gituser and admin.
    • Install Git Server package via Diskstation
    • Open Git Server and allow gituser permissions
    • Enable SSH access on Diskstation (Control Panel > Terminal & SNMP > Enable SSH Service)
  • Configure SSH Access
    • create ~/.ssh folder for gituser on server
    • ssh [email protected]
    • mkdir /volume1/homes/gituser/.ssh
    • copy public rsa key from local computer to gituser account on server
    • scp ~/.ssh/id_rsa.pub [email protected]:/volume1/homes/gituser/.ssh
    • connect via SSH as root and rename id_rsa.pub to authorized_keys on NAS (or append if already exists, cat id_rsa.pub >> authorized_keys)
    • ssh [email protected]
    • mv /volume1/homes/gituser/.ssh/id_rsa.pub /volume1/homes/gituser/.ssh/authorized_keys
    • change permissions while logged in as root
    • cd /volume1/homes/gituser/
    • chown -R gituser:users .ssh
    • chmod 700 .ssh
    • chmod 644 .ssh/authorized_keys
  • Set Up New Repo on NAS
    • create a bare repo as root
    • ssh [email protected]
    • cd /volume1/d2/s2/g5
    • git --bare init my-repo-name.git
    • chown -R gituser:users my-repo-name.git
    • cd my-repo-name.git
    • git update-server-info
    • NOTE: I'm not entirely sure if git update-server-info must be run for each repo or just initially. It seems to work without running this command, but I'm suspcicious that it might cause problems later.

Client clone the repository

References

Synology2

MobaXterm

https://superuser.com/questions/1728958/how-to-use-mingw64-instead-of-msys-when-using-git-bash-in-mobaxterm

When I try to use Git Bash inside MobaXTerm, it shows it as being MSYS. However, when I open Git-Bash directly (not via MobaXTerm), it shows it as being MINGW64. Going with the first approach, I am severely limited (does not recognize "git --version" as a command). Is there a way to switch MobaXTerm git-bash terminal to be MINGW64 instead of MSYS?

Background: I created a "Shell" user session, selected "Bash (external)" terminal shell, and in Advanced Shell Settings, I set the Distribution directory to "C:\Program Files\Git\bin". In Terminal Settings, I selected "Use Windows PATH". Also, I created 2 environment variables in PATH (windows 11), set to "C:\Program Files\Git\bin", one for system and one for my personal path.

Work

d23

  • /Users/dwyk/d/s/m/wk/d23/ie3
dwisianto.github.io
⚠️ **GitHub.com Fallback** ⚠️