- Go to Github > Settings > Developer Settings > Personal access tokens > create token. Check Repo. Copy the token
- Click
git add *>git commit -m '.'>git push. Either a popup or within the console, will ask for the username & password. - Username for 'https://github.com': mapattacker
- Password for 'https://mapattacker@github.com': paste personal access token
- In your terminal >
ssh-keygen - Accept all defaults
- Copy the public key
cat ~/.ssh/id_rsa.pub - Go to Github/Gitlab Settings > SSH Keys, and paste the keys here
- We should be able to git push/pull/clone now without logging in
- Create new Git Directory, eg.
mkdir github - Go into directory:
cd github - Create github key >
ssh-keygen> enter to accept defaults - Open file & copy keys
cat ~/.ssh/id_rsa.pubORopen ~/.ssh/id_rsa.pubORopen -a textedit ~/.ssh/id_rsa.pub - Go to Github/GitLab > Settings > SSH and GPG Keys > paste the key here
- create new project folder
mkdir testfolder, thengit init - Go to Github > create new repository (DO NOT create a README.md or other files) > get repository link
git add README.mdgit commit -m "first commit"git remote add origin https://github.com/mapattacker/testrepo.gitgit push -u origin master> enter username & token as password
bash git init git add README.md git commit -m "first commit" git remote add origin https://github.com/mapattacker/datavis.git git push -u origin master
- Existing repository: clone project, eg
git clone https://github.com/mapattacker/cheatsheets.git
git add: add changes in the repository to the staging areagit add -A: add all changes including deleted filesgit add *: add all changes, excluding deleted files
git commit -m "add a message here": commit files with a message tag to repositorygit push: push committed files to GitHub- A git alias can be used to combine all 3 commands into one. This needs to be set at each repository in your local machine
git config alias.pushall '! git commit -a -m "commit" && git push': push-all refers to the alias name, it can be thus called usinggit pushallgit config --global --unset alias.YourAlias: to remove the set alias
- Checkout Branch
git branch: check which branch is active, asterisk sign besidegit fetch: fetch all remote branchesgit checkout branch: switch to another branchgit fetch; git checkout branch: switch to a branch from remote
- Create Single Branch
git clone --single-branch --branch <repo-url>: clone existing branch. e.g.git clone --single-branch --branch main-branch git@gitlab.com:projectname/repo/scene-understanding.git
- Create from Existing Branch (1)
git checkout -b newBranch developgit push origin newBranch
- Create from Existing Branch (2)
git push origin develop:newBranch
- Delete Branch
- Locally
git branch -d branch-to-delete: will check if the branch is mergedgit branch -D branch-to-delete: will NOT check if the branch is merged (forced delete)
- Remotely
git push origin -d branch-to-delete: delete branch in remote
- Merge Branch
- to merge branchB into branchA
git checkout branchAgit merge branchB
- merge conflicts
- sometimes it might be necessary to merge branches locally
- after forcing the merge locally, go to the IDE & accept/reject the conflicts
git checkout hashkey
git status: shows which branch you are at, and changes not set for commitgit pull: pull all contents from github repository into local repositorygit add 'foldername/scriptname.py': stage specific files for committinggit add -A: stage all changesgit add *: stages new files and modifications, without deletionsgit add -u: stages modifications and deletions, without new filesgit rm -r dir_name: add removed directory & contents withingit reset: unstage all files set for committinggit status: shows files changedgit remote -v: shows the source of repository in remote servergit remote rm origin: remove origin location from remote. Remote location needed for push / pull / fetch commandsgit remote add heroku https://git.heroku.com/xxx.git: add new remote locationgit diff: show changes made in files
alias lazygit="git add *; git add .gitlab-ci.yml; git commit -m 'test'; git push;";: calllazygitin bash
git tag -a v1.0.0 -m "1st prod version": tag in localgit push origin v1.0.0: push to github. can check from the commits sectiongit tag -d v1.0: delete local tag ONLYgit push -d origin v1.0.0: delete remote tag ONLYgit tag: list tags
git stash: store changes in stash before checkout in another branchgit stash list: list all the stashgit stash apply: after going back to branch, put back latest stash
git reset --hard HEAD~1: delete the top commit, unable to retrieve back changes after thisgit reset --hard 2b237987: delete till commit hash ID
git log: lists commits & their msgs/author/date from most recent first. type q to exitgit log --oneline: lists only commit messages & sha hash codegit show hash: list changesgit log --stat: lists commits and summary of changesgit log -p -2: lists commits in detail with their difference (change), -2 for two entries
- For single repo:
git config user.name: display current local usernamegit config user.name "Your Name": change username in current repo
- For all repos:
git config --global user.name: display global usernamegit config --global user.name "Your Name": change global username
- sometimes there can be conflicts when trying to push your commits to master, because there are already a conflict when someone else have committed new changes. a prompt saying
Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branchwill be given. To resolve this:- press
i> write your merge message > pressesc> write:wq> then press enter
- press
brew install git-lfsorsudo apt install git-lfs: install git LFSgit lfs install: update git hooksgit lfs track "*.onnx" "*.pb" "*.pth" "*.h5" "*.tflite" "*.mp4": add large file extensions- if repo is already created, use the link below to migrate large files to LFS
- see this video for greater clarity
- Others
- Download from LFS
git lfs pull: download all files from LFS
git clone --mirror git@gitlab.com:project/repo-name.git: clone only the .gitalias bfg="java -jar /home/jake/Desktop/vama/bfg-1.13.0.jar": in ubuntu, download bfg from official site and set an alias to its path within bash_profile; in Mac, use homebrew to installbfg --delete-files "*.{png,jpg,jpeg,bmp,mp4}" repo-name.git: delete certain file extensionscd repo-name.git: go into git directorygit reflog expire --expire=now --all && git gc --prune=now --aggressive: delete old filesgit push --force: push updated git to remote
git config --get remote.origin.url: just url, without internetgit remote show origin: full output with internet
- for folders
folder/ - for single file
file.txt - for all files of an extension
*.txt - for all folders in all directories
**\__pycache__ - https://medium.com/@haydar_ai/learning-how-to-git-ignoring-files-and-folders-using-gitignore-177556afdbe3
- Add new submodule
git submodule add -b master [URL to Git repo]: Adds.gitmodulesgit submodule init: add.gitmodulessubmodule repo link to.git/configgit submodule update --remote: git pull/checkout a detached head (not existing branch)
- Update commits
- cd into submodule folder <
git pull - update to submodule repo >
git add xx>git commit -m "xx">git push - all normal git commands work within submodule folder including git checkout, branching, logs
- cd into submodule folder <
- Pull branch with existing submodule
git checkout branchnamegit submodule init: add.gitmoduleslink to.git/configgit pull update --remote: checks out current commit
- Delete Submodule from Repo
git submodule deinit -f — mymodulerm -rf .git/modules/mymodulegit rm -f mymodule
- in the Gitlab/Github, clicking on the submodule folder will be directed to the source repo
- https://www.vogella.com/tutorials/GitSubmodules/article.html