How to: Basics of using Git Emman, November 11, 2023November 11, 2023 This tutorial will guide you through basics of using Git, including setting up an SSH connection. This tutorial assumes you have Git installed on your machine. 1. Install Git: If you haven’t installed Git yet, download and install it from git-scm.com. 2. Configure Git: Open a terminal and configure Git with your name and email: git config --global user.name "Your Name" git config --global user.email "your.email@example.com" 3. Create a New Repository: Create a new directory for your project and initialize a Git repository: mkdir my_project cd my_project git init 4. Create a File and Make Changes: Create a new file in your project directory and add some content to it: echo "Hello, Git!" > myfile.txt 5. Stage and Commit Changes: Tell Git to track your changes: git add myfile.txt Now, commit the changes: git commit -m "Initial commit" 6. View Commit History: Check the commit history: git log 7. Create a Remote Repository (GitHub, GitLab, Bitbucket, etc.): Go to your Git hosting provider and create a new repository. 8. Connect Local Repository to Remote: Assuming you’ve created a new repository on GitHub, connect your local repository to the remote: git remote add origin git@github.com:yourusername/your-repo.git Replace “yourusername” and “your-repo” with your GitHub username and repository name. 9. Push Changes to Remote Repository: Push your local commits to the remote repository: git push -u origin master 10. Setting Up SSH for Git: 10.1 Generate SSH Key: If you haven’t generated an SSH key, do so: ssh-keygen -t rsa -b 4096 -C "your.email@example.com" 10.2 Add SSH Key to SSH Agent: eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa 10.3 Add SSH Key to Git Provider: Copy your public key: cat ~/.ssh/id_rsa.pub Add this key to your Git hosting provider (GitHub, GitLab, etc.): GitHub: Settings -> SSH and GPG keys -> New SSH key. GitLab: Settings -> SSH Keys -> Add SSH Key. Bitbucket: Settings -> Security -> SSH keys -> Add Key. 11. Test SSH Connection: Test your SSH connection to GitHub: ssh -T git@github.com If successful, you’ll see a message indicating authentication. 12. Pull Changes from Remote: If someone else has made changes to the repository, you can pull them: git pull origin master 13. Branching: Create a new branch: git branch feature-branch git checkout feature-branch # or use "git switch feature-branch" in newer Git versions Make changes, commit, and push your branch. 14. Merging: Merge your branch into the main branch: git checkout master # or use "git switch master" in newer Git versions git merge feature-branch 15. Handling Conflicts: If there are conflicts during a merge, Git will notify you. Resolve conflicts in the affected files, then: git add conflicted-file.txt git merge --continue 16. Clone an Existing Repository: Clone an existing repository: git clone git@github.com:username/repo.git Replace “username” and “repo” with the username and repository name. This tutorial has covered the basics of using Git, from creating a repository to making changes, collaborating with others, and setting up SSH for secure communication with remote repositories. Keep practicing and exploring additional Git features as you become more comfortable with the basics. Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe Dev Developer Git SSH