srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tools/git.md
blob: dd527fa84299266046f4ac5aa6f8d81aaeb778d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
## Dotfiles
#### Creating a dotfile git repo  
  1. Intialize git and create a folder named ~/.cfg
  ```bash
  $ git init --bare $HOME/.cfg
  ```
  2. Put this in your .bashrc or .zsh to use `config` as a dotfiles alias.  
  ```bash
  $ alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
  ```
  
  3. Make sure to ignore the folder we're going use to clone.  
  ```bash
  $ echo ".cfg" >> .gitignore
  ```
  4. Set flag to not show untracked files  
  ```bash
  $ config config --local status.showUntrackedFiles no
  ```
  5. Replacing git with our newly created config alias we can version control our
    dotfiles.
  ```bash
  $ config status
  $ config add .bashrc
  $ config commit -m "Added bashrc"
  $ config push
  ```
#### Installing onto a new system  
  1. Avoid weird behaviour/recursion issues when .cfg tries to track itself  
  ```bash
  $ echo ".cfg" >> .gitignore
  ```
  2. Clone the repo
  ```bash
  $ git clone <remote-git-repo-url> $HOME/.cfg
  ```
  3. Set up the alias 'config'  
  ```bash
  $ alias config='/usr/bin/git --git-dir=<path to .cfg’s Git directory> --work-tree=$HOME'
  ```
  4. Set local configuration into .cfg to ignore untracked files  
  ```bash
  $ config config --local status.showUntrackedFiles no
  ```
  5. Checkout  
  ```bash
  $ config checkout
  ```
#### Use git subtree to sync other repos into dotfiles  
  1. First make sure dotfiles is up-to date or commit/push/reset all changes
  otherwise this error message will show:  `Working tree has modifications.  Cannot add.`  
  ```bash
  $ config reset --hard origin/main
  ```
  Also NOTE: the prefix (subdirectory in which you will add the subtree) cannot already exist therefore delete/backup locally.


  2. Add the repo's file path and it's remote url:  
NOTE: use the `--squash` flag to omit storing of entire history of subproject into main repo
  ```bash  
  $ config subtree add --prefix /path/to/file <remote-git-repo-url> <branch> --squash  
  # Example:  
  $ config subtree add --prefix .config/nvim https://github.com/srdusr/nvim.git main --squash  
  ```  
  

  3. Update to sync/show any changes into dotfiles (upstream repo):  
  ```bash
  $ config subtree pull --prefix /path/to/file <remote-git-repo-url> main --squash  
  ```

  4. Finally push onto remote repository:  
  ```bash  
  $ config push  
  ```
  5. Optional aliases to put into .gitconfig:  
  ```bash  
  [alias]   
    sba ="!f() { git subtree add --prefix $2 $1 main --squash; }; f"  
    sbu ="!f() { git subtree pull --prefix $2 $1 main --squash; }; f"  
  # Example:  
  $ config sbu https://github.com/srdusr/nvim.git .config/nvim  
  ```

- - -

## Basic commands
Intialize a git repository in the root of the folder:
```bash
$ git init
```
See which files git knows exist:
```bash
$ git status
```
Add a new file to the repo:
```bash
$ git add <file>
```
Create a commit with a message about the commit:
```bash
$ git commit -m <file>
```
Push the changes/commits onto repo:
```bash
$ git push -u origin <branch>
```
Reset the local main branch to remote repository aka...  
Delete the most recent commit, destroying the work you've done:
```bash
$ git reset --hard origin/main
```
Delete the most recent commit, keeping the work you've done:
```bash
$ git reset --soft origin/<branch>
```
Allow unrelated histories when two unrelated projects are merged/unaware of each
other's existence:
```bash
$ git pull origin <branch> --allow-unrelated-histories
```
- - -