Skip to content

Instantly share code, notes, and snippets.

@dr563105
Last active June 16, 2022 07:27
Show Gist options
  • Save dr563105/4422d93398c50925e9f8b96c44e7dfb4 to your computer and use it in GitHub Desktop.
Save dr563105/4422d93398c50925e9f8b96c44e7dfb4 to your computer and use it in GitHub Desktop.
First steps with Git
  1. Install Git appropriate for the platform

  2. 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 "john@xyx.com"
    

    If the email needs to be private, check out this link

  3. Stow ssh from dotfiles repo. It should show .ssh/config file. Modify if needed.

  4. Create ssh key and sync with github. Generate using ssh-keygen with appropriate key requirement. Since AddKeysToAgent yes entry is present within Host * field in config file, we can use ssh-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.

  5. Goto github profile settings, ssh and gpg key tab and store the public key with appropriate name.

  6. Test the ssh connection by running the command ssh -T git@github.com. 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.

  7. 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 command git branch -M main

  8. Create a repo in github. Adjust visibility, license as required. Readme.md and .gitignore can be added later.

  9. Having created a repo will display useful commands to get started.

  10. We can either clone the created repo or initialise the local folder with git init and update the remote-url with git remote add <remotename> <ssh link of the repo> where <remotename> is generally origin.

  11. git status shows the status of the current working directory with files that have been modified, added, deleted etc. git status and git diff give almost similar results with git diff giving a detailed list of the changes.

  12. git add . and git add <file> can be used to stage the untracked files.

  13. 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.

  14. 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 the HEAD(pointer showing the status of the commit) is locally staged.

  15. To publish the local commits, use git push -u <remotename> <branchname>, where <remotename> is generally origin. Now git log will show that the HEAD aligns with remote branch.

    15a. Push can also be done using git push --set-upstream <remotename> <branchname>.

  16. Tip: create aliases in .bashrc or .zshrc for commonly used git commands and use aliases instead.

  17. 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.

Links

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

Generate ssh key

ssh-keygen -t rsa -b 4096 -C "sample_4096" -f ~/.ssh/sample_key
#add public key to github settings --> ssh keys

add it to ssh agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/sample_key #path to private key

Test ssh connection

ssh -T git@github.com

Sync local with remote branch

git fetch origin
git reset --hard origin/master
git clean -f -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment