-
Install Git appropriate for the platform
-
Stow git folder from dotfiles repo.
.gitconfig
and.gitignore
file should be stowed. Change the contents if necessary.git config --global user.name "Mona Lisa" git config --global user.email "[email protected]"
If the email needs to be private, check out this link
-
Stow ssh from dotfiles repo. It should show
.ssh/config
file. Modify if needed. -
Create ssh key and sync with github. Generate using
ssh-keygen
with appropriate key requirement. SinceAddKeysToAgent yes
entry is present withinHost *
field inconfig
file, we can usessh-add
to add the private key we created to the ssh-agent. This allows the agent to take care of autheticating at every communication with the remote url. -
Goto github profile settings, ssh and gpg key tab and store the public key with appropriate name.
-
Test the ssh connection by running the command
ssh -T [email protected]
. If done correctly, executing the command should be greeted with success message. If errors, check if ssh public key is correct. We are now ready to ssh into repository we create. This way we needn't enter github credentials everytime we push or pull and a lot more secure. -
If you intend to change the default branch from
master
to some other name, use this link. Or simply each time with a repo with the commandgit branch -M main
-
Create a repo in github. Adjust visibility, license as required.
Readme.md
and.gitignore
can be added later. -
Having created a repo will display useful commands to get started.
-
We can either clone the created repo or initialise the local folder with
git init
and update the remote-url withgit remote add <remotename> <ssh link of the repo>
where<remotename>
is generallyorigin
. -
git status
shows the status of the current working directory with files that have been modified, added, deleted etc.git status
andgit diff
give almost similar results withgit diff
giving a detailed list of the changes. -
git add .
andgit add <file>
can be used to stage the untracked files. -
git commit -m <message>
can be used to commit the staged files.git commit --amend -m "<amended msg>"
can be used to amend previously commit and not yet pushed to remote files. -
git log
gives the log of all the commits done so far. If the file is staged, then the last commit will be with(HEAD -> <branchname>)
and next commit which has already been push with(<remotename>/<branchname>, <remotename>/HEAD)
. It mean theHEAD
(pointer showing the status of the commit) is locally staged. -
To publish the local commits, use
git push -u <remotename> <branchname>
, where<remotename>
is generallyorigin
. Nowgit log
will show that theHEAD
aligns with remote branch.15a. Push can also be done using
git push --set-upstream <remotename> <branchname>
. -
Tip: create aliases in
.bashrc
or.zshrc
for commonly used git commands and use aliases instead. -
Solving conflicts or mistakes is the part of working with git. Remember
git reset --HARD
is almost the last resort to try resolve conflicts. Almost for all git related problems, internet has answers.
Cheatsheet - https://training.github.com/downloads/github-git-cheat-sheet/
all commands - https://git-scm.com/docs
Git SCM Book - https://git-scm.com/book/en/v2
All remote commands - https://git-scm.com/docs/git-remote.html
Contribute to a project - https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project
Contribute to an external repo example - https://docs.github.com/en/get-started/using-git/about-git#example-contribute-to-an-existing-repository
ssh-keygen -t rsa -b 4096 -C "sample_4096" -f ~/.ssh/sample_key
#add public key to github settings --> ssh keys
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/sample_key #path to private key
ssh -T [email protected]
git fetch origin
git reset --hard origin/master
git clean -f -d