Git / github cheatsheet
Installation of git
- Download git from here
- or use on debian based linux
- Check if git is installed
1
git --version
- Configure git
- (Optional) Change the default brach name from
master
tomain
1
git config --global init.defaultBranch main
- Configuring SSH keys to github
Run these commands on the terminalGo to Github>Settings>SSH and GPG keys. Click on New SSH key. Give a title and paste the key in the key field. Click on Add SSH key. The key should be available from the last command on the terminal.
Git commands
git init
- initialize a git repositorygit add <file>
- add a file to the staging areagit add .
- add all files to the staging areagit add -A
- add all files to the staging area
git commit -m "message"
- commit changes to the local repositorygit push
- push changes to the remote repositorygit push -u origin <branch_name>
- push changes to a branchgit push origin <branch_name>
- push changes to a branch
git pull
- pull changes from the remote repositorygit pull origin <branch_name>
- pull changes from a branch
git status
- check the status of the repositorygit log
- view the commit historygit branch
- view the branchesgit branch <branch_name>
- create a new branchgit checkout <branch_name>
- switch to a branchgit checkout -b <branch_name>
- create and switch to a branchgit merge <branch_name>
- merge a branch into the current branchgit clone <url>
- clone a remote repositorygit remote add origin <url>
- add a remote repositorygit remote -v
- view the remote repositoriesgit remote set-url origin <url>
- change the url of the remote repositorygit remote remove origin
- remove the remote repository- git submodule: git submodule is used to add a git repository inside another git repository. This is useful when you want to use a git repository inside another git repository. For example, you can use git submodule to add a git repository that contains a library to your project. This way, you can use the library in your project without having to copy the library files into your project directory.
git submodule add <url>
- add a submodulegit submodule init
- initialize the submodulegit submodule update
- update the submodulegit submodule update --remote
- update the submodule to the latest commit