Understanding Git and GitHub

All important Git and GitHub commands cheat sheet

·

5 min read

Understanding Git and GitHub

Git Ready to Git Going: Understanding Git and GitHub

As a developer, it is very important for you to learn Git and GitHub. In this brief introduction to Git and GitHub, you will learn about Git and GitHub and some extremely important commands. I have compiled a list of the most useful Git commands you can use while working on a project.

Git

Git is a tool that you can use to manage the versions of your files and create a local repository on your computer. It helps you keep track of the project's history. Who changed what and when, and who made it?

First, download and install git.

Using the terminal, we can manipulate file structures. Here are a few useful commands you should know about.

  1. List all the commands in a folder →ls

  2. Make a directory → mkdir project

  3. Change directory → cd project/

  4. To see the history of the project → git init

  5. To list hidden files →ls -a

  6. What is inside the folder → ls .git

  7. Create a file Linux cmd →touch names.txt

  8. Check what changes are made in the project →git status

  9. Remove a file → rm -rd <filename>

  10. Maintaining history → git add . / git <individual file name>

  11. Commit the changes → git commit -m “message”

  12. Add content in a file →vi/vim <filename> you can add whatever you want to add.

  13. Display the content that is available in the file →cat <filename>

  14. To unstage changes that have been added to the Git index but not yet committed →git restore –staged <filename>

  15. To remove a file completely →git rm <filename>

  16. To remove a file from the working directory but not from the Git index →git rm — cached <file>

  17. To remove the changes to the file from the index and also discard the changes in the working directory → git reset HEAD <filename>

  18. To see the history of the changes you made →git log

  19. Use git stash if you don’t want to commit changes to the history of the project.

  20. Add another change in a file (modified name) → git touch <filename>

  21. Try to add some changes →vi <filename>

  22. If you don’t want to commit and lose changes → git stash

  23. To retrieve changes later → git stash apply

  24. To apply the changes and remove them from the stash at the same time →git stash pop

  25. Want to delete all stash files →git stash clear

GitHub

GitHub is a cloud-based service that hosts your Git repositories.

  1. First of all, create a repository on GitHub.

  2. To connect that repository to the local project→ git remote add origin <repo url>

  3. To show the attached folders with the project →git remote -v

  4. To push the changes →git push origin master

  5. To prevent users from being impacted by your changes (unfinished code), create a new branch →git branch <branchname>

  6. To switch branches →git checkout <branchname> / git switch <branchname>

  7. To combine changes from one branch into another → git merge

  8. To display information about the remote repositories that your local repository is connected to →git remote -v

  9. To push all changes →git push origin master

Contribute to a project on GitHub

  1. First of all fork the project.

  2. Clone the repo →git clone <url>

  3. From where you have forked the project URL →git remote add upstream <url>

  4. To show a list of the remote repositories connected to your local Git repository →git remote -v

  5. Create a new branch →git branch <branchname>

  6. To switch between different branches in Git →checkout/switch <branchname>

  7. Add code →git add .

  8. Give it a message →git commit -m “message”

  9. Then →git log

  10. Now you want to push that new branch you made on your account to the original project →git push upstream

  11. To push changes from a local branch to a remote branch on the GitHub repository → git push origin <branchname>

  12. Add all the changes → git add .

  13. To create a new commit with a message →git commit -m “message”

  14. To push all changes →git push origin <branchname>
    If you’re adding many features with just one pull request, it would be very challenging to review your code. To avoid confusion, create separate branches and separate pull requests for each feature. One pull request can be opened per branch.

  15. To see all the commits and find the one you want to remove →git log

  16. Remove an unnecessary commit (copy the number id from the log you want to delete from commits) → git reset id

  17. git add .
    git statusgit stash
    git log

  18. Make fork repository update with original branch →git checkout main
    git status
    git loggit fetch -all –prune
    git reset –hard upstream/main
    git log
    git push origin main
    git pull upstream main
    git push origin main

Squashing

Let’s say you have many commits and you want to merge that

  1. Create a branch named “temp” →git branch temp

  2. Switch branch →git checkout temp

  3. Create a file named '1' →touch 1

  4. Add changes → git add .

  5. Commit changes with the message "add 1" →git commit -m “add 1”

  6. Create another file named ‘2’ → touch 2

  7. Add changes → git add .

  8. Commit changes with the message “add 2” →git commit -m “add 2”

  9. To see the history of changes you made → git log

  10. To rebase the current branch onto the commit specified by <log id>git rebase <log id>

  11. Force push changes to remote repository → git push -f

  12. Exit out of this →Esc:x

Merge conflicts

Here are some common commands that you can use to resolve merge conflicts.

  1. To avoid conflicts later on →git pull

  2. To switch to the branch →git switch/git checkout

  3. To merge the changes from another branch into the current branch →git merge <branchname>

  4. Check conflicts →git status

  5. to see what changes are causing conflicts →git diff

  6. Implement the changes after the conflicts are finally resolved →git add

  7. Commit changes →git commit

  8. Finally, push the code to the remote repository →git push

Wrap up

That concludes the discussion. I sincerely hope it is useful to you. If so, clap it, share it with others, and follow me, Ishrat Umar for more. Ask me on Twitter at @ishratUmar18 if you have any questions.

Happy learning!

Did you find this article valuable?

Support Ishrat by becoming a sponsor. Any amount is appreciated!