Back (Current repo: scraps)

random scraps and notes that are useful to me
To clone this repository:
git clone https://git.viktor1993.net/scraps.git
Log | Download | Files | Refs

commit cd6f40febb8345964fe50ec059dc8149209bf03c
parent a419558b05c03d5611dfe18816100c2b411c9030
Author: root <none>
Date:   Mon, 28 Sep 2026 20:28:25 +0200

new file

Diffstat:
Anotes/git/git_very_basic.txt | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 80 insertions(+), 0 deletions(-)

diff --git a/notes/git/git_very_basic.txt b/notes/git/git_very_basic.txt @@ -0,0 +1,80 @@ +REMOTE SYSTEM LOCAL SYSTEM +================= | ================================================================================== +================= | ================= =================== =============== ============= ============= +REMOTE REPOSITORY | REMOTE REPOSITORY LOCAL REPOSITORY STAGING AREA WORKING COPY STASH +(ORIGIN) | (CACHED) +I.e. Bitbucket | mirror of the (this is in your (purgatory) (what you see (temporary space +GitHub, GitLab, | remote repo .git folder: in your almost like a +basically any | .git/refs/remotes .git/refs/heads) filesystem) clipboard) +git server | + | + | +git fetch *------------->* +Your local cache of the remote is updated with the origin (the external source) + | +git pull *-------------------------------------------------------------------->* +this is basically a git fetch immediately followed by a git merge, so any changes are merged directly into your local copy. +If merge conflicts occur, git will ask you to resolve them. +A git merge just means that you move the contents of one branch into another branch (oversimplified, but basically). + | +git add | *<-------------* + | +Before committing files, you need to stage them with git add. +Basically, git add just earmarks that files as something to be included in your next commit. +So git commit actually updates things from this staging area, not directly the working copy. + | +git commit | *<------------* + | +Updates your local repository from the staging area. +If you happen to work on multiple unrelated things in one branch, +and you only want to commit one of the things, things here get complicated and you will need to get creative with git stash... + | +git push *<--------------------------------* +Synchronizes your changes back into the origin (remote repository) +from your local repository. + | +git checkout --track <remote branch> + | *--------------------------------------------------->* +Checking out a local branch from a remote branch automatically a tracking branch. +Tracking branches are local branches with a direct relationship to a remote branch. +If you’re on a tracking branch and type git push, git automatically knows which server and branch to push to. +(cloning a new repository automatically does this, sets up your local master as the tracking branch of origin/master, +aka the remote master branch, that is why e.g. git pull just works with no further arguments needed) + | +note: it is also possible to set a tracking branch from any existing branch manually, +git branch --set-upstream-to=<remote>/<branch name> <branch name> + | +git stash | *<------------------>* +doing git stash push/save will save your current changes to a temporary space, +and revert your current WORKING COPY back to the current commit (HEAD) this can allow you to save uncommitted changes and use them later. +Simply do git stash pop/apply to reapply the changes from stash, but be careful +that you don't create conflicts between your working copy and your stash :) + +TL;DR on how to deal with the situation when you want to stash PART of your working directory, and keep working on the committed files: + +#> commit the files temporarily you don't want to stash +git add <some file> +git commit -m "temp" + +#> then stash the other files +git stash save "useful message to self" + +#> then undo the previous temp commit +git reset --soft HEAD^ +# this will undo the commit you just made, but will leave your files staged (sitting in the green "ready to commit" area). +git reset +# this will undo the staging, so moving them back to the WORKING COPY, so you can keep working on your files + +git stash list (to check how to retrieve the things you stashed earlier) +#> you might want to clear stash after all is done + +git stash clear +#> this is the command to remove everything(!) from stash + +A final notice: + +When revisiting an old branch, to continue some work that's been paused for a while, it's IMPERATIVE that you do a git fetch && git merge master into your branch. +Otherwise, if you check the diagram above, you should see, that the files that you do change in your old branch, will be still the old version from a long time ago + your changes, overriding ALL changes that may have happened to those files in the meantime. + +So this is not even a merge conflict or anything, it's just simply stating that your old files + your changes on top are what you want to push. There is no warning. So always remember to git fetch as often as you can, and coordinate with others if you think you're working on files others may also frequently touch (THINK: configuration files, crontab files, etc.). +