|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +title: git bare repository |
| 4 | +tags: |
| 5 | + - git |
| 6 | + - bare-repo |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- TOC --> |
| 10 | + |
| 11 | +# How create bare repo |
| 12 | +> |
| 13 | +> [source](https://www.ackama.com/what-we-think/the-best-way-to-store-your-dotfiles-a-bare-git-repository-explained/) |
| 14 | +``` git init --bare $HOME/dotfiles ``` |
| 15 | + |
| 16 | +- add this to your .zshrc: |
| 17 | + |
| 18 | +```git |
| 19 | +alias git-bare='/usr/bin/git --git-dir=$HOME/dotfiles --work-tree=$HOME' |
| 20 | +alias bare='git --git-dir=$HOME/dotfiles --work-tree=$HOME' |
| 21 | +``` |
| 22 | + |
| 23 | +```git |
| 24 | +alias git-bare='git --git-dir=$HOME/dotfiles --work-tree=$HOME' |
| 25 | +``` |
| 26 | + |
| 27 | +- Add files you want to commit |
| 28 | + |
| 29 | +>[!Important] DELETE .git folder |
| 30 | +> Delete this folder which bare-repo you add. You can't use bare-repo while using .git track for different repo? |
| 31 | +
|
| 32 | +```git |
| 33 | +git-bare add ~/.git-bare/polybar ~/.git-bare/i3 ~/.git-bare/alacritty ~/Documents/screenloyout |
| 34 | +``` |
| 35 | + |
| 36 | +```git |
| 37 | +git-bare commit -m "add my files" |
| 38 | +``` |
| 39 | + |
| 40 | +- Add remote repo if you doing first time |
| 41 | + |
| 42 | +```git |
| 43 | +git-bare remote add origin <remote-url> |
| 44 | +``` |
| 45 | + |
| 46 | +- Push change master or your `branch-name`. |
| 47 | + |
| 48 | +```git |
| 49 | +git-bare push -u origin bare-repo |
| 50 | +``` |
| 51 | + |
| 52 | +## How to use .gitignore on bare repo? |
| 53 | +> |
| 54 | +>[!important] bare-repo .gitignore |
| 55 | +>Firstly you need to untrack files who is already tracked by git before using .gitignore. |
| 56 | +
|
| 57 | +1. Create global exclude file to your specific location: |
| 58 | + |
| 59 | +```python |
| 60 | +# it's exlude files here because bare repo use `--work-tree=$HOME`(e.g /home/<user>/) |
| 61 | +bare config --global core.excludefile ~/.gitignore |
| 62 | +# or |
| 63 | +bare config --global core.excludefile ~/Documents/.gitignore |
| 64 | +``` |
| 65 | + |
| 66 | +2. Add file name to `~/.gitignore` |
| 67 | +3. Untrack if file already tracked by git or unstage if you commit the file. **==WARNING: THIS WOULD DELETE FILES==** |
| 68 | + |
| 69 | +```python |
| 70 | +bare rm -r --cached .stfolder .stversions |
| 71 | +``` |
| 72 | + |
| 73 | +## How to get latest changes on bare-repo for other machines? |
| 74 | + |
| 75 | +If you want to completely replace your local files with the latest version from the bare repository's branch and discard any untracked or modified files, you can follow these steps: |
| 76 | + |
| 77 | +### 1. **Reset the Local Repository** |
| 78 | + |
| 79 | +```python |
| 80 | +git fetch origin my-branch |
| 81 | +bare-fetch |
| 82 | + |
| 83 | +git reset --hard origin/my-branch |
| 84 | +bare-reset |
| 85 | +``` |
| 86 | + |
| 87 | +## How to not get changes from bare-repo branch? (e.g qtile/**pycache** problem) |
| 88 | + |
| 89 | +```python |
| 90 | +bare-push --force |
| 91 | +``` |
| 92 | + |
| 93 | +## How to revert one file? |
| 94 | + |
| 95 | +```python |
| 96 | +# learn commit id |
| 97 | +git-bare log --follow -p -- flake.lock |
| 98 | +# revert back to commit changes. This will modify flake.lock to that commit changes. |
| 99 | +git-bare checkout <commit_id> -- flake.lock |
| 100 | +# Now you reverted back that commit changes to flake.lock |
| 101 | +# You can update file etc. |
| 102 | +``` |
| 103 | + |
| 104 | +## git doesn't look for folder changes. Add bare-repo again when you add folder |
| 105 | + |
| 106 | +- For example, when you create a new folder or file inside qtile bare-repo don't update it via commit. Therefore, you need to add folder again to add new file and folders. |
| 107 | + |
| 108 | +## How to add new folder to bare-repo? |
| 109 | + |
| 110 | +```bash |
| 111 | +git-bare add ~/.git-bare/qtile |
| 112 | +git-bare commit -m "add qtile" |
| 113 | +git-bare push -u origin bare-repo |
| 114 | +``` |
| 115 | + |
| 116 | +## How to update bare-repo? |
| 117 | + |
| 118 | +```python |
| 119 | +git-bare status #to see modifications |
| 120 | +git-bare commit -am 'updated <file_name>' |
| 121 | +git-bare push -u origin bare-repo |
| 122 | +``` |
| 123 | + |
| 124 | +## How to show readme.md on bare-repo? |
| 125 | + |
| 126 | +- You just need to create readme.md folder like this: `qtile/docs/README.md` |
| 127 | + - This will show README.md file on qtile folder. |
| 128 | + |
| 129 | +# Others |
| 130 | + |
| 131 | +- If you want to duplicate all the objects from the main repo, do this inside the main repo: |
| 132 | + |
| 133 | + ``` |
| 134 | + git push --all <url-of-bare-repo> |
| 135 | + ``` |
| 136 | + |
| 137 | + Alternatively, do a fetch inside the bare repo: |
| 138 | + |
| 139 | + ``` |
| 140 | + git fetch <url-of-main-repo> |
| 141 | + ``` |
| 142 | + |
| 143 | +You cannot do a pull, because a pull wants to merge with `HEAD`, which a bare repo does not have. |
| 144 | +You can add these as remotes to save yourself some typing in the future: |
| 145 | +I added origin to whatever-name section like this git-bare remote add origin `bare-repo-url` |
| 146 | + |
| 147 | +- `git remote add <whatever-name> <url-of-other-repo>` |
| 148 | +- Then you can simply do |
| 149 | +- `git push --all <whatever-name>` |
| 150 | +- `git fetch <whatever-name>` |
| 151 | +- depending on what repo you're in. If `<whatever-name>` is `origin`, you can even leave it out altogether. |
| 152 | + |
| 153 | +<!-- /TOC --> |
0 commit comments