Git flow Configuration Configuration has 3 levels git config --system for all users on machine git config --global for current user git config --local or git config for current repo User & mail git config --global user.email "you@example.com" git config --global user.name "name" VS Code as editor For mac git config --global core.editor "code -w" git commit --amend to check, VSCode should open Remote repo Create a folder on a server for remote repository & initialize remote git repository inside by git --bare init mkdir newProject.git cd newProject.git git --bare init post-receive We can automatically deploy the site or run commands upon git events on remote server via remote git repository For example after git push actions can be done on remote server Create file post-receive in /gitFolder/hooks/ nano post-receive make 'post-receive' file executable by chmod +x post-receive folder where post-receive will copy files needs to have Write permission chmod -R 777 /var/www/siteFolder/ (not sure 100%) to check the path to your npm run echo $PATH File content
export PATH=$PATH:/home/sherb/.nvm/versions/node/v16.8.0/bin
echo "--> Copying files..."
git --work-tree=/var/www/html/antonarbus.com --git-dir=/var/www/antonarbus.com(gitFolder) checkout -f
echo "--> Installing libraries..."
cd /var/www/html/antonarbus.com/ && npm i
echo "--> Creating build folder..."
npm run build
For myvocab.org it looks like
git --work-tree=/var/www/html/myvocab.org --git-dir=/var/www/myVocabGitRemoteRepo checkout -f
Create repo git init make local repo inside a folder Clone repo Files can be already in remote repo and we just git clone repo_path clone existing whole remote repo to current folder git clone repo_path path_where_to_clone clone to path (will be created if does not exist) git clone repo_path . clone to current folder git clone -b branch_name --single-branch repo_path folder_name_where_to_clone clone specific branch only git clone repo_path --depth= 5 folder_name_where_to_clone clone only 5 latest commits Example git clone sherb@35.209.92.93:/var/www/antonarbus.com . clone existing remote repo files to current folder Clone repo with all branches git clone --bare repo_url project_folder /.git cd project_folder git config --bool core.bare false git reset --hard Status git status status of changed files Add git add read.me style.css add files to the staging area, can chain files git add pages/posts/vim.js add file with path git add . add all files that are not staged Logs git log list of commits git log -p with details git log --oneline short version git show hash show commit details Commit Files are committed at the add snapshot. If modification are done after git add and we want them to be in commit, we need to git add files again before the commit. git commit -m "message" commit staged files git commit -m "title line" -m "longer description" message will be on 2 lines git commit -am "message" stage (add) all modified files and commit. The -a flag will not add any new files. Commit message Good commit message may look like " this commit will fix the margin issue on windows " without " this commit will " statement. Amend git commit --amend add changes to the last commit + modify commit details (commit hash will be changed) git commit --amend -m "message" update message in terminal git commit --amend --author= "new_author" update author git commit --amend --no-edit add changes to the last commit with same commit details Diff git diff changes in files from the last commit (before next commit) git diff read.me changes in file from the last commit git diff --staged changes in staged files (added) (regardless commits) Patch diff situation - you need to make a new branch from the differences between 2 branches for some reason you can not simply merge/rebase, due to for ex. problems in commit history, it happened to me https://stackoverflow.com/questions/16675766/get-the-difference-between-two-branches-in-git from the branch git diff origin/master > patchfile save differences to file git checkout -b new_branch go to new branch git apply patchfile paste data from the file do not include patchfile into commit probably this will not add patch file in git git diff master Branch1 > ../patchfile Rename git mv file1.txt file2.txt rename file & stage (add) Move git mv file1.txt /path/file2.txt move, rename & stage Remove git rm file.txt remove file from machine and git repo git rm --cached file.txt remove file from git repo only (unstage file) git rm -r folder_path remove folder from git & stage Untrack file and put into .gitignore add the file to .gitignore file git rm --cached file.txt remove file from git repo only (unstage file) git commit -m "Stop tracking file.txt" Ignore Files which are not staged, can be put into .gitignore file. Such filed will not be shown as untracked .
# dependencies
/node_modules # folder
file.js # file
folder/file.txt # file within folder
*.py # files with extension
*.py[abc] # files with extension .pya .pyb .pyc
Branch Show branch git branch local branches git branch -r remote branches, which are available locally git fetch update data about remote branches git branch -a local & remote branches git branch -a -vv branches and tracking remotes Create branch git branch name create local branch git checkout -b name create & checkout local branch Rename branch git branch -m name rename Delete local branch git branch -d branch_name_1 branch_name_2 delete local branches git branch -D branch_name_1 branch_name_2 delete local branches, even if it is unmerged Delete remote branch git push remote_repo_name --delete branch_name delete remote branch git remote prune repo_name --dry-run lists branches that do not exist on remote and can be deleted on your local git remote prune repo_name deletes branch references to remote branches that do not exist (do not delete local branches) git fetch --prune same (recommended) git config --global fetch.prune true automatically prune on every git fetch Set remote branch tracking git push -u repo_name branch_name set or change remote branch tracking git push --set-upstream repo_name branch_name push & track remote branch Show remote branch tracking git branch -vv show track info Checkout Go to branch or commit. git checkout branch_name jump to branch git checkout hash jump to commit (HEAD is moved & new commits are not in the branch) git checkout branch_name hash jump to branch & commit git checkout master jump to master branch git checkout -b name create & jump to branch git checkout file1.me file2.me jump to last committed files (discard changes) Merge types 'Fast-forward' merge happens when the 'HEAD ' mark is just moved across the commits chain. No real merging happens. 'Merge commit' happens when new commit is created combining commits from 2 branches. Merge git merge branch_name branch is merged with current branched & added into the current branch as a new commit git merge branch_name_where_to_merge branch_name_what_to_merge merge without checkout New commit will be created, history is not destroyed, save, may create a mess. Rebase git rebase branch_name branch is added before the current branch When I updated my local branch with updates from the master i did git merge master and got a mix of commits in their chronological order, but better to do git rebase master and it bring all changes from the master, but put all my commits in front. git rebase origin/master from the branch (team mates use it) and then git push origin -f BRANCH_NAME git rebase -i HEAD~ change 4 commits history in text editor git rebase -i hash change all commits after commit with hash History is modified, but commits are linear and beautiful, push -f is needed. Modify commit history In git rebase -i HEAD~ 4 following flags are widely used in front of commit hashes: r flag to modify the commit message f flag to glue commit to the previous one p flag to leave commit as it is d flag to remove commit completely Restore vs Reset git restore . undo unstaged changes (no git add . done) git reset . unstage staged but not committed changes, then you need to git restore . git reset HEAD~1 unstage last commit and then git restore . to undo local changes git reset --hard HEAD~1 unstage and last commit and undo changes (same as above) Restore git restore read.me discard local changes in a file, i.e. restore to its last committed state git restore . discard local changes in all files git restore --staged read.me undo the git stage (add) operation Revert git revert hash undos changes in commit and puts the output as a new commit Used if error is found in some commit. Reset Mixed git reset hash undo changes (just move HEAD to some commit), all further modification are available, but not staged, commits are not deleted. We undo add + commit commands. git reset --mixed hash same we can reset back to the same commit knowing its hash, because commit is not deleted, even it is not visible in logs. git reset --hard origin/main if master is out of order and asks to push commits Soft git reset --soft hash undo changes, but stage all following modifications. We undo just commit command. git reset --soft HEAD~1 undo changes the last commit Hard git reset --hard undo changes to the last commit & remove commits. We undo file modifications + add + commit commands. Untracked files are not involved. git reset --hard hash undo changes to the commit & remove commits, get rid of everything after specific commit git reset --hard HEAD^ remove the last commit & git clean may clean unknown files git reset --hard HEAD~2 remove last 2 commits Clean git clean -n show untracked files to be removed git clean -n -d show untracked files & folders to be removed git clean -f remove untracked files git clean -f -d remove untracked files & folders Remote Add remote git remote add name link link the local & remote repos and name it Example git remote add origin sherb@35.209.92.93:/var/www/myVocabGitRemoteRepo Example git remote add interviewTask git@github.com:sherbsherb/interview-task.git Show remote git remote display names of remote repos git remote show name show info on remote repo git remote -v display remote with url Update remote git remote rename old_name new_name change name of remote repo git remote set-url name new_url set new address or update the existing one Remove remote git remote remove name remove the link to remote repo Push git push repo_name branch_name send data to remote repository git push same, if branch is 'tracked' git push -f push with warnings suppression Example git push origin master send data to origin repo into master branch Fetch git fetch repo_name get the latest changes from remote repo without merging Changes can be reviewed and merged manually later Pull git pull repo_name branch_name get latest updates from remote repo's branch git pull same, if branch is 'tracked' Same as git fetch && git merge Same as git pull -p pull in case got error: cannot lock ref 'refs/remotes/origin/master': is at dce82 but expected 2b16b Copy & paste commit git cherry-pick hash copy commit and paste into current branch at the end git cherry-pick hash -edit add own commit message git cherry-pick hash --no-commit take data from commit and put into the staging area without commit Stash git stash cut uncommitted files & put in stash (kind of a smart clipboard) for letter usage git stash -u cut uncommitted + untracked files & put in stash git stash -a cut uncommitted + untracked + ignored files & put in stash git stash list show stash content git stash save "name" same, but with name for the stash to let you identify it in the list git stash apply copy latest stash & paste on your working directory git stash apply stash_id_number copy stash with id (0,1,2...) & paste into your working directory git stash pop cut latest stash and paste on your working directory git checkout stash@ { stash_id_number } -- file.txt take specific file from stash and put into working directory Track file, but remove from commit git update-index --assume-unchanged file_name.txt file will be kind of untracked git update-index --no-assume-unchanged file_name.txt undo previous operation May face a problem when pull requests because of changed such file, but impossible to notice it. Keyboard keys q exit Recommendations Never work in master, always make a branch Make short commit messages, try to connect it to a Jira ticket number When work alone, prefer to rebase instead of merge Shortcuts git add . && git commit -m "comment" && git push origin master git add . && git commit -m "comment" && git push Minimize merge conflicts We have many commits in a branch It is possible that a conflict should resolved for every commit instead of just resolving it ones for the final code Pull the latest version of main/master and checkout a new branch based on it git checkout master git pull git checkout -b temp_work Pull & squash changes from your messy branch Changes are staged without creating a commit git merge --squash messy_branch At this point, you might be getting a smaller number of unavoidable merge conflicts Make a commit after resolving conflicts git commit If you'd like to re-use your old branch, you can now reset it to your temporary branch created above Force-push the changes up to GitHub before deleting my temporary work branch git checkout messy_branch git reset --hard temp_work git push -f git branch -D temp_work