# How to Git 🔀 ###### tags: `yotta` `git` [TOC] ## Best Practices - Be confident about what you commit: never commit something that just doesn't compile/run - Use process-oriented tools like Github, Gitlab - Have clear general guidelines/rules that you apply consistenly, whether with yourself or within a team As much as possible: - Sync regularly with the master branch - Don't create too complex branching structures ## Create a Project ```bash mkdir ~/code/new_project cd ~/code/new_project git init git add README.md git add .gitignore git branch -M main # sync with a remote repo git remote add origin "https://github.com/.../myrepo.git" git push -u origin main ``` ### Branching Create a new branch ```bash git checkout -b new_branch ``` going back to the master ```bash git checkout master ``` ### Merging While in a branch, see what are the differences with another branch ```bash git diff otherbranch ``` Merge ```bash # typically while in master git merge other_branch ``` Rebase ```bash git rebase new_branch # cancel git rebase --abort ``` ### Remove / Modify a commit ```bash # reset to a specific head from the commit id git reset HEAD HEAD_ID # or, to just restore the previous commit as the current git reset --hard HEAD~1 # modify a commit git commit --amend ``` ## Remote ### Setup ```bash # setup remote git remote add origin <repo_url> # change the url git remote set-url origin <new_repo_url> ``` ### Push ```bash # current branch sync status git status # add file/directory to what is commited (staging) git add . # to unstage something git reset HEAD file.py # commit git commit -m "message on this commit" git push ``` ### Pull ```bash # inside local directory git pull ``` ## Get a project ```bash git clone "https://github.com/.../somerepo.git" ``` ## Info ```bash # see past commits git log git reflog # add tags git tag -n 'v0.0.1' git push <repo> <tag> # see cached files git ls-files # see remote address git remote -v # current branch sync status git status ```