Youssef Samir

Get in touch

Git

Learn the essentials of Git, a distributed version control system, used for tracking changes in source code and collaborating with developers.

General Commands

  • git --help: Displays options for git commands.
  • git -h: Provides a shorter summary for options and help.

Initializing a Repository

  • git init: Initializes a git repository in the current directory.

Staging and Committing

  • git add <file1> <file2>: Adds specified files to the staging area.
  • git add .: Adds all files to the staging area.
  • git status: Checks untracked, modified files and files in the staging area.
  • git commit -m "<commit description>": Commits changes in the staging area with a message.
  • git commit -a -m "<description>": Adds all changes to staging and commits in one step.

Removing and Renaming Files

  • rm <filename>: Removes a file from the directory but not from the staging area.
  • git add <deleted file from directory>: Removes the deleted file from the staging area.
  • git ls-files: Lists the files in git's staging area.
  • git rm <filename>: Removes the file from both the working directory and the staging area.
  • git mv <old file name> <new file name>: Renames a file.

Ignoring Files

  • echo <directory/> >> .gitignore: Adds a directory or file to the .gitignore file to ignore changes.

Status and Diff Tools

  • git status -s: Provides a shorter summary; the left column indicates the staging area and the right column indicates the working directory.
  • git difftool: Launches the diff tool.
  • git reset <file name>: Unstages a file.
  • git reset HEAD~1: Undoes the last commit.
  • git reset <hash of a commit>: Goes back to a certain commit.
  • git log: Prints the commit history.
  • git log --oneline: Provides a shorter description of commits.
  • git restore --staged <filename>: Removes a file from the staging area.

Pushing and Pulling

  • git push -u origin master: Pushes the changes to the master branch on the remote repository.
  • git pull: Fetches and merges changes from the remote repository.

Branching

  • git branch: Displays branches.
  • git checkout <name of the branch>: Switches between branches.
  • git checkout -b <name of the branch>: Creates a new branch.
  • git branch -d <name of the branch>: Deletes a branch.
  • git merge <branch to merge to>: Merges a specified branch into the current branch.

Configurations

  • git config --global user.name "<user name>": Sets the username at a global level.
  • git config --global user.email <email>: Sets the email at the global level.
  • git config --global core.editor "code --wait": Sets the default editor to VS Code.
  • git config --global -e: Opens the global settings in the default editor.
  • git config --global core.autocrlf true: Sets the end of line to automatic add CRLF (for Windows).
  • git config --global core.autocrlf input: Manages end of line from other users (for Mac/Linux).
  • git config --global difftool.code.cmd "code --wait --diff $LOCAL $REMOTE": Sets VS Code as the diff tool.
  • git config --global diff.tool code: Sets the diff tool to VS Code.

Connecting with GitHub

Connecting via SSH

  1. Generate a key pair on the local machine:
    • ssh-keygen -t rsa -b 4096 -C "<email.com>": Generates an SSH key via RSA encryption.
  2. Copy the generated public key and add it to GitHub:
    • cat testkey.pub: Displays the public key to copy.

Publishing a Local Repository to GitHub

  1. Create a new directory for the repository.
  2. Initialize Git in the created directory.
  3. Write code, add, and commit changes.
  4. Create a new repository on GitHub.
  5. Add the remote repository:
    • git remote add origin <ssh link>: Links the local repository to the GitHub repository.