Skip to content

Learning Git and GitHub with Harry on his YouTube channel CodeWithHarry.

Notifications You must be signed in to change notification settings

zulfequar/Git-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Git-tutorial

Learning Git and GitHub with Harry on his YouTube channel CodeWithHarry.

Git Commands


1. Clear the console:
clear
2. Logout from console:
logout
or
exit
3. Show the name of the Git user from Git configuration:
git config --global user.name
4. Set the name of the Git user in Git configuration:
git config --global user.name Zulfequar Ali
5. Show the email of the Git user from Git configuration:
git config --global user.email
6. Set the email of the Git user in Git configuration:
git config --global user.email develperzull@gmail.com
7. Open VS Code with the current directory open:
code .
8. Initialize a Git repository in the current directory:
git init
9. Show all the sub direcories in the current directory:
ls -lart
10. List all files in the current directory (using Unix command):
ls
11. Create a file in the current directory:
touch index.html
12. Get the current status of the repository:
git status
13. Show the current status of the repository in short:
git status -s
14. Add a single file to staging area:
git add index.html
15. Add mulitple files to staging area:
git add index.html about.html
16. Add all files to staging area:
git add --all
or
git add -A
or
git add .
17. Commit staged files:
git commit -m "Feature: Added a nav bar"
18. For a single, file restore changes from the last commit:
git checkout index.html
19. For all files, restore changes from the last commit:
git checkout -f
or
git checkout --force
20. Show all commits log:
git log
21. Show last 1 commit with changes:
git log -p -1
Hint: Press 'q' for quitting.
22. Show last 1 commit:
git log -1
or
git log -n 1
or
git log --max-count=1
23. Show differences between working tree files and staged files:
git diff
24. Show differences between staged files and last committed files:
git diff --staged
25. Directly commit all files without staging them:
git commit -a -m "Feature: Added new files and directly commited without staging."
26. Remove file from the working tree and from the index (i.e. local file is also deleted):
git rm waste.html
27. Move a staged or committed file to untracked area (file is first moved to staging area then deleted from there and moved to untracked area):
git rm --cached waste.html
28. Ignore some files and don't track them:
Create '.gitignore' file in the root directory of the repository and list out the filenames to be ignored.
Add '*.log' to the list to ingore all files with .log extension.
Add '<directory_name>/' to the list to ingnore the directory and its contents.
Add '<file_path>' to the list ingore a file at a specific folder.
29. Show all branches:
git branch
30. Create a branch from current branch:
git branch <branch_name>
31. Switch to a branch:
git checkout <branch_name>
32. Create a new branch and switch to it as well:
git checkout -b <branch_name>
33. Merge a branch to current branch:
git merge <branch_name>
34. Delete a branch:
git branch -d <branch_name>
or
git branch -D <branch_name>
35. Add a remote repository (remote):
git remote add <remote_name> <repository_url>
36. Show all remotes:
git remote
37. Show romtes with their URLs:
git remote -v
or
git remote -verbose
38. Push a (local) branch to a remote:
git push <remote_name> <branch_name>
39. Generate a new SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com"
or
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
40. Start the ssh-agent in the background:
eval "$(ssh-agent -s)"
41. Add your SSH private key to ssh-agent:
ssh-add ~/.ssh/id_ed25519
[Note: If you created your key with a different name, or if you are adding an existing key that
has a different name, replace id_ed25519 in the command with the name of your private key file.]
or
ssh-add <ssh_key_file_address>
or
ssh-add <local_repository_root_directory_address/ssh_key_filename>
42. Copy the SSH public key contents to your clipboard:
clip < ~/.ssh/id_ed25519.pub
or
clip < <ssh_public_key_file_address>
43. Print the SSH public key contents on the terminal:
cat ~/.ssh/id_ed25519.pub
or
cat <ssh_public_key_file_address>
44. Test your SSH connection (attempts to ssh to GitHub):
ssh -T git@github.com

You may see a warning like this: > The authenticity of host 'github.com (IP ADDRESS)' can't be established. > RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. > Are you sure you want to continue connecting (yes/no)?

Verify that the fingerprint in the message you see matches GitHub's public key fingerprint. If it does, then type yes: > Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

Verify that the resulting message contains your username.

45. Show the current HTTPS of SSH URLs for a remote:
git remote get-url <remote_name>
or
git remote get-url --all <remote_name>
46. Change the URL for a remote:
git remote set-url <remote_name> <new_url>
47. Update the remote by pushing/uploading the local files of a branch and add the upstream (tracking) reference as well:
git push -u <remote_name> <branch_name>
or
git push --set-upstream <remote_name> <branch_name>
48. Update the remote by pushing local files from the current branch (last pushed branch using -u or --set-upstream option):
git push
49. Clone/copy a public repository:
git clone <repository_url>
50. Clone a public repository into a new directory:
git clone <repository_url> <new_dictory_name>
51. Set your current local branch to exactly match the remote branch can be done in two steps:
git fetch origin
git reset --hard origin/master

About

Learning Git and GitHub with Harry on his YouTube channel CodeWithHarry.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages